springboot整合quartz多任务集成

1:添加依赖
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-quartz</artifactId>
</dependency>
2:TaskManager
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

@Component
public class TaskManager {
    /**
     * 存放所有定时任务
     */
    public static final List<ScheduledJob> JOB_LIST = new ArrayList<>();

    @Autowired
    SchedulerFactoryBean schedulerFactoryBean;

    /**
     * 执行所有定时任务
     */
    public void executeTasks(){

        if(!CollectionUtils.isEmpty(JOB_LIST)){
            for (ScheduledJob job : JOB_LIST) {
                try {
                    //跑起来
                    job.startJob(schedulerFactoryBean.getScheduler());
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }

    }

}

3:SchedulerListener

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;

@Configuration
public class SchedulerListener implements ApplicationListener<ContextRefreshedEvent> {

    @Autowired
    public TaskManager taskManager;

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        try {
            taskManager.executeTasks();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Bean
    public SchedulerFactoryBean schedulerFactoryBean() {
        SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean();
        return schedulerFactoryBean;

    }
}

4:ScheduledJob

import org.quartz.*;

import javax.annotation.PostConstruct;

public abstract class ScheduledJob implements Job {

    @PostConstruct
    public void init(){
        TaskManager.JOB_LIST.add(this);
    }

    /**
     * cron表达式在线生成 http://cron.qqe2.com/
     *
     * @return
     */
    public abstract String getCron();

    /**
     *
     * @param scheduler
     * 0/5 * * * * ?
     * @throws SchedulerException
     */
    public void startJob(Scheduler scheduler) throws SchedulerException{
        JobDetail jobDetail = JobBuilder.newJob(this.getClass())
                .withIdentity(this.getClass().getName() + "-job", "group1").build();
        CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(getCron());
        CronTrigger cronTrigger = getCronTrigger(scheduleBuilder);
        scheduler.scheduleJob(jobDetail,cronTrigger);
    }

    /**
     * 若要自定义触发器,请在子类重写此方法
     * @return
     */
    protected CronTrigger getCronTrigger(CronScheduleBuilder scheduleBuilder){
        return TriggerBuilder.newTrigger().withIdentity(this.getClass().getName() + "-trigger", "group1")
                .withSchedule(scheduleBuilder).build();

    }
}

5:具体job类 继承ScheduledJob,实现getCron()方法,execute()方法即可

job1

import com.xuchen.quartz.multitask.quartz.ScheduledJob;
import org.quartz.JobExecutionContext;
import org.springframework.stereotype.Component;

@Component
public class Job1 extends ScheduledJob {

    /**
     * 5秒执行一次
     * @return
     */
    @Override
    public String getCron() {
        return "0/5 * * * * ?";
    }

    @Override
    public void execute(JobExecutionContext jobExecutionContext){

        System.out.println("job1 is running ************************");

    }
}

job2

import com.xuchen.quartz.multitask.quartz.ScheduledJob;
import org.quartz.JobExecutionContext;
import org.springframework.stereotype.Component;

@Component
public class Job2 extends ScheduledJob {

    /**
     * 4秒执行一次
     * @return
     */
    @Override
    public String getCron() {
        return "0/4 * * * * ?";
    }

    @Override
    public void execute(JobExecutionContext jobExecutionContext){

        System.out.println("job2 is running ************************");

    }
}

效果演示

posted @ 2020-10-13 14:44  ch3ny  阅读(627)  评论(2)    收藏  举报