quartz 2.0实例

Example 3 - Cron-based Triggers

This example is designed to demonstrate how you can use Cron Triggers to schedule jobs. This example will fire off several simple jobs that say “Hello World” and display the date and time that the job was executed.

The program will perform the following actions:

  • Start up the Quartz Scheduler
  • Schedule several jobs using various features of CronTrigger
  • Wait for 300 seconds (5 minutes) to give Quartz a chance to run the jobs
  • Shut down the Scheduler

Note: Refer to the Quartz javadoc for a thorough explanation of CronTrigger.

Running the Example

This example can be executed from the examples/example3 directory. There are two out-of-the-box methods for running this example

  • example3.sh - A UNIX/Linux shell script
  • example3.bat - A Windows Batch file

The Code

The code for this example resides in the package org.quartz.examples.example3.

The code in this example is made up of the following classes:

Class NameDescription
CronTriggerExample The main program
SimpleJob A simple job that says Hello World and displays the date/time

SimpleJob

SimpleJob is a simple job that implements the Job interface and logs a nice message to the log (by default, this will simply go to the screen). The current date and time is printed in the job so that you can see exactly when the job is run.

public void execute(JobExecutionContext context) throws JobExecutionException {
    JobKey jobKey = context.getJobDetail().getKey();
    _log.info("SimpleJob says: " + jobKey + " executing at " + new Date());
}

CronTriggerExample

The program starts by getting an instance of the Scheduler. This is done by creating a StdSchedulerFactory and then using it to create a scheduler. This will create a simple, RAM-based scheduler.

SchedulerFactory sf = new StdSchedulerFactory();
Scheduler sched = sf.getScheduler();

Job #1 is scheduled to run every 20 seconds

JobDetail job = newJob(SimpleJob.class)
    .withIdentity("job1", "group1")
    .build();

CronTrigger trigger = newTrigger()
    .withIdentity("trigger1", "group1")
    .withSchedule(cronSchedule("0/20 * * * * ?"))
    .build();

sched.scheduleJob(job, trigger);

Job #2 is scheduled to run every other minute, starting at 15 seconds past the minute.

job = newJob(SimpleJob.class)
    .withIdentity("job2", "group1")
    .build();

trigger = newTrigger()
    .withIdentity("trigger2", "group1")
    .withSchedule(cronSchedule("15 0/2 * * * ?"))
    .build();

sched.scheduleJob(job, trigger);

Job #3 is scheduled to every other minute, between 8am and 5pm (17 o’clock).

job = newJob(SimpleJob.class)
    .withIdentity("job3", "group1")
    .build();

trigger = newTrigger()
    .withIdentity("trigger3", "group1")
    .withSchedule(cronSchedule("0 0/2 8-17 * * ?"))
    .build();

sched.scheduleJob(job, trigger);

Job #4 is scheduled to run every three minutes but only between 5pm and 11pm

job = newJob(SimpleJob.class)
    .withIdentity("job4", "group1")
    .build();

trigger = newTrigger()
    .withIdentity("trigger4", "group1")
    .withSchedule(cronSchedule("0 0/3 17-23 * * ?"))
    .build();

sched.scheduleJob(job, trigger);

Job #5 is scheduled to run at 10am on the 1st and 15th days of the month

job = newJob(SimpleJob.class)
    .withIdentity("job5", "group1")
    .build();

trigger = newTrigger()
    .withIdentity("trigger5", "group1")
    .withSchedule(cronSchedule("0 0 10am 1,15 * ?"))
    .build();

sched.scheduleJob(job, trigger);

Job #6 is scheduled to run every 30 seconds on Weekdays (Monday through Friday)

job = newJob(SimpleJob.class)
    .withIdentity("job6", "group1")
    .build();

trigger = newTrigger()
    .withIdentity("trigger6", "group1")
    .withSchedule(cronSchedule("0,30 * * ? * MON-FRI"))
    .build();

sched.scheduleJob(job, trigger);

Job #7 is scheduled to run every 30 seconds on Weekends (Saturday and Sunday)

job = newJob(SimpleJob.class)
    .withIdentity("job7", "group1")
    .build();

trigger = newTrigger()
    .withIdentity("trigger7", "group1")
    .withSchedule(cronSchedule("0,30 * * ? * SAT,SUN"))
    .build();

sched.scheduleJob(job, trigger);

The scheduler is then started (it also would have been fine to start it before scheduling the jobs).

sched.start();

To let the program have an opportunity to run the job, we then sleep for five minutes (300 seconds). The scheduler is running in the background and should fire off several jobs during that time.

Note: Because many of the jobs have hourly and daily restrictions on them, not all of the jobs will run in this example. For example: Job #6 only runs on Weekdays while Job #7 only runs on Weekends.

Thread.sleep(300L * 1000L);

Finally, we will gracefully shutdown the scheduler:

sched.shutdown(true);

Note: passing true into the shutdown message tells the Quartz Scheduler to wait until all jobs have completed running before returning from the method call.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值