简体   繁体   English

Spring-Boot Quartz Job没有运行

[英]Spring-Boot Quartz Job is not running

I just started to create Quartz Scheduled Jobs in a dynamic way. 我刚刚开始以动态方式创建Quartz Scheduled Jobs。 So there is a QuartzConfig class where I create a SchedulerFactoryBean , JobDetailFactoryBean and CronTriggerFactoryBean . 因此,在QuartzConfig类中,我创建了SchedulerFactoryBeanJobDetailFactoryBeanCronTriggerFactoryBean Wherein the Job and CronTrigger beans are prototype ones. 其中Job和CronTrigger 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;
    }
}

In my Controller class I want to create the jobs by request. 在我的Controller类中,我想通过请求创建作业。 I autowire SchedulerFactoryBean to set the quartz trigger to the bean. 我自动连接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";
    }
}

Everything works without creating an error and the Job and the Trigger are set to the quartzScheduler (see it in debug mode). 一切正常,而不会产生错误,并且将JobTrigger设置为quartzScheduler (在调试模式下查看)。 But the Job is never running. 但是工作从未运行过。 What do I miss? 我想念什么?

And dont to forget, there is a class which implements Job : 并且不要忘记,有一个实现Job的类:

@Component
public class DailyJob implements Job{

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

Here is what worked for me: 这对我有用:

@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";
    }
}

You have to use Scheduler instead of SchedulerFactoryBean as the purpose of the latter is to create the actual scheduler that will operate on jobs. 您必须使用Scheduler而不是SchedulerFactoryBean因为后者的目的是创建将对作业进行操作的实际调度程序。

Also if you use scheduler.scheduleJob(cron) it woun't schedule the job because it isn't in the job store yet, so you'll need to create a job using it's details and associate cron expression with it. 另外,如果您使用scheduler.scheduleJob(cron)则它不会安排作业,因为它不在作业存储中,因此您需要使用作业的详细信息创建作业并将cron表达式与该作业关联。

尝试将“ @EnableScheduling”添加到您的配置中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM