基于Springboot執(zhí)行多個(gè)定時(shí)任務(wù)并動(dòng)態(tài)獲取定時(shí)任務(wù)信息
簡介
因?yàn)橐恍I(yè)務(wù)的需要所有需要使用多個(gè)不同的定時(shí)任務(wù),并且每個(gè)定時(shí)任務(wù)中的定時(shí)信息是通過數(shù)據(jù)庫動(dòng)態(tài)獲取的。下面是我寫的使用了Springboot+Mybatis寫的多任務(wù)定時(shí)器。
主要實(shí)現(xiàn)了以下功能:
1、同時(shí)使用多個(gè)定時(shí)任務(wù)
2、動(dòng)態(tài)獲取定時(shí)任務(wù)的定時(shí)信息
說明
因?yàn)槲覀冃枰獜臄?shù)據(jù)庫動(dòng)態(tài)的獲取定時(shí)任務(wù)的信息,所以我們需要集成 SchedulingConfigurer 然后重寫 configureTasks 方法即可,調(diào)用不同的定時(shí)任務(wù)只需要通過service方法調(diào)用不用的實(shí)現(xiàn)返回對(duì)應(yīng)的定時(shí)任務(wù)信息。有多少個(gè)定時(shí)任務(wù)就重寫多少個(gè)該方法(最好創(chuàng)建不同的類)。然后直接在application中啟動(dòng)即可。
SpringApplication-啟動(dòng)類
package test;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@SpringBootApplication
@EnableTransactionManagement
@EnableScheduling
@ComponentScan(value = {"test.*"})
@MapperScan("test.mapper.*")
public class TomcatlogApplication {
public static void main(String[] args) {
SpringApplication.run(TomcatlogApplication.class, args);
}
}
動(dòng)態(tài)獲取定時(shí)任務(wù)信息
mapper
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import java.util.List;
/*
* @version 1.0 created by liuxuewen on 2018/8/21 14:39
*/
public interface TomcatlogMapper {
@Select("SELECT * FROM scheduledtask s WHERE s.`enable` = 1")
String queryScheduledTask();
}
service
package test.service;
import java.util.ArrayList;
import java.util.List;
/*
* @version 1.0 created by liuxuewen on 2018/8/21 14:44
*/
public interface TomcatlogService {
List<ScheduledtaskEntity> queryScheduledTask();
}
service impl
import test.mapper.tomcatlog.TomcatlogMapper;
import test.service.TomcatlogService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/*
* @version 1.0 created by liuxuewen on 2018/8/21 14:44
*/
@Service
public class TomcatlogServiceImpl implements TomcatlogService {
private static final Logger LOGGER = LoggerFactory.getLogger(TomcatlogServiceImpl.class);
@Autowired
TomcatlogMapper tomcatlogMapper;
@Override
public String queryScheduledTask() {
try {
String res = tomcatlogMapper.queryScheduledTask();
return res;
} catch (Exception e) {
LOGGER.info(e);
}
return null;
}
定時(shí)任務(wù)
package test.schedultask;
import test.controller.MainController;
import test.service.ControllerService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
/*
* @version 1.0 created by liuxuewen on 2018/8/27 9:25
*/
@Component
public class ElasticsearchSchedultaskController implements SchedulingConfigurer {
private static final Logger LOGGER = LoggerFactory.getLogger(ElasticsearchSchedultaskController.class);
@Autowired
private ControllerService controllerService;
@Autowired
private MainController mainController;
@Override
public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) {
try {
scheduledTaskRegistrar.addTriggerTask(
//1.添加任務(wù)內(nèi)容(Runnable),可以為方法
() -> Sytem.out.println("定時(shí)任務(wù)1"),
//2.設(shè)置執(zhí)行周期(Trigger)
triggerContext -> {
//2.1 從數(shù)據(jù)庫獲取執(zhí)行周期,在這里調(diào)用不同的方法返回不同的定時(shí)任務(wù)信息
String cron = controllerService.getSchedultaskForElasticsearch();
System.out.println("controllerService.getSchedultaskForElasticsearch()");
System.out.println(cron);
//2.2 合法性校驗(yàn).
if (StringUtils.isEmpty(cron)) {
// Omitted Code ..
LOGGER.error("計(jì)劃任務(wù)為空");
}
//2.3 返回執(zhí)行周期(Date)
return new CronTrigger(cron).nextExecutionTime(triggerContext);
}
);
}catch (Exception e){
String ex = exceptionLoggingUtil.exceptionPrint(e);
LOGGER.info(ex);
}
}
}
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- SpringBoot設(shè)置動(dòng)態(tài)定時(shí)任務(wù)的方法詳解
- Springboot自帶定時(shí)任務(wù)實(shí)現(xiàn)動(dòng)態(tài)配置Cron參數(shù)方式
- 淺談SpringBoot集成Quartz動(dòng)態(tài)定時(shí)任務(wù)
- SpringBoot實(shí)現(xiàn)動(dòng)態(tài)定時(shí)任務(wù)
- Springboot整個(gè)Quartz實(shí)現(xiàn)動(dòng)態(tài)定時(shí)任務(wù)的示例代碼
- 詳解SpringBoot 創(chuàng)建定時(shí)任務(wù)(配合數(shù)據(jù)庫動(dòng)態(tài)執(zhí)行)
- springboot整合Quartz實(shí)現(xiàn)動(dòng)態(tài)配置定時(shí)任務(wù)的方法
- SpringBoot實(shí)現(xiàn)動(dòng)態(tài)增刪啟停定時(shí)任務(wù)的方式
相關(guān)文章
java實(shí)現(xiàn)同步的幾種方式(示例詳解)
這篇文章主要介紹了java實(shí)現(xiàn)同步的幾種方式,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),感興趣的朋友一起看看吧2024-12-12
Mybatis詳解在注解sql時(shí)報(bào)錯(cuò)的解決方法
MyBatis-Plus 是一個(gè) Mybatis 增強(qiáng)版工具,在 MyBatis 上擴(kuò)充了其他功能沒有改變其基本功能,為了簡化開發(fā)提交效率而存在,本篇文章帶你看看在注解sql時(shí)所報(bào)出的錯(cuò)誤解決2022-03-03
java反射調(diào)用方法NoSuchMethodException的解決方案
這篇文章主要介紹了java反射調(diào)用方法NoSuchMethodException的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03
springmvc參數(shù)為對(duì)象,數(shù)組的操作
這篇文章主要介紹了springmvc參數(shù)為對(duì)象,數(shù)組的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08
mybatis對(duì)于list更新sql語句的寫法說明
這篇文章主要介紹了mybatis對(duì)于list更新sql語句的寫法說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08
詳解SpringBoot如何創(chuàng)建自定義Starter
Spring Boot的自動(dòng)配置機(jī)制為開發(fā)人員提供了一種輕松集成和配置各種功能的便捷方式,本文將深入探討在Spring Boot中如何創(chuàng)建自定義Starter,為構(gòu)建模塊化且易維護(hù)的應(yīng)用提供有力的支持,需要的朋友可以參考下2024-02-02

