如何通过数据库在Quartz Schedular中设置日期和时间

时间:2018-06-27 04:32:03

标签: java

我正在尝试从Quartz Schedular中的数据库设置日期和时间,但是无法这样做。请帮帮我。

这是我的代码:

public class CroneScheduler {

    public CroneScheduler() throws Exception {

        SchedulerFactory sf = new StdSchedulerFactory();

        Scheduler sche = sf.getScheduler();

        sche.start();

        JobDetail jDetail;
        jDetail = new JobDetail("Newsletter", "NJob", MyJob.class);

        //"0 0 12 * * ?" Fire at 12pm (noon) every day
        //"0/2 * * * * ?" Fire at every 2 seconds every day
        CronTrigger crTrigger = new CronTrigger("cronTrigger", "NJob", "0/2 * * * * ?");

        sche.scheduleJob(jDetail, crTrigger);
    }
}

1 个答案:

答案 0 :(得分:1)

如果quartz的版本为 1.7.2 ,则可以使用以下代码:

public void resetJob(String expression){
    ApplicationContext applicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(context);
    Scheduler scheduler = (Scheduler) applicationContext.getBean("testScheduler");
    try {
        CronTriggerBean trigger = new CronTriggerBean();
        trigger.setCronExpression(expression);
        trigger.setName("testJobTrigger");
        trigger.setGroup(Scheduler.DEFAULT_GROUP);
        trigger.setJobName("testJobDetail");
        scheduler.rescheduleJob("testJobTrigger", Scheduler.DEFAULT_GROUP, trigger);
    } catch (SchedulerException | ParseException e) {
        e.printStackTrace();
    }
}

如果quartz的版本高于 1.7.2 ,则可以使用以下代码:

public void resetJob(String expression){
        ApplicationContext applicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(context);
        Scheduler scheduler = (Scheduler) applicationContext.getBean("testScheduler");
        CronTriggerImpl trigger = null;
        try {
            TriggerKey triggerKeys = TriggerKey.triggerKey("testJobTrigger",Scheduler.DEFAULT_GROUP);
            trigger = new CronTriggerImpl();
            trigger.setCronExpression(expression);
            trigger.setKey(triggerKeys);//keep key the same
            scheduler.rescheduleJob(triggerKeys,trigger);
        } catch (ParseException | SchedulerException e) {
            e.printStackTrace();
        }
}