之前介绍过了quartz的一些相关理论知识,感觉上挺简单的,实际动手操作也确实如此,去quartz的官网上把Jar包下载下来以后,会看到它的目录里有例子程序,看完第一个例子觉得还可以,但是看后面两个例子程序有点让人小小的无语,可能quartz觉得程序写的越多,给出的使用方法也会越多,但是看上去很乱,而且大部分代码是重复的,所以我把他们简单化成一个例子,把它们记录下来。
本文一共有两个例子,这两个例子共用一个Job的实现,叫MyJob类,代码如下
package com.jll.quartz; import java.util.Date; import org.quartz.Job; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class MyJob implements Job { private static Logger logger = LoggerFactory.getLogger(MyJob.class); @Override public void execute(JobExecutionContext context) throws JobExecutionException { logger.info(context.getJobDetail().getKey() + " start to work at" + new Date()); } }
下面的代码是第一个例子,设置重复的次数和重复时间的
package com.jll.quartz; import java.util.Date; import org.quartz.DateBuilder; import org.quartz.JobBuilder; import org.quartz.JobDetail; import org.quartz.Scheduler; import org.quartz.SchedulerException; import org.quartz.SchedulerFactory; import org.quartz.SimpleScheduleBuilder; import org.quartz.SimpleTrigger; import org.quartz.TriggerBuilder; import org.quartz.impl.StdSchedulerFactory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class SimpleExample { public void run() throws SchedulerException{ Logger log = LoggerFactory.getLogger(SimpleExample.class); log.info("------- Initializing ----------------------"); SchedulerFactory factory = new StdSchedulerFactory(); Scheduler sched = factory.getScheduler(); log.info("------- Initialization Complete -----------"); log.info("------- Scheduling Job -------------------"); Date startTime = DateBuilder.evenMinuteDate(new Date()); JobDetail job = JobBuilder.newJob(MyJob.class).withIdentity("job1", "group1").build(); SimpleTrigger trigger = (SimpleTrigger)TriggerBuilder.newTrigger().withIdentity("trigger1", "group1").startAt(startTime).withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(1).withRepeatCount(10)).build(); Date ft = sched.scheduleJob(job, trigger); log.info(job.getKey() + " will run at: " + ft + " and repeat: " + trigger.getRepeatCount() + " times, every " + trigger.getRepeatInterval() / 1000L + " seconds"); sched.start(); try { Thread.sleep(30000L); } catch (Exception e) { } } public static void main(String[] args) throws Exception { SimpleExample example = new SimpleExample(); example.run(); } }
从代码中可以看出来任务重复10次,每1秒重复一次
第二个例子是使用CronExpression表达式的,
package com.jll.quartz; import java.util.Date; import org.quartz.CronScheduleBuilder; import org.quartz.CronTrigger; import org.quartz.JobBuilder; import org.quartz.JobDetail; import org.quartz.Scheduler; import org.quartz.SchedulerException; import org.quartz.SchedulerFactory; import org.quartz.SchedulerMetaData; import org.quartz.TriggerBuilder; import org.quartz.impl.StdSchedulerFactory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class CronExpressionExample { public void run() throws SchedulerException { Logger log = LoggerFactory.getLogger(CronExpressionExample.class); SchedulerFactory factory = new StdSchedulerFactory(); Scheduler sched = factory.getScheduler(); JobDetail job = JobBuilder.newJob(MyJob.class) .withIdentity("job1", "group1").build(); CronTrigger trigger = TriggerBuilder .newTrigger() .withIdentity("trigger1", "group1") .withSchedule( CronScheduleBuilder.cronSchedule("0/2 * * * * ?")) .build(); Date date = sched.scheduleJob(job, trigger); log.info(job.getKey() + " has been scheduled to run at: " + date + " and repeat based on expression: " + trigger.getCronExpression()); sched.start(); log.info("------- Started Scheduler -----------------"); try { Thread.sleep(30000L); } catch (Exception e) { } log.info("------- Shutting Down ---------------------"); sched.shutdown(true); log.info("------- Shutdown Complete -----------------"); SchedulerMetaData metaData = sched.getMetaData(); log.info("Executed " + metaData.getNumberOfJobsExecuted() + " jobs."); } public static void main(String[] args) throws Exception { CronExpressionExample example = new CronExpressionExample(); example.run(); } }
从上面的代码可以看出来任务每两秒钟执行一次,主线程30秒后死亡。