Implementation of unified management of timing tasks based on database in springboot

Time:2021-2-25

Timing task 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 ("reflection call test [i]");
  }
}

Timing task 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 (Class 2 of reflection call test);
  }
}

Configuration class

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


  /*
   *Simulation database storage
   */
  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 ("execute timing task, {}"), 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 ("method name: {}"), 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 ("method name: {}"), declaredMethod.getName ());
              declaredMethod.invoke(o);
            }
          }
        } catch (Exception e1) {
          e1.printStackTrace();
        }
      };
      CronTask cronTask = new CronTask(runnable, cron);
      ScheduledTask scheduledTask = taskRegistrar.scheduleCronTask(cronTask);
      // scheduledTask.cancel (); cancel scheduled task

    }


  }


  @Data
  private static class TaskRecord {

    private String classPath;

    private String executeMehod;

    private String cron;

    //You can add a type to perform other types of timed tasks
  }
}

The above is the whole content of this article, I hope to help you learn, and I hope you can support developer more.