天天看點

[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=
# 優先級
org.quartz.threadPool.threadPriority=
org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread=true
org.quartz.jobStore.misfireThreshold=
# 預設存儲在記憶體中
#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=
           

持久化資料庫建表

從官網下載下傳對應版本的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 = , message = "類(名)不能為空")
    @NotNull(message = "類(名)不能為空")
    private String className;
    @Size(min = , message = "任務(名)不能為空")
    @NotNull(message = "任務(名)不能為空")
    private String jobName;
    @Size(min = , message = "任務(名)不能為空")
    @NotNull(message = "任務(名)不能為空")
    private String jobGroup;
    @Size(min = , message = "觸發器(名)不能為空")
    @NotNull(message = "觸發器(名)不能為空")
    private String tgName;
    @Size(min = , message = "觸發器(組)不能為空")
    @NotNull(message = "觸發器(組)不能為空")
    private String tgGroup;
    @Size(min = , 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
           
[springboot]內建org.quartz-scheduler進行任務排程
使用postman測試
stop
           
[springboot]內建org.quartz-scheduler進行任務排程

console

[springboot]內建org.quartz-scheduler進行任務排程

start

[springboot]內建org.quartz-scheduler進行任務排程

console

[springboot]內建org.quartz-scheduler進行任務排程

list

[springboot]內建org.quartz-scheduler進行任務排程

startjob

[springboot]內建org.quartz-scheduler進行任務排程

console

[springboot]內建org.quartz-scheduler進行任務排程

檢查持久化

前面步驟,早就已經建立好的資料庫表
           
[springboot]內建org.quartz-scheduler進行任務排程
檢視
           
[springboot]內建org.quartz-scheduler進行任務排程
[springboot]內建org.quartz-scheduler進行任務排程
持久化成功(第一條别糾結,那是我以前用的,第二條資料才是我們測的)
           

簡單快速上手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)