Spring 4.0.6 with quartz 1.8.6 : setCronExpression method is not exposed to CronTriggerBean class

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-20 04:38:18

问题


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:


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.".



来源:https://stackoverflow.com/questions/27901922/spring-4-0-6-with-quartz-1-8-6-setcronexpression-method-is-not-exposed-to-cron

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!