欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot基于數(shù)據(jù)庫(kù)的定時(shí)任務(wù)統(tǒng)一管理的實(shí)現(xiàn)

 更新時(shí)間:2019年12月26日 11:58:17   作者:盲目的拾荒者  
這篇文章主要介紹了SpringBoot基于數(shù)據(jù)庫(kù)的定時(shí)任務(wù)統(tǒng)一管理的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

定時(shí)任務(wù)1

import lombok.extern.slf4j.Slf4j;

/**
 * @author Created by niugang on 2019/12/24/15:29
 */
@Slf4j
public class TaskTest {


  public void task1() {
    log.info("反射調(diào)用測(cè)試[一]類(lèi)");
  }
}

定時(shí)任務(wù)2

import lombok.extern.slf4j.Slf4j;

/**
 * @author Created by niugang on 2019/12/24/15:54
 */
@Slf4j
public class TaskTest2 {
  public void task2() {
    log.info("反射調(diào)用測(cè)試[二]類(lèi)");
  }
}

配置類(lèi)

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.CronTask;
import org.springframework.scheduling.config.ScheduledTask;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;


/**
 * @author Created by niugang on 2019/12/24/15:19
 */
@Configuration
@EnableScheduling
@Slf4j
public class CompleteScheduleConfig implements SchedulingConfigurer {

  private static List<TaskRecord> taskRecordList = new ArrayList<>();


  /*
   *模擬數(shù)據(jù)庫(kù)存儲(chǔ)
   */
  static {
    TaskRecord taskRecord = new TaskRecord();
    taskRecord.setExecuteMehod("task1");
    taskRecord.setClassPath("com.example.demo.pojo.TaskTest");
    taskRecord.setCron("0/5 * * * * ?");
    taskRecordList.add(taskRecord);

    TaskRecord taskRecord2 = new TaskRecord();
    taskRecord2.setExecuteMehod("task2");
    taskRecord2.setClassPath("com.example.demo.pojo.TaskTest2");
    taskRecord2.setCron("0/10 * * * * ?");
    taskRecordList.add(taskRecord2);
  }


  @Override
  public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
    // taskRegistrar.addCronTask(() -> log.info("執(zhí)行定時(shí)任務(wù),{}", LocalDateTime.now()), "0/5 * * * * ?");
/*    taskRegistrar.addCronTask(new Runnable() {
      @Override
      public void run() {
        try {
          Class<?> aClass = Class.forName("com.example.demo.pojo.TaskTest");
          Object o = aClass.newInstance();
          Method[] declaredMethods = aClass.getDeclaredMethods();
          for (Method declaredMethod : declaredMethods) {
            declaredMethod.invoke(o);
            // log.info("方法名稱(chēng):{}",declaredMethod.getName());
          }
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
    }, "0/5 * * * * ?");*/

    for (TaskRecord taskRecord : taskRecordList) {
      String classPath = taskRecord.getClassPath();
      String cron = taskRecord.getCron();
      String executeMehod = taskRecord.getExecuteMehod();
      Runnable runnable = () -> {
        Class<?> aClass;
        try {
          aClass = Class.forName(classPath);
          Object o = aClass.newInstance();
          Method[] declaredMethods = aClass.getDeclaredMethods();
          for (Method declaredMethod : declaredMethods) {
            if (declaredMethod.getName().equals(executeMehod)) {
              /// log.info("方法名稱(chēng):{}",declaredMethod.getName());
              declaredMethod.invoke(o);
            }
          }
        } catch (Exception e1) {
          e1.printStackTrace();
        }
      };
      CronTask cronTask = new CronTask(runnable, cron);
      ScheduledTask scheduledTask = taskRegistrar.scheduleCronTask(cronTask);
      //scheduledTask.cancel(); 取消定時(shí)任務(wù)

    }


  }


  @Data
  private static class TaskRecord {

    private String classPath;

    private String executeMehod;

    private String cron;

    //可以在增加一個(gè)type 執(zhí)行其他類(lèi)型的定時(shí)任務(wù)
  }
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 通過(guò)第三方接口發(fā)送短信驗(yàn)證碼/短信通知(推薦)

    通過(guò)第三方接口發(fā)送短信驗(yàn)證碼/短信通知(推薦)

    這篇文章主要介紹了通過(guò)第三方接口發(fā)送短信驗(yàn)證碼/短信通知(推薦)的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下
    2016-08-08
  • Java實(shí)現(xiàn)字符數(shù)組全排列的方法

    Java實(shí)現(xiàn)字符數(shù)組全排列的方法

    這篇文章主要介紹了Java實(shí)現(xiàn)字符數(shù)組全排列的方法,涉及Java針對(duì)字符數(shù)組的遍歷及排序算法的實(shí)現(xiàn)技巧,需要的朋友可以參考下
    2015-12-12
  • Java中的functor實(shí)現(xiàn)

    Java中的functor實(shí)現(xiàn)

    Java中的functor實(shí)現(xiàn)...
    2006-12-12
  • Java Collections.shuffle()方法案例詳解

    Java Collections.shuffle()方法案例詳解

    這篇文章主要介紹了Java Collections.shuffle()方法案例詳解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-08-08
  • java通過(guò)證書(shū)訪(fǎng)問(wèn)etcd的實(shí)現(xiàn)步驟

    java通過(guò)證書(shū)訪(fǎng)問(wèn)etcd的實(shí)現(xiàn)步驟

    Etcd提供了多種語(yǔ)言的客戶(hù)端庫(kù),本文主要介紹了java通過(guò)證書(shū)訪(fǎng)問(wèn)etcd的實(shí)現(xiàn)步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2024-05-05
  • MybatisPlus多表連接查詢(xún)的具體實(shí)現(xiàn)

    MybatisPlus多表連接查詢(xún)的具體實(shí)現(xiàn)

    MyBatis Plus是一款針對(duì)MyBatis框架的增強(qiáng)工具, 它提供了很多方便的方法來(lái)實(shí)現(xiàn)多表聯(lián)查,本文主要介紹了MybatisPlus多表連接查詢(xún)的具體實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下
    2023-10-10
  • TransmittableThreadLocal解決線(xiàn)程間上下文傳遞煩惱

    TransmittableThreadLocal解決線(xiàn)程間上下文傳遞煩惱

    這篇文章主要為大家介紹了TransmittableThreadLocal解決線(xiàn)程間上下文傳遞煩惱詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-11-11
  • Java中this的用法實(shí)例總結(jié)

    Java中this的用法實(shí)例總結(jié)

    JAVA中的this是一個(gè)非常重要的模塊,在編程中有非常重要的地位,擅長(zhǎng)用this的人常常可以使程序更加簡(jiǎn)潔和方便,下面這篇文章主要給大家介紹了關(guān)于Java中this用法的相關(guān)資料,需要的朋友可以參考下
    2022-08-08
  • Struts2實(shí)現(xiàn)文件上傳時(shí)顯示進(jìn)度條功能

    Struts2實(shí)現(xiàn)文件上傳時(shí)顯示進(jìn)度條功能

    這篇文章主要為大家詳細(xì)介紹了Struts2實(shí)現(xiàn)文件上傳時(shí)顯示進(jìn)度條功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-05-05
  • springboot+redis實(shí)現(xiàn)簡(jiǎn)單的熱搜功能

    springboot+redis實(shí)現(xiàn)簡(jiǎn)單的熱搜功能

    這篇文章主要介紹了springboot+redis實(shí)現(xiàn)一個(gè)簡(jiǎn)單的熱搜功能,通過(guò)代碼介紹了過(guò)濾不雅文字的過(guò)濾器,代碼簡(jiǎn)單易懂,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-05-05

最新評(píng)論