溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

SpringBoot2.6.3怎么集成quartz

發(fā)布時間:2022-02-18 13:42:46 來源:億速云 閱讀:140 作者:iii 欄目:開發(fā)技術

這篇文章主要介紹了SpringBoot2.6.3怎么集成quartz的相關知識,內容詳細易懂,操作簡單快捷,具有一定借鑒價值,相信大家閱讀完這篇SpringBoot2.6.3怎么集成quartz文章都會有所收獲,下面我們一起來看看吧。

quartz使用

quartz啟動需要數(shù)據(jù)庫有很多表的支持,這些表的建表腳本可以通過如下方式找到

如何找到quartz的數(shù)據(jù)庫腳本
在這里下載,需要注意的是下載2.2.3這個版本,不知道為什么高版本的反而沒有,真是佛了

SpringBoot2.6.3怎么集成quartz

集成Springboot

代碼
yml配置

spring:
  application:
    name: demo-excel
  datasource:
    url: jdbc:mysql://rm-xxx.mysql.rds.aliyuncs.com:3306/quartz_demo?zeroDateTimeBehavior=convertToNull
    password: quartz_demo
    username: quartz_demo
    driver-class-name: com.mysql.cj.jdbc.Driver
    name: datasource1
  quartz:
    # quartz任務存儲類型:jdbc或memory
    job-store-type: jdbc
    # 關閉時等待任務完成
    wait-for-jobs-to-complete-on-shutdown: true
    # 可以覆蓋已有的任務
    overwrite-existing-jobs: true
    properties:
      org:
        quartz:
          scheduler:
            # 調度器實例名稱
            instanceName: scheduler
            # 調度器實例ID自動生成
            instanceId: AUTO
          jobStore:
            class: org.springframework.scheduling.quartz.LocalDataSourceJobStore
            driverDelegateClass: org.quartz.impl.jdbcjobstore.StdJDBCDelegate
            # quartz相關表前綴
            tablePrefix: QRTZ_
            useProperties: false
          threadPool:
            class: org.quartz.simpl.SimpleThreadPool
            # 設置并發(fā)線程數(shù)量
            threadCount: 10
            # 指定線程優(yōu)先級
            threadPriority: 5
            threadsInheritContextClassLoaderOfInitializingThread: true
server:
  port: 8190
mybatis-plus:
  mapper-locations: classpath*:/mapperxml/*.xml

實現(xiàn)一個最簡單的任務,該任務輸出1111

@Component
public class TestJob extends QuartzJobBean {

    @Override
    protected void executeInternal(JobExecutionContext jobExecutionContext)
            throws JobExecutionException {
        // 任務的具體邏輯
        System.out.println(1111);
    }
}

配置這個任務的執(zhí)行計劃

@Configuration
public class QuartzConfig {

    @Bean
    public JobDetail jobDetail() {
        JobDetail jobDetail = JobBuilder.newJob(TestJob.class)
                .withIdentity("test", "test")
                .storeDurably()
                .build();
        return jobDetail;
    }
    public Trigger trigger() {
        Trigger trigger = TriggerBuilder.newTrigger()
                .forJob(jobDetail())
                .startNow()
                .withSchedule(CronScheduleBuilder.cronSchedule("* * * * * ?"))
        return trigger;
}

啟動任務會看到控制臺每秒鐘打印一次1111

進階

上訴任務是配置在代碼中,那么如果我們想把任務配置數(shù)據(jù)庫中,這樣我們就可以做一個定時任務的維護頁面,可以對定時任務的觸發(fā)規(guī)則修改,及修改刪除定時任務應該怎么做呢?

先定義一張存儲定時任務的表

-- auto-generated definition
create table sys_job
(
    id              bigint                  not null primary key,
    job_name        varchar(64)             not null comment '任務名稱',
    job_group       varchar(64)             not null comment '任務組名',
    method_name     varchar(500)            null comment '任務方法',
    method_params   varchar(50)             null comment '方法參數(shù)',
    cron_expression varchar(255)            null comment 'cron執(zhí)行表達式',
    misfire_policy  varchar(20) default '3' null comment '計劃執(zhí)行錯誤策略(1立即執(zhí)行 2執(zhí)行一次 3放棄執(zhí)行)',
    concurrent      char        default '1' null comment '是否并發(fā)執(zhí)行(0允許 1禁止)',
    status          char        default '0' null comment '狀態(tài)(0正常 1暫停)',
    create_by       varchar(64)             null comment '創(chuàng)建者',
    create_time     datetime                null comment '創(chuàng)建時間',
    update_by       varchar(64)             null comment '更新者',
    update_time     datetime                null comment '更新時間',
    remark          varchar(500)            null comment '備注信息'
)
    comment '定時任務調度表';

插入一條數(shù)據(jù)

INSERT INTO quartz_demo.sys_job (id, job_name, job_group, method_name, method_params, cron_expression, misfire_policy, concurrent, status, create_by, create_time, update_by, update_time, remark) VALUES (1, 'testJob2', 'test2', 'exec', null, '* * * * * ?', '2', '1', '0', null, null, null, null, null);

同時定義一張執(zhí)行結果記錄表

-- auto-generated definition
create table sys_job_log
(
    job_log_id     int auto_increment comment '任務日志ID'
        primary key,
    job_name       varchar(64)      not null comment '任務名稱',
    job_group      varchar(64)      not null comment '任務組名',
    method_name    varchar(500)     null comment '任務方法',
    method_params  varchar(50)      null comment '方法參數(shù)',
    job_message    varchar(500)     null comment '日志信息',
    status         char default '0' null comment '執(zhí)行狀態(tài)(0正常 1失?。?#39;,
    exception_info varchar(2000)    null comment '異常信息',
    create_time    datetime         null comment '創(chuàng)建時間'
)
    comment '定時任務調度日志表';

項目啟動時讀取這張表里的數(shù)據(jù)放到quartz中執(zhí)行
由于代碼太多了,這邊就不列出來代碼了,demo已經上傳到GitHub,項目基于springboot、mybatisplus。啟動加載任務的代碼在com.bxoon.service.impl.SysJobServiceImpl

關于“SpringBoot2.6.3怎么集成quartz”這篇文章的內容就介紹到這里,感謝各位的閱讀!相信大家對“SpringBoot2.6.3怎么集成quartz”知識都有一定的了解,大家如果還想學習更多知識,歡迎關注億速云行業(yè)資訊頻道。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經查實,將立刻刪除涉嫌侵權內容。

AI