Execute a plurality of timing tasks based on Springboot and dynamically acquire timing task information
- 2021-07-24 10:52:33
- OfStack
Brief introduction
Because 1 of the business needs all need to use a number of different timing tasks, and each timing task in the timing information is dynamically obtained through the database. The following is the multitasking timer I wrote using Springboot+Mybatis.
The main functions are as follows:
1. Use multiple timed tasks at the same time
2. Obtain the timing information of the timing task dynamically
Description
Because we need to dynamically obtain the information of timing tasks from the database, we need to integrate SchedulingConfigurer and rewrite configureTasks method. To call different timing tasks, we only need to call different implementations through service method to return the corresponding timing task information. Override this method as many as there are timed tasks (it is best to create different classes). Then start it directly in application.
SpringApplication-Startup class
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);
}
}
Dynamic acquisition of timed task information
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;
}
Timing task
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. Add task content (Runnable) Can be used for a method
() -> Sytem.out.println(" Timing task 1"),
//2. Set the execution cycle (Trigger)
triggerContext -> {
//2.1 Get the execution cycle from the database, where different methods are called to return different timing task information
String cron = controllerService.getSchedultaskForElasticsearch();
System.out.println("controllerService.getSchedultaskForElasticsearch()");
System.out.println(cron);
//2.2 Validity check .
if (StringUtils.isEmpty(cron)) {
// Omitted Code ..
LOGGER.error(" Scheduled task is empty ");
}
//2.3 Return to the execution cycle (Date)
return new CronTrigger(cron).nextExecutionTime(triggerContext);
}
);
}catch (Exception e){
String ex = exceptionLoggingUtil.exceptionPrint(e);
LOGGER.info(ex);
}
}
}