Spring-Boot Quartz Job未运行

时间:2017-06-26 11:47:27

标签: java spring spring-boot cron quartz-scheduler

我刚开始以动态方式创建Quartz Scheduled Jobs。因此,我在QuartzConfig类中创建了SchedulerFactoryBeanJobDetailFactoryBeanCronTriggerFactoryBean。其中Job和CronTrigger bean是原型bean。

@Configuration
public class QuartzConfig {

    @Autowired
    ApplicationContext context;;

    @Bean
    public SchedulerFactoryBean quartzScheduler(){
        SchedulerFactoryBean quartzScheduler = new SchedulerFactoryBean();
        quartzScheduler.setOverwriteExistingJobs(true);
        quartzScheduler.setSchedulerName("job-scheduler");
        AutowiringSpringBeanJobFactory jobFactory = new AutowiringSpringBeanJobFactory();
        jobFactory.setApplicationContext(context);
        quartzScheduler.setJobFactory(jobFactory);
        return quartzScheduler;
    }

    @Bean
    @Scope(value = "prototype")
    public JobDetailFactoryBean getJobBean(){
        JobDetailFactoryBean bean = new JobDetailFactoryBean();
        bean.setJobClass(DailyJob.class);
        bean.setGroup("daily-group");
        bean.setName("daily-name");
        bean.setBeanName("daily-name");
        bean.getJobDataMap().put("daily", "daily");
        return bean;
    }

    @Bean
    @Scope(value = "prototype")
//    @Lazy(value = true)
    public CronTriggerFactoryBean getCronTriggerBean(String cron){
        CronTriggerFactoryBean bean = new CronTriggerFactoryBean();
        bean.setCronExpression(cron);
        bean.setJobDetail(getJobBean().getObject());
        bean.setGroup("daily-group");
        return bean;
    }
}

在我的Controller类中,我想按请求创建作业。我自动装配SchedulerFactoryBean以将石英触发器设置为bean。

@Controller
public class JobController {

    @Autowired
    SchedulerFactoryBean quartzScheduler;

    @Autowired
    ApplicationContext context;;

    @ResponseBody
    @RequestMapping("/job/create/daily")
    public String dailyJob(){
        CronTriggerImpl cron = (CronTriggerImpl) context.getBean("getCronTriggerBean","30 * * ? * MON-FRI");
        Trigger[] triggers = { cron };
        quartzScheduler.setTriggers(triggers);
        return "dailyJob";
    }
}

一切正常而不会产生错误,JobTrigger设置为quartzScheduler(在调试模式下查看)。但约伯从来没有跑过。我错过了什么?

不要忘记,有一个实现Job的类:

@Component
public class DailyJob implements Job{

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        System.out.println("Daily Job runs!");
    }
}

2 个答案:

答案 0 :(得分:1)

这对我有用:

@Controller
public class JobController {

    @Autowired
    private Scheduler scheduler;

    @Autowired
    private ApplicationContext context;

    @ResponseBody
    @RequestMapping("/job/create/daily")
    public String dailyJob() throws SchedulerException {
        JobDetail jobDetail = context.getBean(JobDetail.class);
        Trigger cronTrigger = context.getBean(Trigger.class, "30 * * ? * MON-FRI");

        scheduler.scheduleJob(jobDetail, cronTrigger);

        return "dailyJob";
    }
}

您必须使用Scheduler而不是SchedulerFactoryBean,因为后者的目的是创建将在作业上运行的实际调度程序。

此外,如果您使用scheduler.scheduleJob(cron),它还没有安排工作,因为它还没有在工作存储中,所以您需要使用它来创建工作' s细节并将cron表达与它联系起来。

答案 1 :(得分:0)

尝试添加" @ EnableScheduling"到您的配置