import java.util.Date; import org.quartz.JobDetail; import org.quartz.Scheduler; import org.quartz.SchedulerFactory; import org.quartz.SimpleTrigger; import org.quartz.impl.StdSchedulerFactory; public class SimpleTriggerRunner { public static void main(String args[]) { try { // ①创建一个JobDetail实例,指定SimpleJob JobDetail jobDetail1 = new JobDetail("job1_1", "jGroup1", SimpleJob.class); // ②通过SimpleTrigger定义调度规则:马上启动,每2秒运行一次,共运行100次 SimpleTrigger simpleTrigger1 = new SimpleTrigger("trigger1_1", "tgroup1"); simpleTrigger1.setStartTime(new Date()); simpleTrigger1.setRepeatInterval(1000); simpleTrigger1.setRepeatCount(SimpleTrigger.REPEAT_INDEFINITELY); // ①创建一个JobDetail实例,指定SimpleJob JobDetail jobDetail2 = new JobDetail("job1_2", "jGroup2", DemoJob.class); // ②通过SimpleTrigger定义调度规则:马上启动,每2秒运行一次,共运行100次 SimpleTrigger simpleTrigger2 = new SimpleTrigger("trigger1_2", "tgroup2"); simpleTrigger2.setStartTime(new Date()); simpleTrigger2.setRepeatInterval(3000); simpleTrigger2.setRepeatCount(SimpleTrigger.REPEAT_INDEFINITELY); // ③通过SchedulerFactory获取一个调度器实例 SchedulerFactory schedulerFactory = new StdSchedulerFactory(); Scheduler scheduler = schedulerFactory.getScheduler(); scheduler.scheduleJob(jobDetail1, simpleTrigger1);// ④ 注册并进行调度 scheduler.scheduleJob(jobDetail2, simpleTrigger2);// ④ 注册并进行调度 scheduler.start();// ⑤调度启动 } catch (Exception e) { e.printStackTrace(); } } } //=============================================== import java.util.Date; import org.quartz.Job; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; public class SimpleJob implements Job { // ①实例Job接口方法 public void execute(JobExecutionContext jobCtx) throws JobExecutionException { System.out.println(jobCtx.getTrigger().getName() + " triggered. time is:" + (new Date())); } }