IT干货网

java之Spring-Boot Quartz 作业未运行

mate10pro 2024年09月03日 编程设计 20 0

我刚刚开始以动态方式创建 Quartz 计划作业。因此,有一个 QuartzConfig 类,我在其中创建了 SchedulerFactoryBeanJobDetailFactoryBeanCronTriggerFactoryBean。其中Job和CronTrigger bean是原型(prototype)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 类中,我想根据请求创建作业。我 Autowiring SchedulerFactoryBean 以将 quartz 触发器设置为 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(在 Debug模式下查看)。但作业从未运行。我想念什么?

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

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

请您参考如下方法:

这是对我有用的:

@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) 它不会安排作业,因为它尚未在作业存储中,因此您需要使用它的详细信息创建作业并将 cron 表达式与其关联起来。


评论关闭
IT干货网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!