亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

基于Springboot執(zhí)行多個定時任務并動態(tài)獲取定時任務信息

 更新時間:2019年04月24日 15:36:01   作者:CarolLXW  
這篇文章主要為大家詳細介紹了基于Springboot執(zhí)行多個定時任務并動態(tài)獲取定時任務信息,具有一定的參考價值,感興趣的小伙伴們可以參考一下

簡介

因為一些業(yè)務的需要所有需要使用多個不同的定時任務,并且每個定時任務中的定時信息是通過數據庫動態(tài)獲取的。下面是我寫的使用了Springboot+Mybatis寫的多任務定時器。

主要實現(xiàn)了以下功能:

1、同時使用多個定時任務
2、動態(tài)獲取定時任務的定時信息

說明

因為我們需要從數據庫動態(tài)的獲取定時任務的信息,所以我們需要集成 SchedulingConfigurer 然后重寫 configureTasks 方法即可,調用不同的定時任務只需要通過service方法調用不用的實現(xiàn)返回對應的定時任務信息。有多少個定時任務就重寫多少個該方法(最好創(chuàng)建不同的類)。然后直接在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);
  }

}

動態(tài)獲取定時任務信息

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.設置執(zhí)行周期(Trigger)
          triggerContext -> {
            //2.1 從數據庫獲取執(zhí)行周期,在這里調用不同的方法返回不同的定時任務信息
            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 返回執(zhí)行周期(Date)
            return new CronTrigger(cron).nextExecutionTime(triggerContext);
          }
      );
    }catch (Exception e){
      String ex = exceptionLoggingUtil.exceptionPrint(e);
      LOGGER.info(ex);
    }

  }
}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • 詳解SpringBoot讀取resource目錄下properties文件的常見方式

    詳解SpringBoot讀取resource目錄下properties文件的常見方式

    這篇文章主要介紹了SpringBoot讀取resource目錄下properties文件的常見方式,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-02-02
  • 深入了解Java并發(fā)AQS的獨占鎖模式

    深入了解Java并發(fā)AQS的獨占鎖模式

    AQS是一種提供了原子式管理同步狀態(tài)、阻塞和喚醒線程功能以及隊列模型的簡單框架。一般來說,同步工具實現(xiàn)鎖的控制分為獨占鎖和共享鎖,而AQS提供了對這兩種模式的支持。本文主要來介紹一下獨占鎖模式,需要的可以參考一下
    2022-10-10
  • Spring事務管理只對出現(xiàn)運行期異常進行回滾

    Spring事務管理只對出現(xiàn)運行期異常進行回滾

    Spring的事務管理默認只對出現(xiàn)運行期異常(java.lang.RuntimeException及其子類)進行回滾,需要了解更多Spring事務方面的知識,可詳看本文
    2012-11-11
  • Flutter實現(xiàn)文本組件、圖標及按鈕組件的代碼

    Flutter實現(xiàn)文本組件、圖標及按鈕組件的代碼

    這篇文章主要介紹了Flutter實現(xiàn)文本組件、圖標及按鈕組件的代碼,本文給大家介紹的非常詳細,具有一定的參考借鑒價值 ,需要的朋友可以參考下
    2019-07-07
  • java中break和continue源碼解析

    java中break和continue源碼解析

    這篇文章主要針對java中break和continue的區(qū)別進行詳細介紹,幫助大家更好的學習了解java中break和continue源碼,感興趣的小伙伴們可以參考一下
    2016-06-06
  • SpringBoot使用AOP+注解實現(xiàn)簡單的權限驗證的方法

    SpringBoot使用AOP+注解實現(xiàn)簡單的權限驗證的方法

    這篇文章主要介紹了SpringBoot使用AOP+注解實現(xiàn)簡單的權限驗證的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-05-05
  • SWT(JFace)體驗之Slider,Scale

    SWT(JFace)體驗之Slider,Scale

    SWT(JFace)體驗之Slider,Scale實現(xiàn)代碼。
    2009-06-06
  • Spring?Cloud?OpenFeign實例介紹使用方法

    Spring?Cloud?OpenFeign實例介紹使用方法

    Spring?Cloud?OpenFeign?對?Feign?進行了二次封裝,使得在?Spring?Cloud?中使用?Feign?的時候,可以做到使用?HTTP?請求訪問遠程服務,就像調用本地方法一樣的,開發(fā)者完全感知不到這是在調用遠程訪問,更感知不到在訪問?HTTP?請求
    2022-09-09
  • IDEA中springboot的熱加載thymeleaf靜態(tài)html頁面的方法

    IDEA中springboot的熱加載thymeleaf靜態(tài)html頁面的方法

    這篇文章主要介紹了IDEA中springboot的熱加載thymeleaf靜態(tài)html頁面的方法,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-07-07
  • Spring?Cloud?OpenFeign模版化客戶端搭建過程

    Spring?Cloud?OpenFeign模版化客戶端搭建過程

    OpenFeign是一個顯示聲明式的WebService客戶端。使用OpenFeign能讓編寫Web Service客戶端更加簡單,這篇文章主要介紹了Spring?Cloud?OpenFeign模版化客戶端,需要的朋友可以參考下
    2022-06-06

最新評論