[Quartz]如何动态地将作业添加到一个石英调度程序中?

时间:2015-12-28 03:25:02

标签: java spring quartz-scheduler

我启动了调度程序一段时间,然后我将新作业添加到此调度程序中,并发现此新作业将立即执行,因为此作业的开始时间与调度程序相同。如果我只是希望工作生效,我将它添加到Scheduler中,我该怎么办?谢谢!!!!! 这是代码:

SchedulerFactory sf = new StdSchedulerFactory();
    Scheduler sched = sf.getScheduler();
    JobDetail job = newJob(InvokeStatJob.class)             //print "hello -- "+ new Date()
            .withIdentity("job1", "group1")
            .build();
    CronTrigger trigger = newTrigger()
            .withIdentity("trigger1", "group1")
            .withSchedule(cronSchedule("0/2 * * * * ?"))
            .build();
    JobDetail job2 = newJob(InvokeStatJob2.class)           //print "world! --- "+new Date()
            .withIdentity("job2", "group2")
            .build();
    CronTrigger trigger2 = newTrigger()
            .withIdentity("trigger2", "group2")
            .withSchedule(cronSchedule("0/3 * * * * ?"))
            .build();
    sched.scheduleJob(job, trigger);
    sched.start();
    Thread.sleep(10L * 1000L);
    System.out.println("=========================");
    sched.scheduleJob(job2,trigger2);
    Thread.sleep(30L*1000L);
    sched.shutdown(true);

这是结果:

hello -- Mon Dec 28 10:33:18 CST 2015
hello -- Mon Dec 28 10:33:20 CST 2015
hello -- Mon Dec 28 10:33:22 CST 2015
hello -- Mon Dec 28 10:33:24 CST 2015
hello -- Mon Dec 28 10:33:26 CST 2015
hello -- Mon Dec 28 10:33:28 CST 2015
=========================
world! --- Mon Dec 28 10:33:28 CST 2015
world! --- Mon Dec 28 10:33:28 CST 2015
world! --- Mon Dec 28 10:33:28 CST 2015
world! --- Mon Dec 28 10:33:28 CST 2015
hello -- Mon Dec 28 10:33:30 CST 2015
world! --- Mon Dec 28 10:33:30 CST 2015
hello -- Mon Dec 28 10:33:32 CST 2015
world! --- Mon Dec 28 10:33:33 CST 2015
hello -- Mon Dec 28 10:33:34 CST 2015
hello -- Mon Dec 28 10:33:36 CST 2015
world! --- Mon Dec 28 10:33:36 CST 2015

第一个4“世界!---星期一12月28日10:33:28 CST 2015”之后“========================= “不是我想要的。

1 个答案:

答案 0 :(得分:0)

来自Quartz Scheduler Developer Guide : Working with Triggers : Triggers

  

startTime属性指示触发器的计划何时首次生效。该值是一个java.util.Date对象,它定义给定日历日期的时刻。对于某些触发类型,触发器实际上会在开始时触发。对于其他人,它只是标记应该开始遵循计划的时间。这意味着你可以存储一个触发器,其中包含一个时间表,例如1月份的“每月的第5天”,如果startTime属性设置为4月1日,那么它将在第一次触发前几个月。

代码应为:

SchedulerFactory sf = new StdSchedulerFactory();
    Scheduler sched = sf.getScheduler();
    JobDetail job = newJob(InvokeStatJob.class)             //print "hello -- "+ new Date()
            .withIdentity("job1", "group1")
            .build();
    CronTrigger trigger = newTrigger()
            .withIdentity("trigger1", "group1")
            .withSchedule(cronSchedule("0/2 * * * * ?"))
            .build();
    sched.scheduleJob(job, trigger);
    sched.start();
    Thread.sleep(10L * 1000L);
    System.out.println("=========================");
    JobDetail job2 = newJob(InvokeStatJob2.class)           //print "world! --- "+new Date()
            .withIdentity("job2", "group2")
            .build();
    CronTrigger trigger2 = newTrigger()
            .withIdentity("trigger2", "group2")
            .startNow()
            .withSchedule(cronSchedule("0/3 * * * * ?"))
            .build();
    sched.scheduleJob(job2, trigger2);
    Thread.sleep(30L*1000L);
    sched.shutdown(true);