SpringBoot2.6.3集成quartz的方式
quartz使用
quartz啟動(dòng)需要數(shù)據(jù)庫(kù)有很多表的支持,這些表的建表腳本可以通過(guò)如下方式找到
如何找到quartz的數(shù)據(jù)庫(kù)腳本
在這里下載,需要注意的是下載2.2.3這個(gè)版本,不知道為什么高版本的反而沒(méi)有,真是佛了
集成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任務(wù)存儲(chǔ)類(lèi)型:jdbc或memory job-store-type: jdbc # 關(guān)閉時(shí)等待任務(wù)完成 wait-for-jobs-to-complete-on-shutdown: true # 可以覆蓋已有的任務(wù) overwrite-existing-jobs: true properties: org: quartz: scheduler: # 調(diào)度器實(shí)例名稱(chēng) instanceName: scheduler # 調(diào)度器實(shí)例ID自動(dòng)生成 instanceId: AUTO jobStore: class: org.springframework.scheduling.quartz.LocalDataSourceJobStore driverDelegateClass: org.quartz.impl.jdbcjobstore.StdJDBCDelegate # quartz相關(guān)表前綴 tablePrefix: QRTZ_ useProperties: false threadPool: class: org.quartz.simpl.SimpleThreadPool # 設(shè)置并發(fā)線程數(shù)量 threadCount: 10 # 指定線程優(yōu)先級(jí) threadPriority: 5 threadsInheritContextClassLoaderOfInitializingThread: true server: port: 8190 mybatis-plus: mapper-locations: classpath*:/mapperxml/*.xml
實(shí)現(xiàn)一個(gè)最簡(jiǎn)單的任務(wù),該任務(wù)輸出1111
@Component public class TestJob extends QuartzJobBean { @Override protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException { // 任務(wù)的具體邏輯 System.out.println(1111); } }
配置這個(gè)任務(wù)的執(zhí)行計(jì)劃
@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; }
啟動(dòng)任務(wù)會(huì)看到控制臺(tái)每秒鐘打印一次1111
進(jìn)階
上訴任務(wù)是配置在代碼中,那么如果我們想把任務(wù)配置數(shù)據(jù)庫(kù)中,這樣我們就可以做一個(gè)定時(shí)任務(wù)的維護(hù)頁(yè)面,可以對(duì)定時(shí)任務(wù)的觸發(fā)規(guī)則修改,及修改刪除定時(shí)任務(wù)應(yīng)該怎么做呢?
先定義一張存儲(chǔ)定時(shí)任務(wù)的表
-- auto-generated definition create table sys_job ( id bigint not null primary key, job_name varchar(64) not null comment '任務(wù)名稱(chēng)', job_group varchar(64) not null comment '任務(wù)組名', method_name varchar(500) null comment '任務(wù)方法', method_params varchar(50) null comment '方法參數(shù)', cron_expression varchar(255) null comment 'cron執(zhí)行表達(dá)式', misfire_policy varchar(20) default '3' null comment '計(jì)劃執(zhí)行錯(cuò)誤策略(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)建時(shí)間', update_by varchar(64) null comment '更新者', update_time datetime null comment '更新時(shí)間', remark varchar(500) null comment '備注信息' ) comment '定時(shí)任務(wù)調(diào)度表';
插入一條數(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);
同時(shí)定義一張執(zhí)行結(jié)果記錄表
-- auto-generated definition create table sys_job_log ( job_log_id int auto_increment comment '任務(wù)日志ID' primary key, job_name varchar(64) not null comment '任務(wù)名稱(chēng)', job_group varchar(64) not null comment '任務(wù)組名', method_name varchar(500) null comment '任務(wù)方法', method_params varchar(50) null comment '方法參數(shù)', job_message varchar(500) null comment '日志信息', status char default '0' null comment '執(zhí)行狀態(tài)(0正常 1失?。?, exception_info varchar(2000) null comment '異常信息', create_time datetime null comment '創(chuàng)建時(shí)間' ) comment '定時(shí)任務(wù)調(diào)度日志表';
項(xiàng)目啟動(dòng)時(shí)讀取這張表里的數(shù)據(jù)放到quartz中執(zhí)行
由于代碼太多了,這邊就不列出來(lái)代碼了,demo已經(jīng)上傳到GitHub,項(xiàng)目基于springboot、mybatisplus。啟動(dòng)加載任務(wù)的代碼在com.bxoon.service.impl.SysJobServiceImpl
中
到此這篇關(guān)于SpringBoot2.6.3集成quartz的文章就介紹到這了,更多相關(guān)SpringBoot集成quartz內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 淺談SpringBoot集成Quartz動(dòng)態(tài)定時(shí)任務(wù)
- SpringBoot與Quartz集成實(shí)現(xiàn)分布式定時(shí)任務(wù)集群的代碼實(shí)例
- SpringBoot集成Quartz實(shí)現(xiàn)定時(shí)任務(wù)的方法
- Spring集成Quartz的簡(jiǎn)單配置的方法
- 詳解Quartz 與 Spring框架集成的三種方式
- Spring Boot集成Quartz注入Spring管理的類(lèi)的方法
- SpringBoot集成quartz實(shí)現(xiàn)定時(shí)任務(wù)詳解
- Quartz與Spring集成的兩種方法示例
- springBoot項(xiàng)目集成quartz開(kāi)發(fā)定時(shí)任務(wù)案例及注意事項(xiàng)
- 可視化定時(shí)任務(wù)quartz集成解析全過(guò)程
相關(guān)文章
@FeignClient注解中屬性contextId的使用說(shuō)明
這篇文章主要介紹了@FeignClient注解中屬性contextId的使用說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06Java?Spring框架創(chuàng)建項(xiàng)目與Bean的存儲(chǔ)與讀取詳解
本篇文章將介紹Spring項(xiàng)目的創(chuàng)建,IDEA國(guó)內(nèi)源的配置以及Bean的存儲(chǔ)與讀取,所謂的Bean其實(shí)就是對(duì)象的意思,更詳細(xì)地說(shuō)Spring Bean是被實(shí)例的,組裝的及被Spring 容器管理的Java對(duì)象2022-07-07Mac配置 maven以及環(huán)境變量設(shè)置方式
這篇文章主要介紹了Mac配置 maven以及環(huán)境變量設(shè)置方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08一個(gè)簡(jiǎn)陋的java圖書(shū)管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了一個(gè)簡(jiǎn)陋的java圖書(shū)管理系統(tǒng),簡(jiǎn)單的實(shí)現(xiàn)功能測(cè)試,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-07-07Mybatis中傳遞多個(gè)參數(shù)的4種方法總結(jié)
這篇文章主要給大家介紹了關(guān)于Mybatis中傳遞多個(gè)參數(shù)的4種方法,并且介紹了關(guān)于使用Mapper接口時(shí)參數(shù)傳遞方式,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2018-04-04java中StringBuffer的length()和capacity()方法對(duì)比
這篇文章主要介紹了java中StringBuffer的length()和capacity()方法對(duì)比,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07SpringBoot系列教程之dubbo和Zookeeper集成方法
這篇文章主要介紹了SpringBoot系列教程之dubbo和Zookeeper集成方法,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09