[springboot]集成org.quartz-scheduler进行任务调度

前言

springboot内置有org.springframework.scheduling.annotation.Scheduled可以让我们进行简单快速的任务调度(例如定时执行的任务),当我们一些和任务调度有关的业务开始复杂的时候,极其需要非常灵活的任务调度策略;在这种情况,博主使用了quartz,写下此文以记;

依赖(gradle)

    compile group: 'org.quartz-scheduler', name: 'quartz', version: '2.2.1'

quartz的配置文件

springboot中集成quartz并进行持久化配置,在springboot项目中的resources目录下创建properties文件(和springboot的配置文件同级),命名quartz.properties;

内容如下:

# 固定前缀org.quartz
# 主要分为scheduler、threadPool、jobStore、plugin等部分
#
#实例名
org.quartz.scheduler.instanceName=TioadScheduler
#实例id(唯一,有缺省值)
org.quartz.scheduler.instanceId=TioadSchedulerId
org.quartz.scheduler.rmi.export=false
org.quartz.scheduler.rmi.proxy=false
org.quartz.scheduler.wrapJobExecutionInUserTransaction=false
# 实例化ThreadPool时,使用的线程类为SimpleThreadPool
org.quartz.threadPool.class=org.quartz.simpl.SimpleThreadPool
# threadCount和threadPriority将以setter的形式注入ThreadPool实例
# 并发个数
org.quartz.threadPool.threadCount=5
# 优先级
org.quartz.threadPool.threadPriority=5
org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread=true
org.quartz.jobStore.misfireThreshold=5000
# 默认存储在内存中
#org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore
#持久化
org.quartz.jobStore.class=org.quartz.impl.jdbcjobstore.JobStoreTX
org.quartz.jobStore.tablePrefix=QRTZ_
org.quartz.jobStore.dataSource=qzDS
org.quartz.dataSource.qzDS.driver=com.mysql.jdbc.Driver
#dev(第一次初始化quartz,需要执行quartz包下的sql,创建表结构)
org.quartz.dataSource.qzDS.URL=持久化的数据库url配置
org.quartz.dataSource.qzDS.user=持久化的数据库连接用户账号配置
org.quartz.dataSource.qzDS.password=持久化的数据库连接用户密码配置
org.quartz.dataSource.qzDS.maxConnections=10

持久化数据库建表

官网下载对应版本的quartz,解压,目录下的docs/dbTables会有大多数数据库的建表语句,执行建表;

基于注解的quartz配置

import org.quartz.Scheduler;
import org.quartz.ee.servlet.QuartzInitializerListener;
import org.springframework.beans.factory.config.PropertiesFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;

import java.io.IOException;
import java.util.Properties;

@Configuration
public class SchedulerConfig {
@Autowired
TioadJobFactory tioadJobFactory;//注入我们自己的factory,防止无法在job中注入service层

    @Bean(name="TioadSchedulerFactory")
    public SchedulerFactoryBean tioadSchedulerFactoryBean() throws IOException {
        SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean();
        schedulerFactoryBean.setQuartzProperties(quartzProperties());
//        schedulerFactoryBean.setJobFactory(tioadJobFactory);
        return schedulerFactoryBean;
    }

    @Bean
    public Properties quartzProperties() throws IOException {
        PropertiesFactoryBean propertiesFactoryBean = new PropertiesFactoryBean();
        propertiesFactoryBean.setLocation(new ClassPathResource("/quartz.properties"));
        //在quartz.properties中的属性被读取并注入后再初始化对象
        propertiesFactoryBean.afterPropertiesSet();
        return propertiesFactoryBean.getObject();
    }

    /*
     * quartz初始化监听器
     */
    @Bean
    public QuartzInitializerListener executorListener() {
        return new QuartzInitializerListener();
    }

    /*
     * 通过SchedulerFactoryBean获取Scheduler的实例
     */
    @Bean(name="TioadScheduler")
    public Scheduler scheduler() throws IOException {
        return tioadSchedulerFactoryBean().getScheduler();
    }

新建测试任务Job1

实现quartz的job接口,重写execute(),这里就是任务的逻辑,在execute()中执行的任务逻辑如果用到service层,发现注入不成功的话,需要在上面的SchedulerConfig.class中set我们自己的;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;

@Component
public class Job1 implements Job {

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd HH:mm:ss");
        System.out.println("job 1 " + sdf.format(new Date()));
    }
}

我们自己的工厂防止无法注入问题TioadJobFactory

@Component
public class TioadJobFactory extends AdaptableJobFactory {

    @Autowired
    private AutowireCapableBeanFactory capableBeanFactory;

    @Override
    protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception {
        Object jobInstance = super.createJobInstance(bundle);
        capableBeanFactory.autowireBean(jobInstance); //这一步解决不能spring注入bean的问题
        return jobInstance;
    }

}

controller级测试使用quartz

import com.crm.restapi.param.QuartzParam;
import com.crm.restapi.result.ApiResult;
import org.quartz.*;
import org.quartz.impl.matchers.GroupMatcher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.Set;

import static org.quartz.CronScheduleBuilder.cronSchedule;
import static org.quartz.JobBuilder.newJob;
import static org.quartz.TriggerBuilder.newTrigger;

@Validated
@RequestMapping(value = "/task")
@RestController
public class TaskController extends BaseController{
    @Autowired @Qualifier("TioadScheduler")//这里是config里面定义的scheduler,通过添加注解@Qualifier,使@Autowired的自动注入由bytype变为byname
    private Scheduler scheduler;
    @Value("${quartzname}")
    private String quartzname;//这是我自己定义的quartzname,这里是字符串:IamROOT
    @Value("${quartzpath}")
    private String quartzpath;//这是配置好的反射path,这里是:com.crm.restapi.schedule.job.

    @PostMapping(value = "/startjob")
    public ApiResult startjob(@RequestBody @Valid QuartzParam quartzParam) throws ClassNotFoundException, SchedulerException {
        // 定义jobdetail
        Class jobclass = Class.forName(quartzpath + quartzParam.getClassName());
        JobDetail job = newJob(jobclass)
                .withIdentity(quartzParam.getJobName(), quartzParam.getJobGroup())
                .build();
        // 定义trigger触发器
        Trigger trigger = newTrigger()
                .withIdentity(quartzParam.getTgName(), quartzParam.getTgGroup())
                .startNow()
                .withSchedule(cronSchedule(quartzParam.getTrigger()))//这里是cron表达式
                .build();
        // Tell quartz to schedule the job using our trigger
        scheduler.scheduleJob(job, trigger);//定义的JobDetail和trigger注册到scheduler里
        return new ApiResult().success();
    }

    @GetMapping("/start")
    public ApiResult startTask() throws ClassNotFoundException, SchedulerException {
        scheduler.start();
        return new ApiResult().success();
    }

    @GetMapping("/stop")
    public ApiResult stopTrigger() throws ClassNotFoundException, SchedulerException {
        scheduler.standby();//这里使用standby()方法,shutdown()方法会把scheduler实例关闭,start也会无法启动
        return new ApiResult().success();
    }

    @GetMapping("/list")
    public ApiResult list() throws ClassNotFoundException, SchedulerException {
        HashMap map = new HashMap();
        Set<JobKey> jobKeySet = scheduler.getJobKeys(GroupMatcher.groupEquals("group1"));
        map.put("jobNum", jobKeySet.size());
        map.put("jobDedail", jobKeySet);
        map.put("nowJob", scheduler.getCurrentlyExecutingJobs());
        return new ApiResult().success(map);
    }

}

QuartzParam.java(这里我用了validation验证)

import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

public class QuartzParam {
    @Size(min = 1, message = "类(名)不能为空")
    @NotNull(message = "类(名)不能为空")
    private String className;
    @Size(min = 1, message = "任务(名)不能为空")
    @NotNull(message = "任务(名)不能为空")
    private String jobName;
    @Size(min = 1, message = "任务(名)不能为空")
    @NotNull(message = "任务(名)不能为空")
    private String jobGroup;
    @Size(min = 1, message = "触发器(名)不能为空")
    @NotNull(message = "触发器(名)不能为空")
    private String tgName;
    @Size(min = 1, message = "触发器(组)不能为空")
    @NotNull(message = "触发器(组)不能为空")
    private String tgGroup;
    @Size(min = 1, message = "触发器(规则)不能为空")
    @NotNull(message = "触发器(规则)不能为空")
    private String trigger;

    public String getClassName() {
        return className;
    }

    public void setClassName(String className) {
        this.className = className;
    }

    public String getJobName() {
        return jobName;
    }

    public void setJobName(String jobName) {
        this.jobName = jobName;
    }

    public String getJobGroup() {
        return jobGroup;
    }

    public void setJobGroup(String jobGroup) {
        this.jobGroup = jobGroup;
    }

    public String getTgName() {
        return tgName;
    }

    public void setTgName(String tgName) {
        this.tgName = tgName;
    }

    public String getTgGroup() {
        return tgGroup;
    }

    public void setTgGroup(String tgGroup) {
        this.tgGroup = tgGroup;
    }

    public String getTrigger() {
        return trigger;
    }

    public void setTrigger(String trigger) {
        this.trigger = trigger;
    }
}

Run起来

springboot启动后scheduler实例会自动start

这里写图片描述

使用postman测试
stop

这里写图片描述
console
这里写图片描述
start
这里写图片描述
console
这里写图片描述
list
这里写图片描述
startjob
这里写图片描述
console
这里写图片描述

检查持久化

前面步骤,早就已经创建好的数据库表

这里写图片描述

    查看

这里写图片描述
这里写图片描述

    持久化成功(第一条别纠结,那是我以前用的,第二条数据才是我们测的)

简单快速上手springboot集成quartz的快速教程,简单粗暴的还是需要官方文档;
有问题,欢迎留言;

demo源码

附上官方文档
org.quartz-scheduler官方文档

quartz学习
Quartz学习——Quartz大致介绍(一)

常见问题
quartz Couldn’t rollback jdbc connection. Communications link failure during rollback().

java.net.SocketException: Broken pipe with Quartz and MySQL and Tomcat (Tomcat Crash)

优秀的个人博客,低调大师

微信关注我们

原文链接:https://yq.aliyun.com/articles/576470

转载内容版权归作者及来源网站所有!

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

相关文章

发表评论

资源下载

更多资源
Mario,低调大师唯一一个Java游戏作品

Mario,低调大师唯一一个Java游戏作品

马里奥是站在游戏界顶峰的超人气多面角色。马里奥靠吃蘑菇成长,特征是大鼻子、头戴帽子、身穿背带裤,还留着胡子。与他的双胞胎兄弟路易基一起,长年担任任天堂的招牌角色。

Oracle Database,又名Oracle RDBMS

Oracle Database,又名Oracle RDBMS

Oracle Database,又名Oracle RDBMS,或简称Oracle。是甲骨文公司的一款关系数据库管理系统。它是在数据库领域一直处于领先地位的产品。可以说Oracle数据库系统是目前世界上流行的关系数据库管理系统,系统可移植性好、使用方便、功能强,适用于各类大、中、小、微机环境。它是一种高效率、可靠性好的、适应高吞吐量的数据库方案。

Eclipse(集成开发环境)

Eclipse(集成开发环境)

Eclipse 是一个开放源代码的、基于Java的可扩展开发平台。就其本身而言,它只是一个框架和一组服务,用于通过插件组件构建开发环境。幸运的是,Eclipse 附带了一个标准的插件集,包括Java开发工具(Java Development Kit,JDK)。

Sublime Text 一个代码编辑器

Sublime Text 一个代码编辑器

Sublime Text具有漂亮的用户界面和强大的功能,例如代码缩略图,Python的插件,代码段等。还可自定义键绑定,菜单和工具栏。Sublime Text 的主要功能包括:拼写检查,书签,完整的 Python API , Goto 功能,即时项目切换,多选择,多窗口等等。Sublime Text 是一个跨平台的编辑器,同时支持Windows、Linux、Mac OS X等操作系统。