欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-02-24 09:48:34
简介 因为一些业务的需要所有需要使用多个不同的定时任务,并且每个定时任务中的定时信息是通过数据库动态获取的。下面是我写的使用了springboot+mybatis写的多任...

简介

因为一些业务的需要所有需要使用多个不同的定时任务,并且每个定时任务中的定时信息是通过数据库动态获取的。下面是我写的使用了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);
    }

  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。