Get the App
SLTechnology News&Howtos  ›  Development  › 

How to realize the unified management of timed tasks based on database in SpringBoot

Shulou Source: shulou.com Published: 2022-06-02 06:10:13 09月11日 Update

This article introduces how to implement the unified management of SpringBoot timed tasks based on database. The content is very detailed. Interested friends can use it for reference. I hope it will be helpful to you.

Scheduled Task 1

Import lombok.extern.slf4j.Slf4j;/** * @ author Created by niugang on 2019 Slf4jpublic class TaskTest * / @ Slf4jpublic class TaskTest {public void task1 () {log.info ("reflection call Test [I]");}}

Scheduled Task 2

Import lombok.extern.slf4j.Slf4j;/** * @ author Created by niugang on 2019 Slf4jpublic class TaskTest2 * / @ Slf4jpublic class TaskTest2 {public void task2 () {log.info ("reflection call Test [II]");}}

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

/ * * simulated database storage * / static {

TaskRecord taskRecord = new TaskRecord (); taskRecord.setExecuteMehod ("task1")

TaskRecord.setClassPath ("com.example.demo.pojo.TaskTest")

TaskRecord.setCron ("0ram 5 *?")

TaskRecordList.add (taskRecord)

TaskRecord taskRecord2 = new TaskRecord ()

TaskRecord2.setExecuteMehod ("task2")

TaskRecord2.setClassPath ("com.example.demo.pojo.TaskTest2")

TaskRecord2.setCron ("0excel 10 *?")

TaskRecordList.add (taskRecord2);} @ Override public void configureTasks (ScheduledTaskRegistrar taskRegistrar) {/ / taskRegistrar.addCronTask (()-> log.info ("execute scheduled tasks, {}", LocalDateTime.now ()), "0 taskRegistrar.addCronTask 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 ()

}}, "0ram 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 tasks}}

@ Data private static class TaskRecord {private String classPath

Private String executeMehod

Private String cron

/ / you can add a type to perform other types of scheduled tasks}}

On how to carry out SpringBoot database-based timed task unified management implementation is shared here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Tags: Tasks data database management unification content name method more reflection help testing good interest guys buddies articles knowledge articles types Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia MariaDB macOS Xiaomi Microsoft