SpringBoot+Quartz實(shí)現(xiàn)動(dòng)態(tài)定時(shí)任務(wù)
本文實(shí)例為大家分享了springBoot+Quartz實(shí)現(xiàn)動(dòng)態(tài)定時(shí)任務(wù)的具體代碼,供大家參考,具體內(nèi)容如下
目前常用的幾種任務(wù)調(diào)度
- Timer,簡(jiǎn)單無(wú)門檻,一般也沒人用。
- spring @Scheduled注解,一般集成于項(xiàng)目中,小任務(wù)很方便。
- 開源工具 Quartz,分布式集群開源工具,以下兩個(gè)分布式任務(wù)應(yīng)該都是基于Quartz實(shí)現(xiàn)的,可以說(shuō)是中小型公司必選,當(dāng)然也視自身需求而定。
- 分布式任務(wù) XXL-JOB,是一個(gè)輕量級(jí)分布式任務(wù)調(diào)度框架,支持通過 Web 頁(yè)面對(duì)任務(wù)進(jìn)行 CRUD 操作,支持動(dòng)態(tài)修改任務(wù)狀態(tài)、暫停/恢復(fù)任務(wù),以及終止運(yùn)行中任務(wù),支持在線配置調(diào)度任務(wù)入?yún)⒑驮诰€查看調(diào)度結(jié)果。
- 分布式任務(wù) Elastic-Job,是一個(gè)分布式調(diào)度解決方案,由兩個(gè)相互獨(dú)立的子項(xiàng)目 Elastic-Job-Lite 和 Elastic-Job-Cloud 組成。定位為輕量級(jí)無(wú)中心化解決方案,使用 jar 包的形式提供分布式任務(wù)的協(xié)調(diào)服務(wù)。支持分布式調(diào)度協(xié)調(diào)、彈性擴(kuò)容縮容、失效轉(zhuǎn)移、錯(cuò)過執(zhí)行作業(yè)重觸發(fā)、并行調(diào)度、自診。
- 分布式任務(wù) Saturn,Saturn是唯品會(huì)在github開源的一款分布式任務(wù)調(diào)度產(chǎn)品。它是基于當(dāng)當(dāng)elastic-job來(lái)開發(fā)的,其上完善了一些功能和添加了一些新的feature。目前在github上開源大半年,470個(gè)star。Saturn的任務(wù)可以用多種語(yǔ)言開發(fā)比如python、Go、Shell、Java、Php。其在唯品會(huì)內(nèi)部已經(jīng)發(fā)部署350+個(gè)節(jié)點(diǎn),每天任務(wù)調(diào)度4000多萬(wàn)次。同時(shí),管理和統(tǒng)計(jì)也是它的亮點(diǎn)。
SpringBoot項(xiàng)目的實(shí)現(xiàn)方法
1.config配置
import org.quartz.Scheduler;
import org.quartz.ee.servlet.QuartzInitializerListener;
import org.springframework.beans.factory.annotation.Autowired;
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;
?
/**
?* quartz配置
?*/
@Configuration
public class SchedulerConfig {
?? ?
? ? @Bean(name="SchedulerFactory")
? ? public SchedulerFactoryBean schedulerFactoryBean() throws IOException {
? ? ? ? SchedulerFactoryBean factory = new SchedulerFactoryBean();
? ? ? ? factory.setAutoStartup(true);
? ? ? ? //factory.setStartupDelay(5);//延時(shí)5秒啟動(dòng)
? ? ? ? return factory;
? ? }
?
? ? /*
? ? ?* quartz初始化監(jiān)聽器
? ? ?*/
? ? @Bean
? ? public QuartzInitializerListener executorListener() {
? ? ? ?return new QuartzInitializerListener();
? ? }
?
? ? /*
? ? ?* 通過SchedulerFactoryBean獲取Scheduler的實(shí)例
? ? ?*/
? ? @Bean(name="Scheduler")
? ? public Scheduler scheduler() throws IOException {
? ? ? ? return schedulerFactoryBean().getScheduler();
? ? }
?
}2.實(shí)現(xiàn)方法
/**
? ? ?* 新建任務(wù)
? ? ?* **/
? ? @ApiOperation(value = "Test-add", notes = "Test-add")
? ? @ApiImplicitParams({
? ? ? ? ? ? @ApiImplicitParam(name = "cruiseId", value = "巡航線id", required = true, paramType = "path"),
? ? ? ? ? ? @ApiImplicitParam(name = "jobName", value = "任務(wù)名稱", required = true, paramType = "query"),
? ? ? ? ? ? @ApiImplicitParam(name = "jobGroup", value = "任務(wù)分組", required = true, paramType = "query"),
? ? ? ? ? ? @ApiImplicitParam(name = "executeDate", value = "當(dāng)前時(shí)間[yyyy-MM-dd HH:mm:ss]", required = true, paramType = "query", dataType = "Date")})
? ? @PutMapping("/test/add/{cruiseId}")
? ? public ResultBody test(@PathVariable("cruiseId") String cruiseId,@RequestParam("jobName") String jobName,@RequestParam("jobGroup") String jobGroup,
? ? ? ? ? ? ? ? ? ? ? ? ? ?@RequestParam(value = "executeDate")@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date executeDate) {
? ? ? ? try {
? ? ? ? ? ? SimpleDateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
? ? ? ? ? ? //構(gòu)建job信息
? ? ? ? ? ? JobDetail job = JobBuilder.newJob(CruisePlanJob.class).withIdentity(jobName, jobGroup)
? ? ? ? ? ? ? ? ? ? ? ? ? ? .withDescription("巡航計(jì)劃").build();
? ? ? ? ? ? JobDataMap jobDataMap = job.getJobDataMap();
? ? ? ? ? ? jobDataMap.put("cruiseId", cruiseId);
? ? ? ? ? ? //CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule("cron的表達(dá)式");
? ? ? ? ? ? Trigger trigger = TriggerBuilder.newTrigger()
? ? ? ? ? ? ? ? ? ? .withIdentity("trigger" + jobName, jobGroup)
? ? ? ? ? ? ? ? ? ? .startAt(executeDate)
? ? ? ? ? ? ? ? ? ? .withSchedule(SimpleScheduleBuilder.simpleSchedule().withRepeatCount(0))
? ? ? ? ? ? ? ? ? ? .build();
? ? ? ? ? ? //SimpleScheduleBuilder.simpleSchedule().withRepeatCount(0).withIntervalInSeconds(20)//每隔多少秒執(zhí)行一次; withRepeatCount 設(shè)置重復(fù)的次數(shù)
? ? ? ? ? ? //.startNow().withSchedule(cronScheduleBuilder)
? ? ? ? ? ? //交由Scheduler安排觸發(fā)
? ? ? ? ? ? scheduler.scheduleJob(job, trigger);
? ? ? ? ? ? System.out.println("startJob:"+JobKey.jobKey(jobName));
? ? ? ? } catch (Exception e) {
? ? ? ? ? ? e.printStackTrace();
? ? ? ? ? ? return ResultBody.failed();
? ? ? ? }
? ? ? ? return ResultBody.ok();
? ? }
?
? ? /**
? ? ?* 刪除任務(wù)
? ? ?* **/
? ? @ApiOperation(value = "Test-remove", notes = "Test-remove")
? ? @ApiImplicitParams({
? ? ? ? ? ? @ApiImplicitParam(name = "jobName", value = "任務(wù)名稱", required = true, paramType = "query"),
? ? ? ? ? ? @ApiImplicitParam(name = "jobGroup", value = "任務(wù)分組", required = true, paramType = "query")})
? ? @GetMapping("/test/remove")
? ? public ResultBody remove(@RequestParam("jobName") String jobName,@RequestParam("jobGroup") String jobGroup) {
? ? ? ? try {
? ? ? ? ? ? TriggerKey triggerKey = TriggerKey.triggerKey(jobName, jobGroup);
? ? ? ? ? ? // 停止觸發(fā)器
? ? ? ? ? ? scheduler.pauseTrigger(triggerKey);
? ? ? ? ? ? // 移除觸發(fā)器
? ? ? ? ? ? scheduler.unscheduleJob(triggerKey);
? ? ? ? ? ? // 刪除任務(wù)
? ? ? ? ? ? boolean result = scheduler.deleteJob(JobKey.jobKey(jobName, jobGroup));
? ? ? ? ? ? System.out.println(result);
? ? ? ? ? ? System.out.println("removeJob:"+JobKey.jobKey(jobName));
? ? ? ? } catch (Exception e) {
? ? ? ? ? ? e.printStackTrace();
? ? ? ? ? ? return ResultBody.failed();
? ? ? ? }
? ? ? ? return ResultBody.ok();
? ? }3.CruisePlanJob類實(shí)現(xiàn)具體邏輯
@Slf4j
public class CruisePlanJob implements Job {
?
? @Override
? public void execute(JobExecutionContext context) {
? ? try{
? ? ? JobDataMap jdMap = context.getJobDetail().getJobDataMap();
? ? ? String cruiseId = (String) jdMap.get("cruiseId");
? ? ? String panId = (String) jdMap.get("panId");
? ? ? String userId = (String) jdMap.get("userId");
? ? ? String appId = (String) jdMap.get("appId");
? ? ? //TODO 邏輯待補(bǔ)充
?
? ? ? log.info("cruisePlan-CruisePlanJob-計(jì)劃執(zhí)行開始===>panId:{}---cruiseId:{}---appId:{}---userId:{}---resultBody:{}", panId, cruiseId, ?appId, ?userId, resultBody);
? ? }catch (Exception e){
? ? ? e.printStackTrace();
? ? }
? }
?
}4.另外兩種任務(wù)的方法
//恢復(fù)任務(wù) ? scheduler.resumeJob(key); //停止任務(wù) scheduler.pauseJob(key);
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- SpringBoot實(shí)現(xiàn)動(dòng)態(tài)定時(shí)任務(wù)
- SpringBoot 動(dòng)態(tài)定時(shí)器的使用方法
- 淺談SpringBoot集成Quartz動(dòng)態(tài)定時(shí)任務(wù)
- SpringBoot設(shè)置動(dòng)態(tài)定時(shí)任務(wù)的方法詳解
- SpringBoot實(shí)現(xiàn)固定和動(dòng)態(tài)定時(shí)任務(wù)的三種方法
- SpringBoot實(shí)現(xiàn)設(shè)置動(dòng)態(tài)定時(shí)任務(wù)的方法詳解
- SpringBoot創(chuàng)建動(dòng)態(tài)定時(shí)任務(wù)的幾種方式小結(jié)
- Springboot實(shí)現(xiàn)動(dòng)態(tài)定時(shí)任務(wù)流程詳解
- SpringBoot動(dòng)態(tài)定時(shí)任務(wù)實(shí)現(xiàn)完整版
- Springboot實(shí)現(xiàn)動(dòng)態(tài)定時(shí)任務(wù)管理的示例代碼
- 基于Nacos實(shí)現(xiàn)SpringBoot動(dòng)態(tài)定時(shí)任務(wù)調(diào)度
相關(guān)文章
MAC 系統(tǒng)如何使用 Sublime Text 2 直接編譯運(yùn)行 java 代碼
這篇文章主要介紹了MAC 系統(tǒng)如何使用 Sublime Text 2 直接編譯運(yùn)行 java 代碼,需要的朋友可以參考下2014-10-10
Springboot實(shí)現(xiàn)接口加密的示例代碼
Springboot實(shí)現(xiàn)一個(gè)接口加密 首先來(lái)看效果 這個(gè)主要是為了防止篡改請(qǐng)求的。 我們這里采用的是一個(gè)AOP的攔截,在有需要這樣的接口上添加了加密處理。 下面是一些功能 防篡改 HMAC-SHA252025-08-08
Java基礎(chǔ)教程之final關(guān)鍵字淺析
這篇文章主要給大家介紹了關(guān)于Java基礎(chǔ)教程之final關(guān)鍵字的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06
spring?IOC的理解之原理和實(shí)現(xiàn)過程
這篇文章主要介紹了spring?IOC的理解之原理和實(shí)現(xiàn)過程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2025-05-05
Spring Boot之內(nèi)嵌tomcat版本升級(jí)操作示例
這篇文章主要為大家介紹了Spring Boot之內(nèi)嵌tomcat版本升級(jí)操作示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06

