Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report