mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
add methods
This commit is contained in:
@@ -110,9 +110,10 @@ public class CronUtil {
|
||||
* 移除任务
|
||||
*
|
||||
* @param schedulerId 任务ID
|
||||
* @return 是否移除成功,{@code false}表示未找到对应ID的任务
|
||||
*/
|
||||
public static void remove(String schedulerId) {
|
||||
scheduler.deschedule(schedulerId);
|
||||
public static boolean remove(String schedulerId) {
|
||||
return scheduler.descheduleWithStatus(schedulerId);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -289,10 +289,21 @@ public class Scheduler implements Serializable {
|
||||
* @return this
|
||||
*/
|
||||
public Scheduler deschedule(String id) {
|
||||
this.taskTable.remove(id);
|
||||
descheduleWithStatus(id);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 移除Task,并返回是否移除成功
|
||||
*
|
||||
* @param id Task的ID
|
||||
* @return 是否移除成功,{@code false}表示未找到对应ID的任务
|
||||
* @since 5.7.17
|
||||
*/
|
||||
public boolean descheduleWithStatus(String id) {
|
||||
return this.taskTable.remove(id);
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新Task执行的时间规则
|
||||
*
|
||||
|
@@ -1,5 +1,6 @@
|
||||
package cn.hutool.cron;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.cron.pattern.CronPattern;
|
||||
import cn.hutool.cron.task.CronTask;
|
||||
import cn.hutool.cron.task.Task;
|
||||
@@ -271,6 +272,16 @@ public class TaskTable implements Serializable {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
final StringBuilder builder = StrUtil.builder();
|
||||
for (int i = 0; i < size; i++) {
|
||||
builder.append(StrUtil.format("[{}] [{}] [{}]\n",
|
||||
ids.get(i), patterns.get(i), tasks.get(i)));
|
||||
}
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 如果时间匹配则执行相应的Task,无锁
|
||||
*
|
||||
|
21
hutool-cron/src/test/java/cn/hutool/cron/TaskTableTest.java
Normal file
21
hutool-cron/src/test/java/cn/hutool/cron/TaskTableTest.java
Normal file
@@ -0,0 +1,21 @@
|
||||
package cn.hutool.cron;
|
||||
|
||||
import cn.hutool.core.lang.Console;
|
||||
import cn.hutool.core.util.IdUtil;
|
||||
import cn.hutool.cron.pattern.CronPattern;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
public class TaskTableTest {
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void toStringTest(){
|
||||
final TaskTable taskTable = new TaskTable();
|
||||
taskTable.add(IdUtil.fastUUID(), new CronPattern("*/10 * * * * *"), ()-> Console.log("Task 1"));
|
||||
taskTable.add(IdUtil.fastUUID(), new CronPattern("*/20 * * * * *"), ()-> Console.log("Task 2"));
|
||||
taskTable.add(IdUtil.fastUUID(), new CronPattern("*/30 * * * * *"), ()-> Console.log("Task 3"));
|
||||
|
||||
Console.log(taskTable);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user