Quartz解决无法注入bean问题,以及选用自己定义构造器实例化

 

1、通过@Autowired注入所需要的bean

自定义JobFactory

@Component
public class JobFactory extends AdaptableJobFactory {

    private AutowireCapableBeanFactory capableBeanFactory;

    public QuartzJobFactory(AutowireCapableBeanFactory capableBeanFactory) {
        this.capableBeanFactory = capableBeanFactory;
    }

    @Override
    protected Object createJobInstance(TriggerFiredBundle triggerFiredBundle) throws Exception {
    //调用父类的方法
        Object jobInstance = super.createJobInstance(triggerFiredBundle);
        //进行注入,交给spring容器管理
        capableBeanFactory.autowireBean(jobInstance);
        
        return jobInstance;
    }
}
2、可以通过有参构造实例化job
自定义JobFactory
@Component
public class JobFactory extends AdaptableJobFactory {

    private AutowireCapableBeanFactory capableBeanFactory;

    public QuartzJobFactory(AutowireCapableBeanFactory capableBeanFactory) {
        this.capableBeanFactory = capableBeanFactory;
    }

    @Override
    protected Object createJobInstance(TriggerFiredBundle triggerFiredBundle) throws Exception {
        Class<?> jobClass = triggerFiredBundle.getJobDetail().getJobClass();

        //根据自己的构造器创建
        return capableBeanFactory.createBean(jobClass);
    }
}
以上俩中方式可以自行选择,然后将自定义的JobFactory设置给scheduler
@Configuration
public class QuartzConfig {

    @Autowired
    private JobFactory jobFactory;

    @Bean
    @SneakyThrows
    public Scheduler scheduler() {
        SchedulerFactory schedulerFactory = new StdSchedulerFactory();
        Scheduler scheduler = schedulerFactory.getScheduler();
        // 自定义 JobFactory 使得在 Quartz Job 中可以使用 @Autowired/有参构造
        scheduler.setJobFactory(jobFactory);
        scheduler.start();
        return scheduler;
    }
}
ScheduleManager工具类,可以开启一个job,删除一个job
@Component
public class ScheduleManager {

    private Scheduler scheduler;

    public ScheduleManager(Scheduler scheduler) {
        this.scheduler = scheduler;
    }

    public void start(JobParamter jobParamter, Class<? extends Job> jobClass) {

        //创建任务
        JobDetail jobDetail = JobBuilder
                .newJob(jobClass)
                .withIdentity(jobParamter.getJobName())
                .build();
        //创建触发器
        Trigger trigger = TriggerBuilder.newTrigger()
                .withIdentity(jobParamter.getJobName())
                .withSchedule(CronScheduleBuilder.cronSchedule(jobParamter.getJobCron()))
                .build();
        try {
            //将任务及其触发器放入调度器
            scheduler.scheduleJob(jobDetail, trigger);
            //调度器开始调度任务
            if (!scheduler.isStarted()) {
                scheduler.start();
            }

            log.info("启动任务:{}", jobParamter.getJobName());
        } catch (SchedulerException e) {
            log.error("任务:{}启动失败", jobParamter.getJobName(), e);
        }

    }


    /**
     * 停止任务--再启动需要从新初始化
     *
     * @param jobParamter
     */
    public void stop(JobParamter jobParamter) {
        try {
            // 停止触发器
            scheduler.pauseTrigger(new TriggerKey(jobParamter.getJobName()));
            scheduler.unscheduleJob(new TriggerKey(jobParamter.getJobName()));
            // 删除任务
            scheduler.deleteJob(new JobKey(jobParamter.getJobName()));
            log.info("停止任务:{}", jobParamter.getJobName());
        } catch (SchedulerException e) {
            log.error("任务:{}停止失败", jobParamter.getJobName(), e);
        }
    }
}

job实现类

public class ExampleJob implements Job {
    private RestTemplateWrapper restTemplateWrapper;
    


    public HeartBeatJob(RestTemplateWrapper restTemplateWrapper) {
        this.restTemplateWrapper = restTemplateWrapper;
        
    }

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
       //具体业务
    }
}

 

上一篇:为什么在 React 16 版本中 render 阶段放弃了使用递归?


下一篇:quartz的实现流程