SpringBoot集成Quartz动态定时任务

Posted yvan1115

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringBoot集成Quartz动态定时任务相关的知识,希望对你有一定的参考价值。

SpringBoot自带schedule

沿用的springboot少xml配置的优良传统,本身支持表达式等多种定时任务
注意在程序启动的时候加上@EnableScheduling

@Scheduled(cron="0/5 * * * * ?")
    public void job()
        System.out.println("每五秒执行一次");
    

为什么要使用Quartz

多任务情况下,quartz更容易管理,可以实现动态配置
执行时间表达式:

表达式示例:

集成Quartz

如何搭建springboot就不再细说,官网和网上的教程很多。

maven依赖

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

测试Job类
SchedulerQuartzJob1和SchedulerQuartzJob2代码差不多就不一一列出

package com.yvan.quartz.job;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
/**
 * 实现Job接口
 * @author yvan
 *
 */
public class SchedulerQuartzJob1 implements Job
    private void before()
        System.out.println("任务开始执行");
    

    @Override
    public void execute(JobExecutionContext arg0) throws JobExecutionException 
        before();
        System.out.println("开始:"+System.currentTimeMillis());
        // TODO 业务
        System.out.println("结束:"+System.currentTimeMillis());
        after();
    

    private void after()
        System.out.println("任务开始执行");
    

任务调用处理
将其拆分成两个部分

  • 实例化Job,将任务触发器加入任务调度中
    private void startJob1(Scheduler scheduler) throws SchedulerException 
        // 通过JobBuilder构建JobDetail实例,JobDetail规定只能是实现Job接口的实例
        // JobDetail 是具体Job实例
        JobDetail jobDetail = JobBuilder.newJob(SchedulerQuartzJob1.class).withIdentity("job1", "group1").build();
        // 基于表达式构建触发器
        CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule("0/5 * * * * ?");
        // CronTrigger表达式触发器 继承于Trigger
        // TriggerBuilder 用于构建触发器实例
        CronTrigger cronTrigger = TriggerBuilder.newTrigger().withIdentity("job1", "group1")
                .withSchedule(cronScheduleBuilder).build();
        scheduler.scheduleJob(jobDetail, cronTrigger);
    

    private void startJob2(Scheduler scheduler) throws SchedulerException 
        JobDetail jobDetail = JobBuilder.newJob(SchedulerQuartzJob2.class).withIdentity("job2", "group2").build();
        CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule("0 0/5 * * * ?");
        CronTrigger cronTrigger = TriggerBuilder.newTrigger().withIdentity("job2", "group2")
                .withSchedule(cronScheduleBuilder).build();
        scheduler.scheduleJob(jobDetail, cronTrigger);
    
  • 对外提供关于动态管理任务的方法
    /**
     * 开始执行所有任务
     * 
     * @throws SchedulerException
     */
    public void startJob() throws SchedulerException 
        startJob1(scheduler);
        startJob2(scheduler);
        scheduler.start();
    

    /**
     * 获取Job信息
     * 
     * @param name
     * @param group
     * @return
     * @throws SchedulerException
     */
    public String getJobInfo(String name, String group) throws SchedulerException 
        TriggerKey triggerKey = new TriggerKey(name, group);
        CronTrigger cronTrigger = (CronTrigger) scheduler.getTrigger(triggerKey);
        return String.format("time:%s,state:%s", cronTrigger.getCronExpression(),
                scheduler.getTriggerState(triggerKey).name());
    

    /**
     * 修改某个任务的执行时间
     * 
     * @param name
     * @param group
     * @param time
     * @return
     * @throws SchedulerException
     */
    public boolean modifyJob(String name, String group, String time) throws SchedulerException 
        Date date = null;
        TriggerKey triggerKey = new TriggerKey(name, group);
        CronTrigger cronTrigger = (CronTrigger) scheduler.getTrigger(triggerKey);
        String oldTime = cronTrigger.getCronExpression();
        if (!oldTime.equalsIgnoreCase(time)) 
            CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule(time);
            CronTrigger trigger = TriggerBuilder.newTrigger().withIdentity(name, group)
                    .withSchedule(cronScheduleBuilder).build();
            date = scheduler.rescheduleJob(triggerKey, trigger);
        
        return date != null;
    

    /**
     * 暂停所有任务
     * 
     * @throws SchedulerException
     */
    public void pauseAllJob() throws SchedulerException 
        scheduler.pauseAll();
    

    /**
     * 暂停某个任务
     * 
     * @param name
     * @param group
     * @throws SchedulerException
     */
    public void pauseJob(String name, String group) throws SchedulerException 
        JobKey jobKey = new JobKey(name, group);
        JobDetail jobDetail = scheduler.getJobDetail(jobKey);
        if (jobDetail == null)
            return;
        scheduler.pauseJob(jobKey);
    

    /**
     * 恢复所有任务
     * 
     * @throws SchedulerException
     */
    public void resumeAllJob() throws SchedulerException 
        scheduler.resumeAll();
    

    /**
     * 恢复某个任务
     * 
     * @param name
     * @param group
     * @throws SchedulerException
     */
    public void resumeJob(String name, String group) throws SchedulerException 
        JobKey jobKey = new JobKey(name, group);
        JobDetail jobDetail = scheduler.getJobDetail(jobKey);
        if (jobDetail == null)
            return;
        scheduler.resumeJob(jobKey);
    

    /**
     * 删除某个任务
     * 
     * @param name
     * @param group
     * @throws SchedulerException
     */
    public void deleteJob(String name, String group) throws SchedulerException 
        JobKey jobKey = new JobKey(name, group);
        JobDetail jobDetail = scheduler.getJobDetail(jobKey);
        if (jobDetail == null)
            return;
        scheduler.deleteJob(jobKey);
    
  • 完整代码
package com.yvan.quartz;

import java.util.Date;

import org.quartz.CronScheduleBuilder;
import org.quartz.CronTrigger;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.TriggerBuilder;
import org.quartz.TriggerKey;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

import com.yvan.quartz.job.SchedulerQuartzJob1;
import com.yvan.quartz.job.SchedulerQuartzJob2;
/**
 * 任务调度处理
 * @author yvan
 *
 */
@Configuration
public class QuartzScheduler 
    // 任务调度
    @Autowired
    private Scheduler scheduler;

    /**
     * 开始执行所有任务
     * 
     * @throws SchedulerException
     */
    public void startJob() throws SchedulerException 
        startJob1(scheduler);
        startJob2(scheduler);
        scheduler.start();
    

    /**
     * 获取Job信息
     * 
     * @param name
     * @param group
     * @return
     * @throws SchedulerException
     */
    public String getJobInfo(String name, String group) throws SchedulerException 
        TriggerKey triggerKey = new TriggerKey(name, group);
        CronTrigger cronTrigger = (CronTrigger) scheduler.getTrigger(triggerKey);
        return String.format("time:%s,state:%s", cronTrigger.getCronExpression(),
                scheduler.getTriggerState(triggerKey).name());
    

    /**
     * 修改某个任务的执行时间
     * 
     * @param name
     * @param group
     * @param time
     * @return
     * @throws SchedulerException
     */
    public boolean modifyJob(String name, String group, String time) throws SchedulerException 
        Date date = null;
        TriggerKey triggerKey = new TriggerKey(name, group);
        CronTrigger cronTrigger = (CronTrigger) scheduler.getTrigger(triggerKey);
        String oldTime = cronTrigger.getCronExpression();
        if (!oldTime.equalsIgnoreCase(time)) 
            CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule(time);
            CronTrigger trigger = TriggerBuilder.newTrigger().withIdentity(name, group)
                    .withSchedule(cronScheduleBuilder).build();
            date = scheduler.rescheduleJob(triggerKey, trigger);
        
        return date != null;
    

    /**
     * 暂停所有任务
     * 
     * @throws SchedulerException
     */
    public void pauseAllJob() throws SchedulerException 
        scheduler.pauseAll();
    

    /**
     * 暂停某个任务
     * 
     * @param name
     * @param group
     * @throws SchedulerException
     */
    public void pauseJob(String name, String group) throws SchedulerException 
        JobKey jobKey = new JobKey(name, group);
        JobDetail jobDetail = scheduler.getJobDetail(jobKey);
        if (jobDetail == null)
            return;
        scheduler.pauseJob(jobKey);
    

    /**
     * 恢复所有任务
     * 
     * @throws SchedulerException
     */
    public void resumeAllJob() throws SchedulerException 
        scheduler.resumeAll();
    

    /**
     * 恢复某个任务
     * 
     * @param name
     * @param group
     * @throws SchedulerException
     */
    public void resumeJob(String name, String group) throws SchedulerException 
        JobKey jobKey = new JobKey(name, group);
        JobDetail jobDetail = scheduler.getJobDetail(jobKey);
        if (jobDetail == null)
            return;
        scheduler.resumeJob(jobKey);
    

    /**
     * 删除某个任务
     * 
     * @param name
     * @param group
     * @throws SchedulerException
     */
    public void deleteJob(String name, String group) throws SchedulerException 
        JobKey jobKey = new JobKey(name, group);
        JobDetail jobDetail = scheduler.getJobDetail(jobKey);
        if (jobDetail == null)
            return;
        scheduler.deleteJob(jobKey);
    

    private void startJob1(Scheduler scheduler) throws SchedulerException 
        // 通过JobBuilder构建JobDetail实例,JobDetail规定只能是实现Job接口的实例
        // JobDetail 是具体Job实例
        JobDetail jobDetail = JobBuilder.newJob(SchedulerQuartzJob1.class).withIdentity("job1", "group1").build();
        // 基于表达式构建触发器
        CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule("0/5 * * * * ?");
        // CronTrigger表达式触发器 继承于Trigger
        // TriggerBuilder 用于构建触发器实例
        CronTrigger cronTrigger = TriggerBuilder.newTrigger().withIdentity("job1", "group1")
                .withSchedule(cronScheduleBuilder).build();
        scheduler.scheduleJob(jobDetail, cronTrigger);
    

    private void startJob2(Scheduler scheduler) throws SchedulerException 
        JobDetail jobDetail = JobBuilder.newJob(SchedulerQuartzJob2.class).withIdentity("job2", "group2").build();
        CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule("0 0/5 * * * ?");
        CronTrigger cronTrigger = TriggerBuilder.newTrigger().withIdentity("job2", "group2")
                .withSchedule(cronScheduleBuilder).build();
        scheduler.scheduleJob(jobDetail, cronTrigger);
    

  • Scheduler 注入
    这里采用监听spring容器加载完毕后事件,启动任务调用
    将Scheduler交给spring初始化管理
package com.yvan.quartz;

import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.impl.StdSchedulerFactory;
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;

@Configuration
public class ApplicationStartQuartzJobListener implements ApplicationListener<ContextRefreshedEvent>
    @Autowired
    private QuartzScheduler quartzScheduler;

    /**
     * 初始启动quartz
     */
    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) 
        try 
            quartzScheduler.startJob();
            System.out.println("任务已经启动...");
         catch (SchedulerException e) 
            e.printStackTrace();
        
    

    /**
     * 初始注入scheduler
     * @return
     * @throws SchedulerException
     */
    @Bean
    public Scheduler scheduler() throws SchedulerException
        SchedulerFactory schedulerFactoryBean = new StdSchedulerFactory();
        return schedulerFactoryBean.getScheduler(); 
    

  • 封装API
    这里封装的API主要是为了方便动态管理执行任务,当然依赖这些接口也可以做一个界面来更直观的操作管理任务,这个就不再赘述。
package com.yvan.controller.quartzApi;

import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.yvan.quartz.QuartzScheduler;

/**
 * 这里并没有采用restful风格 只是简单封装了一下api
 * 
 * @author yvan
 *
 */
@RestController
@RequestMapping("/quartz")
public class QuartzApiController 
    @Autowired
    private QuartzScheduler quartzScheduler;

    @RequestMapping("/start")
    public void startQuartzJob() 
        try 
            quartzScheduler.startJob();
         catch (SchedulerException e) 
            e.printStackTrace();
        
    

    @RequestMapping("/info")
    public String getQuartzJob(String name, String group) 
        String info = null;
        try 
            info = quartzScheduler.getJobInfo(name, group);
         catch (SchedulerException e) 
            e.printStackTrace();
        
        return info;
    

    @RequestMapping("/modify")
    public boolean modifyQuartzJob(String name, String group, String time) 
        boolean flag = true;
        try 
            flag = quartzScheduler.modifyJob(name, group, time);
         catch (SchedulerException e) 
            e.printStackTrace();
        
        return flag;
    

    @RequestMapping(value = "/pause")
    public void pauseQuartzJob(String name, String group) 
        try 
            quartzScheduler.pauseJob(name, group);
         catch (SchedulerException e) 
            e.printStackTrace();
        
    

    @RequestMapping(value = "/pauseAll")
    public void pauseAllQuartzJob() 
        try 
            quartzScheduler.pauseAllJob();
         catch (SchedulerException e) 
            e.printStackTrace();
        
    

    @RequestMapping(value = "/delete")
    public void deleteJob(String name, String group) 
        try 
            quartzScheduler.deleteJob(name, group);
         catch (SchedulerException e) 
            e.printStackTrace();
        
    


  • 运行结果
 任务已经启动...
18-03-02 14:39:46,879  INFO TomcatEmbeddedServletContainer:201 - Tomcat started on port(s): 8080 (http)
  18-03-02 14:39:46,883  INFO Start:57 - Started Start in 3.18 seconds (JVM running for 3.47)
  任务开始执行
开始:1519972790003
结束:1519972790003
任务开始执行
任务开始执行
开始:1519972795000
结束:1519972795000
任务开始执行

以上是关于SpringBoot集成Quartz动态定时任务的主要内容,如果未能解决你的问题,请参考以下文章

SpringBoot集成Quartz动态定时任务

Spring Boot 入门:集成Quartz定时任务

SpringBoot系列:Spring Boot集成定时任务Quartz

SpringBoot系列:Spring Boot集成定时任务Quartz

SpringBoot整合Quartz实现动态的创建或删除定时任务并将定时调度任务持久化到MySQL以及Quartz集群配置

实践:SpringBoot实现定时任务的动态增删启停