下面我们来看看两个小demo实现定时任务的执行:
一、
package test2; import org.quartz.CronScheduleBuilder; import org.quartz.JobBuilder; import org.quartz.JobDetail; import org.quartz.Scheduler; import org.quartz.SchedulerFactory; import org.quartz.Trigger; import org.quartz.TriggerBuilder; import org.quartz.impl.StdSchedulerFactory; public class SchedulerTest { public static void main(String[] args) { // 通过schedulerFactory获取一个调度器 SchedulerFactory schedulerfactory = new StdSchedulerFactory(); Scheduler scheduler = null; try { // 通过schedulerFactory获取一个调度器 scheduler = schedulerfactory.getScheduler(); // 创建jobDetail实例,绑定Job实现类 // 指明job的名称,所在组的名称,以及绑定job类 JobDetail job = JobBuilder.newJob(MyJob.class) .withIdentity("job1", "jgroup1").build(); /* 定义调度触发规则*/ // 使用simpleTrigger规则 // Trigger // trigger=TriggerBuilder.newTrigger().withIdentity("simpleTrigger", // "triggerGroup") // .withSchedule(SimpleScheduleBuilder.repeatSecondlyForever(1).withRepeatCount(8)) // .startNow().build(); // 使用cornTrigger规则 每两秒钟执行一次 Trigger trigger = TriggerBuilder .newTrigger() .withIdentity("simpleTrigger", "triggerGroup") .withSchedule( CronScheduleBuilder.cronSchedule("0/2 * * * * ?")) .startNow().build(); // 把作业和触发器注册到任务调度中 scheduler.scheduleJob(job, trigger); // 启动调度 scheduler.start(); } catch (Exception e) { e.printStackTrace(); } } }
package test2; import java.util.Date; import org.quartz.Job; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; public class MyJob implements Job { @Override //把要执行的操作,写在execute方法中 public void execute(JobExecutionContext arg0) throws JobExecutionException { System.out.println("测试Quartz"+new Date()); } }
二、
package test2; import java.text.ParseException; import org.quartz.*; import org.quartz.impl.JobDetailImpl; import org.quartz.impl.StdSchedulerFactory; public class TestQuartz { public static void main(String[] args) throws SchedulerException, ParseException { SchedulerFactory schedulerFactory = new StdSchedulerFactory(); Scheduler scheduler = schedulerFactory.getScheduler(); JobDetail jobDetail = JobBuilder.newJob().ofType(HelloJob.class) .usingJobData("Test1", "Quartz") .withIdentity("Test1", "Group1").build();// 通过JobBuilder构建JobDetailImpl // 实例,也可以直接new // JobDetailImpl Trigger trigger = TriggerBuilder .newTrigger() .withSchedule(CronScheduleBuilder.cronSchedule("0/3 * * * * ?")) .forJob("Test1", "Group1") // Trigger找到对应的名称为Test1组为Group1的Job,如果不存在则会在执行scheduler.scheduleJob(jobDetail,trigger);报错 .build();// 通过TriggerBuilder构建CronTriggerImpl实例,也可以直接new // CronTriggerImpl scheduler.scheduleJob(jobDetail, trigger);// 任务每3秒触发一次 scheduler.start(); } }
package test2; import org.quartz.Job; import org.quartz.JobDataMap; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; public class HelloJob implements Job { @Override public void execute(JobExecutionContext arg0) throws JobExecutionException { // TODO Auto-generated method stub JobDataMap jobDataMap = arg0.getJobDetail().getJobDataMap(); String a = (String)jobDataMap.get("Test1"); System.out.println("Hello " + a +" hello quartz……"); } }
可以看看这篇文章:http://blog.csdn.net/pengpegv5yaya/article/details/37595889/
具体详解原理:http://lavasoft.blog.51cto.com/62575/181907/?from=singlemessage&isappinstalled=0