温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

基于Springboot执行多个定时任务并动态获取定时任务信息的示例分析

发布时间:2021-05-22 11:13:07 来源:亿速云 阅读:326 作者:小新 栏目:编程语言

小编给大家分享一下基于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.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);
  }

}

动态获取定时任务信息

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;
}

定时任务

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);
    }

  }
}

springboot是什么

springboot一种全新的编程规范,其设计目的是用来简化新Spring应用的初始搭建以及开发过程,SpringBoot也是一个服务于框架的框架,服务范围是简化配置文件。

以上是“基于Springboot执行多个定时任务并动态获取定时任务信息的示例分析”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI