spring-boot-2.0.3之quartz集成,不是你想的那樣哦!

前言

  開心一刻html

    晚上回家,爸媽正在吵架,見我回來就都不說話了,看見我媽坐在那裏瞪着我爸,我就問老爸「你幹什麼了惹我媽生這麼大氣?」  我爸說「沒有什麼啊,卻是你,這麼大了尚未媳婦,要是你有媳婦給咱們生一個孫子玩,咱們致於吵架嗎?」我一聽就感受要壞,老爸你這是來了一招調虎離山啊,實力坑兒子啊,果真我媽改瞪我了,而後徹底不理我爸,直接指着我開罵了……java

  路漫漫其修遠兮,吾將上下而求索!mysql

  github:https://github.com/youzhibinggit

  碼雲(gitee):https://gitee.com/youzhibinggithub

java定時任務調度的實現方式 

  Timerweb

    這個相信你們都有用過,我也用過,但用的很少;spring

    特色是:簡單易用,但因爲全部任務都是由同一個線程來調度,所以全部任務都是串行執行的,同一時間只能有一個任務在執行,前一個任務的延遲或異常都將會影響到以後的任務;能實現簡單的定時任務,稍微複雜點(或要求高一些)的定時任務卻很差實現。
  ScheduledExecutorsql

    這個我相信你們也都用過,並且用的比Timer多;正是鑑於Timer的缺陷,Java 5推出了基於線程池設計的ScheduledExecutor;數據庫

    特色:每個被調度的任務都會由線程池中一個線程去執行,所以任務是併發執行的,相互之間不會受到干擾。須要注意的是,只有當任務的執行時間到來時,ScheduedExecutor 纔會真正啓動一個線程,其他時間 ScheduledExecutor 都是在輪詢任務的狀態。express

    雖然用ScheduledExecutor和Calendar可以實現複雜任務調度,但實現起來仍是比較麻煩,對開發仍是不夠友善。

  Spring Scheduler

    spring對任務調度的實現支持,能夠指定任務的執行時間,但對任務隊列和線程池的管控較弱;通常集成於項目中,小任務很方便。
  JCronTab

    JCronTab則是一款徹底按照crontab語法編寫的java任務調度工具。

    特色:

      可指定任務的執行時間;

      提供徹底按照Unix的UNIX-POSIX crontab的格式來規定時間;

      支持多種任務調度的持久化方法,包括普通文件、數據庫以及 XML 文件進行持久化;

      JCronTab內置了發郵件功能,能夠將任務執行結果方便地發送給須要被通知的人;

      設計和部署是高性能並可擴展。

  Quartz

    本文主角,請往下看
  固然還有XXL-JOB、Elastic-Job、Saturn等等

quartz相關概念

  Scheduler:調度器,進行任務調度;quartz的大腦
  Job:業務job,亦可稱業務組件;定時任務的具體執行業務須要實現此接口,調度器會調用此接口的execute方法完成咱們的定時業務
  JobDetail:用來定義業務Job的實例,咱們能夠稱之爲quartz job,不少時候咱們談到的job指的是JobDetail
  Trigger:觸發器,用來定義一個指定的Job什麼時候被執行
  JobBuilder:Job構建器,用來定義或建立JobDetail的實例;JobDetail限定了只能是Job的實例
  TriggerBuilder:觸發器構建器,用來定義或建立觸發器的實例

  具體爲何要分這麼細,你們能夠去查閱下相關資料,你會發現不少東西

工程實現

  pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.lee</groupId>
    <artifactId>spring-boot-quartz</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <java.version>1.8</java.version>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <druid.version>1.1.10</druid.version>
        <pagehelper.version>1.2.5</pagehelper.version>
        <druid.version>1.1.10</druid.version>
    </properties>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.3.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-quartz</artifactId>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>${druid.version}</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>${pagehelper.version}</version>
        </dependency>

        <!-- 日誌 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-logging</artifactId>
            <exclusions>            <!-- 排除spring-boot-starter-logging中的所有依賴 -->
                <exclusion>
                    <groupId>*</groupId>
                    <artifactId>*</artifactId>
                </exclusion>
            </exclusions>
            <scope>test</scope>     <!-- 打包的時候不打spring-boot-starter-logging.jar -->
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
    </dependencies>

    <build>
        <finalName>spring-boot-quartz</finalName>
        <plugins>
            <!-- 打包項目 mvn clean package -->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>
View Code

  application.xml

server:
  port: 9001
  servlet:
    context-path: /quartz
spring:
  thymeleaf:
    mode: HTML
    cache: false
  #鏈接池配置
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    druid:
      driver-class-name: com.mysql.jdbc.Driver
      url: jdbc:mysql://localhost:3306/spring-boot-quartz?useSSL=false&useUnicode=true
      username: root
      password: 123456
      initial-size: 1                     #鏈接池初始大小
      max-active: 20                      #鏈接池中最大的活躍鏈接數
      min-idle: 1                         #鏈接池中最小的活躍鏈接數
      max-wait: 60000                     #配置獲取鏈接等待超時的時間
      pool-prepared-statements: true    #打開PSCache,而且指定每一個鏈接上PSCache的大小
      max-pool-prepared-statement-per-connection-size: 20
      validation-query: SELECT 1 FROM DUAL
      validation-query-timeout: 30000
      test-on-borrow: false             #是否在得到鏈接後檢測其可用性
      test-on-return: false             #是否在鏈接放回鏈接池後檢測其可用性
      test-while-idle: true             #是否在鏈接空閒一段時間後檢測其可用性
  quartz:
    #相關屬性配置
    properties:
      org:
        quartz:
          scheduler:
            instanceName: quartzScheduler
            instanceId: AUTO
          jobStore:
            class: org.quartz.impl.jdbcjobstore.JobStoreTX
            driverDelegateClass: org.quartz.impl.jdbcjobstore.StdJDBCDelegate
            tablePrefix: QRTZ_
            isClustered: false
            clusterCheckinInterval: 10000
            useProperties: false
          threadPool:
            class: org.quartz.simpl.SimpleThreadPool
            threadCount: 10
            threadPriority: 5
            threadsInheritContextClassLoaderOfInitializingThread: true
    #數據庫方式
    job-store-type: JDBC
    #初始化表結構
    jdbc:
      initialize-schema: NEVER
#mybatis配置
mybatis:
  type-aliases-package: com.lee.quartz.entity
  mapper-locations: classpath:mybatis/mapper/*.xml
#分頁配置, pageHelper是物理分頁插件
pagehelper:
  #4.0.0之後版本能夠不設置該參數,該示例中是5.1.4
  helper-dialect: mysql
  #啓用合理化,若是pageNum<1會查詢第一頁,若是pageNum>pages會查詢最後一頁
  reasonable: true
logging:
  level:
    com.lee.quartz.mapper: debug
View Code

  這樣,quartz就配置好了,應用裏面直接用便可

  JobController.java

package com.lee.quartz.web;


import com.github.pagehelper.PageInfo;
import com.lee.quartz.common.Result;
import com.lee.quartz.entity.QuartzJob;
import com.lee.quartz.service.IJobService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/job")
public class JobController {
    private final static Logger LOGGER = LoggerFactory.getLogger(JobController.class);

    @Autowired
    private IJobService jobService;
    
    @SuppressWarnings({ "unchecked", "rawtypes" })
    @PostMapping("/add")
    public Result save(QuartzJob quartz){
        LOGGER.info("新增任務");
        Result result = jobService.saveJob(quartz);
        return result;
    }
    @PostMapping("/list")
    public PageInfo list(String jobName,Integer pageNo,Integer pageSize){
        LOGGER.info("任務列表");
        PageInfo pageInfo = jobService.listQuartzJob(jobName, pageNo, pageSize);
        return pageInfo;
    }

    @PostMapping("/trigger")
    public  Result trigger(String jobName, String jobGroup) {
        LOGGER.info("觸發任務");
        Result result = jobService.triggerJob(jobName, jobGroup);
        return result;
    }

    @PostMapping("/pause")
    public  Result pause(String jobName, String jobGroup) {
        LOGGER.info("中止任務");
        Result result = jobService.pauseJob(jobName, jobGroup);
        return result;
    }

    @PostMapping("/resume")
    public  Result resume(String jobName, String jobGroup) {
        LOGGER.info("恢復任務");
        Result result = jobService.resumeJob(jobName, jobGroup);
        return result;
    }

    @PostMapping("/remove")
    public  Result remove(String jobName, String jobGroup) {
        LOGGER.info("移除任務");
        Result result = jobService.removeJob(jobName, jobGroup);
        return result;
    }
}
View Code

  JobServiceImpl.java

package com.lee.quartz.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lee.quartz.common.Result;
import com.lee.quartz.entity.QuartzJob;
import com.lee.quartz.mapper.JobMapper;
import com.lee.quartz.service.IJobService;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class JobServiceImpl implements IJobService {

    @Autowired
    private Scheduler scheduler;

    @Autowired
    private JobMapper jobMapper;

    @Override
    public PageInfo listQuartzJob(String jobName, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<QuartzJob> jobList = jobMapper.listJob(jobName);
        PageInfo pageInfo = new PageInfo(jobList);
        return pageInfo;
    }

    @Override
    public Result saveJob(QuartzJob quartz){
        try {
            //若是是修改  展現舊的 任務
            if(quartz.getOldJobGroup() != null && !"".equals(quartz.getOldJobGroup())){
                JobKey key = new JobKey(quartz.getOldJobName(),quartz.getOldJobGroup());
                scheduler.deleteJob(key);
            }

            //構建job信息
            Class cls = Class.forName(quartz.getJobClassName()) ;
            cls.newInstance();
            JobDetail job = JobBuilder.newJob(cls).withIdentity(quartz.getJobName(),
                    quartz.getJobGroup())
                    .withDescription(quartz.getDescription()).build();
            // 觸發時間點
            CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule(quartz.getCronExpression().trim());
            Trigger trigger = TriggerBuilder.newTrigger().withIdentity("trigger"+quartz.getJobName(), quartz.getJobGroup())
                    .startNow().withSchedule(cronScheduleBuilder).build();
            //交由Scheduler安排觸發
            scheduler.scheduleJob(job, trigger);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error();
        }
        return Result.ok();
    }

    @Override
    public Result triggerJob(String jobName, String jobGroup) {
        JobKey key = new JobKey(jobName,jobGroup);
        try {
            scheduler.triggerJob(key);
        } catch (SchedulerException e) {
            e.printStackTrace();
            return Result.error();
        }
        return Result.ok();
    }

    @Override
    public Result pauseJob(String jobName, String jobGroup) {
        JobKey key = new JobKey(jobName,jobGroup);
        try {
            scheduler.pauseJob(key);
        } catch (SchedulerException e) {
            e.printStackTrace();
            return Result.error();
        }
        return Result.ok();
    }

    @Override
    public Result resumeJob(String jobName, String jobGroup) {
        JobKey key = new JobKey(jobName,jobGroup);
        try {
            scheduler.resumeJob(key);
        } catch (SchedulerException e) {
            e.printStackTrace();
            return Result.error();
        }
        return Result.ok();
    }

    @Override
    public Result removeJob(String jobName, String jobGroup) {
        try {
            TriggerKey triggerKey = TriggerKey.triggerKey(jobName, jobGroup);
            // 中止觸發器
            scheduler.pauseTrigger(triggerKey);
            // 移除觸發器
            scheduler.unscheduleJob(triggerKey);
            // 刪除任務
            scheduler.deleteJob(JobKey.jobKey(jobName, jobGroup));
            System.out.println("removeJob:"+JobKey.jobKey(jobName));
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error();
        }
        return Result.ok();
    }
}
View Code

  主要就是以上文件,詳情請查看spring-boot-quartz

  工程裏面數據源用的druid,springboot默認也會將該數據源應用到quartz,若是想給quartz單獨配置數據源,可配合@QuartzDataSource來實現(更多quarz數據源問題,請查看spring-boot-2.0.3之quartz集成,數據源問題,源碼探究

  最終效果以下

trigger狀態

  org.quartz.impl.jdbcjobstore.Constants中存放了一些列的常量,源代碼以下

/* 
 * All content copyright Terracotta, Inc., unless otherwise indicated. All rights reserved.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 
 * use this file except in compliance with the License. You may obtain a copy 
 * of the License at 
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0 
 *   
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 
 * License for the specific language governing permissions and limitations 
 * under the License.
 * 
 */

package org.quartz.impl.jdbcjobstore;

/**
 * <p>
 * This interface can be implemented by any <code>{@link
 * org.quartz.impl.jdbcjobstore.DriverDelegate}</code>
 * class that needs to use the constants contained herein.
 * </p>
 * 
 * @author <a href="mailto:jeff@binaryfeed.org">Jeffrey Wescott</a>
 * @author James House
 */
public interface Constants {

    /*
     * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     * 
     * Constants.
     * 
     * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     */

    // Table names
    String TABLE_JOB_DETAILS = "JOB_DETAILS";

    String TABLE_TRIGGERS = "TRIGGERS";

    String TABLE_SIMPLE_TRIGGERS = "SIMPLE_TRIGGERS";

    String TABLE_CRON_TRIGGERS = "CRON_TRIGGERS";

    String TABLE_BLOB_TRIGGERS = "BLOB_TRIGGERS";

    String TABLE_FIRED_TRIGGERS = "FIRED_TRIGGERS";

    String TABLE_CALENDARS = "CALENDARS";

    String TABLE_PAUSED_TRIGGERS = "PAUSED_TRIGGER_GRPS";

    String TABLE_LOCKS = "LOCKS";

    String TABLE_SCHEDULER_STATE = "SCHEDULER_STATE";

    // TABLE_JOB_DETAILS columns names
    
    String COL_SCHEDULER_NAME = "SCHED_NAME";
    
    String COL_JOB_NAME = "JOB_NAME";

    String COL_JOB_GROUP = "JOB_GROUP";

    String COL_IS_DURABLE = "IS_DURABLE";

    String COL_IS_VOLATILE = "IS_VOLATILE";

    String COL_IS_NONCONCURRENT = "IS_NONCONCURRENT";

    String COL_IS_UPDATE_DATA = "IS_UPDATE_DATA";

    String COL_REQUESTS_RECOVERY = "REQUESTS_RECOVERY";

    String COL_JOB_DATAMAP = "JOB_DATA";

    String COL_JOB_CLASS = "JOB_CLASS_NAME";

    String COL_DESCRIPTION = "DESCRIPTION";

    // TABLE_TRIGGERS columns names
    String COL_TRIGGER_NAME = "TRIGGER_NAME";

    String COL_TRIGGER_GROUP = "TRIGGER_GROUP";

    String COL_NEXT_FIRE_TIME = "NEXT_FIRE_TIME";

    String COL_PREV_FIRE_TIME = "PREV_FIRE_TIME";

    String COL_TRIGGER_STATE = "TRIGGER_STATE";

    String COL_TRIGGER_TYPE = "TRIGGER_TYPE";

    String COL_START_TIME = "START_TIME";

    String COL_END_TIME = "END_TIME";

    String COL_PRIORITY = "PRIORITY";

    String COL_MISFIRE_INSTRUCTION = "MISFIRE_INSTR";

    String ALIAS_COL_NEXT_FIRE_TIME = "ALIAS_NXT_FR_TM";

    // TABLE_SIMPLE_TRIGGERS columns names
    String COL_REPEAT_COUNT = "REPEAT_COUNT";

    String COL_REPEAT_INTERVAL = "REPEAT_INTERVAL";

    String COL_TIMES_TRIGGERED = "TIMES_TRIGGERED";

    // TABLE_CRON_TRIGGERS columns names
    String COL_CRON_EXPRESSION = "CRON_EXPRESSION";

    // TABLE_BLOB_TRIGGERS columns names
    String COL_BLOB = "BLOB_DATA";

    String COL_TIME_ZONE_ID = "TIME_ZONE_ID";

    // TABLE_FIRED_TRIGGERS columns names
    String COL_INSTANCE_NAME = "INSTANCE_NAME";

    String COL_FIRED_TIME = "FIRED_TIME";

    String COL_SCHED_TIME = "SCHED_TIME";
    
    String COL_ENTRY_ID = "ENTRY_ID";

    String COL_ENTRY_STATE = "STATE";

    // TABLE_CALENDARS columns names
    String COL_CALENDAR_NAME = "CALENDAR_NAME";

    String COL_CALENDAR = "CALENDAR";

    // TABLE_LOCKS columns names
    String COL_LOCK_NAME = "LOCK_NAME";

    // TABLE_LOCKS columns names
    String COL_LAST_CHECKIN_TIME = "LAST_CHECKIN_TIME";

    String COL_CHECKIN_INTERVAL = "CHECKIN_INTERVAL";

    // MISC CONSTANTS
    String DEFAULT_TABLE_PREFIX = "QRTZ_";

    // STATES
    String STATE_WAITING = "WAITING";

    String STATE_ACQUIRED = "ACQUIRED";

    String STATE_EXECUTING = "EXECUTING";

    String STATE_COMPLETE = "COMPLETE";

    String STATE_BLOCKED = "BLOCKED";

    String STATE_ERROR = "ERROR";

    String STATE_PAUSED = "PAUSED";

    String STATE_PAUSED_BLOCKED = "PAUSED_BLOCKED";

    String STATE_DELETED = "DELETED";

    /**
     * @deprecated Whether a trigger has misfired is no longer a state, but 
     * rather now identified dynamically by whether the trigger's next fire 
     * time is more than the misfire threshold time in the past.
     */
    String STATE_MISFIRED = "MISFIRED";

    String ALL_GROUPS_PAUSED = "_$_ALL_GROUPS_PAUSED_$_";

    // TRIGGER TYPES
    /** Simple Trigger type. */
    String TTYPE_SIMPLE = "SIMPLE";

    /** Cron Trigger type. */
    String TTYPE_CRON = "CRON";

    /** Calendar Interval Trigger type. */
    String TTYPE_CAL_INT = "CAL_INT";

    /** Daily Time Interval Trigger type. */
    String TTYPE_DAILY_TIME_INT = "DAILY_I";

    /** A general blob Trigger type. */
    String TTYPE_BLOB = "BLOB";
}

// EOF
View Code

  裏面有quartz的表名、各個表包含的列名、trigger狀態、trigger類型等內容

  狀態包括

    WAITING:等待中
    ACQUIRED:將觸發,此時還未到trigger真正的觸發時刻
    EXECUTING:觸發,亦可理解成執行中,trigger真正的觸發時刻
    COMPLETE:完成,再也不觸發
    BLOCKED:受阻,不容許併發執行job時會出現(@DisallowConcurrentExecution)
    ERROR:出錯
    PAUSED:暫停中
    PAUSED_BLOCKED:暫停受阻,不容許併發執行job時會出現(@DisallowConcurrentExecution)
    DELETED:已刪除
    MISFIRED:觸發失敗,已棄用,有另外的替代方式

  狀態變化流程圖以下所示

  trigger的初始狀態是WAITING,處於WAITING狀態的trigger等待被觸發。調度線程會不停地掃triggers表,根據NEXT_FIRE_TIME提早拉取即將觸發的trigger,若是這個trigger被該調度線程拉取到,它的狀態就會變爲ACQUIRED。由於是提早拉取trigger,並未到達trigger真正的觸發時刻,因此調度線程會等到真正觸發的時刻,再將trigger狀態由ACQUIRED改成EXECUTING。若是這個trigger再也不執行,就將狀態改成COMPLETE,不然爲WAITING,開始新的週期。若是這個週期中的任何環節拋出異常,trigger的狀態會變成ERROR。若是手動暫停這個trigger,狀態會變成PAUSED。

總結

  Quartz做爲一個開源的做業調度框架,提供了巨大的靈活性而不犧牲簡單性。咱們可以用它來爲執行一個做業而建立簡單的或複雜的調度。它有不少特徵,如:數據庫、集羣、插件、JavaMail支持,EJB做業預構建,支持cron-like表達式等等;

  springboot集成quartz很是簡單,最簡單的狀況下只須要引入依賴咱們就能夠享受quartz提供的功能,springboot默認會幫咱們配置好quartz;固然咱們也能夠自定義配置來實現quartz的定製;

參考

  幾種任務調度的Java實現方法與比較

  小柒2012 / spring-boot-quartz

  boot-features-quartz

  做業調度系統—Quartz

  記一次Quartz重複調度(任務重複執行)的問題排查

  Quartz FAQ

相關文章
相關標籤/搜索