Spring 4.0.6 with quartz 1.8.6 : setCronExpression

2020-04-30 09:35发布

I am using spring version 4.0.6 and quartz with it, version 1.8.6. The problem is, when I try to use a configuration class instead of xml (with @Configuration annotation), I am not able to set the cron expression with the method

CronTriggerBean ctBean = new CronTriggerBean(); ctBean.setCronExpression("0 35 15 ? * MON *");

Please tell me if there is a version compatibility issue.

The exact error is this:

The method setCronExpression(String) is undefined for the type CronTriggerBean

1条回答
Bombasti
2楼-- · 2020-04-30 09:45

Consider using CronTriggerFactoryBean:

    @Bean
    public CronTriggerFactoryBean cronTriggerFactoryBean() {
        CronTriggerFactoryBean cronTriggerFactoryBean = new CronTriggerFactoryBean();
        cronTriggerFactoryBean.setJobDetail(jobDetailFactoryBean().getObject());
        cronTriggerFactoryBean.setCronExpression("0 35 15 ? * MON *");
        return cronTriggerFactoryBean;
    }
    @Bean
    public JobDetailFactoryBean jobDetailFactoryBean() {
        JobDetailFactoryBean jobDetailFactoryBean = new JobDetailFactoryBean();
        jobDetailFactoryBean.setJobClass(YOUR_CLASS.class);
        return jobDetailFactoryBean;
    }

and then:

CronTrigger trigger = cronTriggerFactoryBean.getObject();

The documentation here says: "NOTE: This FactoryBean works against both Quartz 1.x and Quartz 2.0/2.1, in contrast to the older CronTriggerBean class.".

查看更多
登录 后发表回答