基于Springboot执行多个定时任务并动态获取定时任务信息

网友投稿 537 2023-01-08


基于Springboot执行多个定时任务并动态获取定时任务信息

简介

因为一些业务的需要所有需要使用多个不同的定时任务,并且每个定时任务中的定时信息是通过数据库动态获取的。下面是我写的使用了Springboot+Mybatis写的多任务定时器。

主要实现了以下功能:

1、同时使用多个定时任务

2、动态获取定时任务的定时信息

说明

因为我们需要从数据库动态的获取定时任务的信息,所以我们需要集成 SchedulingConfigurer 然后重写 configureTasks 方法即可,调用不同的定时任务只需要通过service方法调用不用的实现返回对应的定时任务信息。有多少个定时任务就重写多少个该方法(最好创建不同的类)。然后直接在application中启动即可。

SpringApplication-启动类

package test;

import org.mybatis.spring.annotation.MapperScan;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.sprEDSDrringframework.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);

}

}

动态获取定时任务信息

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 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 (ExceptiEDSDrron e) {

LOGGER.info(e);

}

return null;

}

定时任务

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.添加任务内容(Runnable),可以为方法

() -> Sytem.out.println("定时任务1"),

//2.设置执行周期(Trigger)

triggerContext -> {

//2.1 从数据库获取执行周期,在这里调用不同的方法返回不同的定时任务信息

String cron = controllerService.getSchedultaskForElasticsearch();

System.out.println("controllerService.getSchedultaskForElasticsearch()");

System.out.println(cron);

//2.2 合法性校验.

if (StringUtils.isEmpty(cron)) {

// Omitted Code ..

LOGGER.error("计划任务为空");

}

//2.3 返回执行周期(Date)

return new CronTrigger(cron).nextExecutionTime(triggerContext);

}

);

}catch (Exception e){

String ex = exceptionLoggingUtil.exceptionPrint(e);

LOGGER.info(ex);

}

}

}


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:自动生成接口测试报告(接口自动化测试教程)
下一篇:微服务网关用限流算法(微服务限流框架)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~