溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

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

發(fā)布時間:2021-12-18 15:56:49 來源:億速云 閱讀:262 作者:柒染 欄目:編程語言

這篇文章給大家介紹怎樣進行SpringBoot基于數(shù)據(jù)庫的定時任務(wù)統(tǒng)一管理的實現(xiàn),內(nèi)容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。

定時任務(wù)1

import lombok.extern.slf4j.Slf4j;/** * @author Created by niugang on 2019/12/24/15:29 */@Slf4jpublic class TaskTest {  public void task1() {    log.info("反射調(diào)用測試[一]類");  }}

定時任務(wù)2

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

配置類

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@Slf4jpublic class CompleteScheduleConfig implements SchedulingConfigurer {  private static List<TaskRecord> taskRecordList = new ArrayList<>();  

/*   *模擬數(shù)據(jù)庫存儲   */  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í)行定時任務(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("方法名稱:{}",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("方法名稱:{}",declaredMethod.getName());              

declaredMethod.invoke(o); 

           }          }        } 

catch (Exception e1) {          e1.printStackTrace();

        }      };      

CronTask cronTask = new CronTask(runnable, cron);      

ScheduledTask scheduledTask = taskRegistrar.scheduleCronTask(cronTask);      

//scheduledTask.cancel(); 取消定時任務(wù)    }  }  

@Data  private static class TaskRecord {    private String classPath;    

private String executeMehod;    

private String cron;    

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

關(guān)于怎樣進行SpringBoot基于數(shù)據(jù)庫的定時任務(wù)統(tǒng)一管理的實現(xiàn)就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI