Spring Boot搭建可配置Quartz2

第一步 从配置入手

@EnableScheduling
此注解为开放执行定时任务

@Autowired
private ApplicationContext applicationContext;

@Bean
public SchedulerFactoryBean getSchedulerFactoryBean() {
    SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean();
    AutowiringSpringBeanJobFactory jobFactory = new AutowiringSpringBeanJobFactory();
    jobFactory.setApplicationContext(applicationContext);
    schedulerFactoryBean.setJobFactory(jobFactory);
    return schedulerFactoryBean;
}
因为我是效率开发 很多我自己都是没有理解就把代码拿过来用了 这个我以后再补充吧,这个是需要配置在taskconfig中的


第二部分,自定义定时任务的核心配置类

@Autowired
private SchedulerFactoryBean schedulerFactoryBean;

// 定义动态任务
public void startAndExecuteJob(ScheduleJobEntity scheduleJob) throws SchedulerException {
    Scheduler scheduler = schedulerFactoryBean.getScheduler();
    ServerUpdateType type = scheduleJob.getUpdateType();
    if (type == ServerUpdateType.HAND) {
        // close task
        TriggerKey triggerKey = TriggerKey.triggerKey(scheduleJob.getId().toString(), scheduleJob.getJobName());
        scheduler.unscheduleJob(triggerKey);
    } else {
        // if express is null , them rollback;
        if (StringUtil.format(scheduleJob.getCronExpress()).equals("")) {
            return;
        }
        TriggerKey triggerKey = TriggerKey.triggerKey(scheduleJob.getId().toString(), scheduleJob.getJobName());
        CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey);

        // build new cron expression
        CronScheduleBuilder scheduleBuilder = null;
        if (type == ServerUpdateType.DAILY) {
            scheduleBuilder = CronScheduleBuilder.cronSchedule(StringUtil.format(scheduleJob.getCronExpress()));
        } else {
            //if the enum is not equals them rollback
            return;
        }

        // if no task running
        if (null == trigger) {
            JobDetail jobDetail = JobBuilder.newJob(JobService.class)
                    .withIdentity(scheduleJob.getId().toString(), scheduleJob.getJobName()).build();
            jobDetail.getJobDataMap().put("scheduleJob", scheduleJob);

            // build new trigger by new cron expression
            trigger = TriggerBuilder.newTrigger().withIdentity(scheduleJob.getId().toString(), scheduleJob.getJobName())
                    .withSchedule(scheduleBuilder).build();
            scheduler.scheduleJob(jobDetail, trigger);
        } else {
            // task is running
            // build new trigger by new cron expression
            trigger = trigger.getTriggerBuilder().withIdentity(triggerKey)
                    .withSchedule(scheduleBuilder).build();

            //reset task
            scheduler.rescheduleJob(triggerKey, trigger);
        }
    }
}
因为此类需要spring来管理 所以需要加 @Service注解

需要注意的是 config配置文件中 需要扫描到此类,以便于spring来管理@ComponentScan


第三部分

我们来定义具体需要执行的service

class JobService implements org.quartz.Job
实现execute方法
@Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        ScheduleJobEntity scheduleJob = (ScheduleJobEntity) jobExecutionContext.getMergedJobDataMap().get("scheduleJob");
        logger.info("-- start task " + scheduleJob.getJobName() + ", update type "
                + scheduleJob.getUpdateType().getValue() + " --");
        // 具体进行的任务
//        service.xxx();
    }

第四部分

entity的开发

由于我设计的是表之间的关联关系

任务表

核心字段 : id   jobname(任务名称)  cromExpress(定时的表达式) isOpen(开关)

其它表采用manyToOne进行关联


第五部分

引入所需jar包

<dependency>
    <groupId>org.quartz-scheduler</groupId>
    <artifactId>quartz</artifactId>
    <version>2.2.2</version>
</dependency>

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页