基于Springboot执行多个定时任务并动态获取定时任务信息
简介
因为一些业务的需要所有需要使用多个不同的定时任务,并且每个定时任务中的定时信息是通过数据库动态获取的。下面是我写的使用了Springboot+Mybatis写的多任务定时器。
主要实现了以下功能:
1、同时使用多个定时任务
2、动态获取定时任务的定时信息
说明
因为我们需要从数据库动态的获取定时任务的信息,所以我们需要集成SchedulingConfigurer然后重写configureTasks方法即可,调用不同的定时任务只需要通过service方法调用不用的实现返回对应的定时任务信息。有多少个定时任务就重写多少个该方法(最好创建不同的类)。然后直接在application中启动即可。
SpringApplication-启动类
packagetest;
importorg.mybatis.spring.annotation.MapperScan;
importorg.springframework.boot.SpringApplication;
importorg.springframework.boot.autoconfigure.SpringBootApplication;
importorg.springframework.context.annotation.ComponentScan;
importorg.springframework.scheduling.annotation.EnableScheduling;
importorg.springframework.transaction.annotation.EnableTransactionManagement;
@SpringBootApplication
@EnableTransactionManagement
@EnableScheduling
@ComponentScan(value={"test.*"})
@MapperScan("test.mapper.*")
publicclassTomcatlogApplication{
publicstaticvoidmain(String[]args){
SpringApplication.run(TomcatlogApplication.class,args);
}
}
动态获取定时任务信息
mapper
importorg.apache.ibatis.annotations.Param;
importorg.apache.ibatis.annotations.Select;
importjava.util.List;
/*
*@version1.0createdbyliuxuewenon2018/8/2114:39
*/
publicinterfaceTomcatlogMapper{
@Select("SELECT*FROMscheduledtasksWHEREs.`enable`=1")
StringqueryScheduledTask();
}
service
packagetest.service;
importjava.util.ArrayList;
importjava.util.List;
/*
*@version1.0createdbyliuxuewenon2018/8/2114:44
*/
publicinterfaceTomcatlogService{
ListqueryScheduledTask();
}
serviceimpl
importtest.mapper.tomcatlog.TomcatlogMapper;
importtest.service.TomcatlogService;
importorg.slf4j.Logger;
importorg.slf4j.LoggerFactory;
importorg.springframework.beans.factory.annotation.Autowired;
importorg.springframework.stereotype.Service;
importjava.util.List;
/*
*@version1.0createdbyliuxuewenon2018/8/2114:44
*/
@Service
publicclassTomcatlogServiceImplimplementsTomcatlogService{
privatestaticfinalLoggerLOGGER=LoggerFactory.getLogger(TomcatlogServiceImpl.class);
@Autowired
TomcatlogMappertomcatlogMapper;
@Override
publicStringqueryScheduledTask(){
try{
Stringres=tomcatlogMapper.queryScheduledTask();
returnres;
}catch(Exceptione){
LOGGER.info(e);
}
returnnull;
}
定时任务
packagetest.schedultask;
importtest.controller.MainController;
importtest.service.ControllerService;
importorg.slf4j.Logger;
importorg.slf4j.LoggerFactory;
importorg.springframework.beans.factory.annotation.Autowired;
importorg.springframework.scheduling.annotation.SchedulingConfigurer;
importorg.springframework.scheduling.config.ScheduledTaskRegistrar;
importorg.springframework.scheduling.support.CronTrigger;
importorg.springframework.stereotype.Component;
importorg.springframework.util.StringUtils;
/*
*@version1.0createdbyliuxuewenon2018/8/279:25
*/
@Component
publicclassElasticsearchSchedultaskControllerimplementsSchedulingConfigurer{
privatestaticfinalLoggerLOGGER=LoggerFactory.getLogger(ElasticsearchSchedultaskController.class);
@Autowired
privateControllerServicecontrollerService;
@Autowired
privateMainControllermainController;
@Override
publicvoidconfigureTasks(ScheduledTaskRegistrarscheduledTaskRegistrar){
try{
scheduledTaskRegistrar.addTriggerTask(
//1.添加任务内容(Runnable),可以为方法
()->Sytem.out.println("定时任务1"),
//2.设置执行周期(Trigger)
triggerContext->{
//2.1从数据库获取执行周期,在这里调用不同的方法返回不同的定时任务信息
Stringcron=controllerService.getSchedultaskForElasticsearch();
System.out.println("controllerService.getSchedultaskForElasticsearch()");
System.out.println(cron);
//2.2合法性校验.
if(StringUtils.isEmpty(cron)){
//OmittedCode..
LOGGER.error("计划任务为空");
}
//2.3返回执行周期(Date)
returnnewCronTrigger(cron).nextExecutionTime(triggerContext);
}
);
}catch(Exceptione){
Stringex=exceptionLoggingUtil.exceptionPrint(e);
LOGGER.info(ex);
}
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。