依赖jar包

引入包 版本
jdk 1.8
spring boot 2.7.10
spring-context 5.3.22
allbs-common 2.0.0

使用

添加依赖

1
2
3
4
5
<dependency>
<groupId>cn.allbs</groupId>
<artifactId>allbs-scheduled</artifactId>
<version>2.0.0</version>
</dependency>
1
implementation 'cn.allbs:allbs-scheduled:2.0.0'
1
implementation("cn.allbs:allbs-scheduled:2.0.0")

开启动态定时任务

默认功能开启

注入

1
private final CronTaskRegistrar cronTaskRegistrar;

添加参数并启动

String

1
2
SchedulingRunnable task = new SchedulingRunnable("testTask", "taskWithStr", str);
cronTaskRegistrar.addCronTask(task, "0/2 * * * * ?");

Map

1
2
SchedulingRunnable task = new SchedulingRunnable("testTask", "taskWithStr", params);
cronTaskRegistrar.addCronTask(task, "0/5 * * * * ?");

停止任务

1
2
SchedulingRunnable task = new SchedulingRunnable("testTask", "taskWithStr", params);
cronTaskRegistrar.removeCronTask(task);

测试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@Component("testTask")
public class TestTask {

public void taskWithStr(String params) {
System.out.println("执行参数为String任务:" + params);
}

public void taskWithMap(Map<String, Object> params) {
System.out.println("执行参数为Map任务:" + params);
}

public void taskNoParams() {
System.out.println("执行无参示例任务");
}
}