This commit is contained in:
Looly
2025-10-11 16:36:59 +08:00
parent 9f1e240b25
commit b0c375f2d2
4 changed files with 17 additions and 15 deletions

View File

@@ -23,16 +23,18 @@ package cn.hutool.v7.cron;
*
* @author Looly
* @param scheduler 调度器
* @param millis 毫秒数
* @param millis 触发事件的时间戳
*/
public record TaskLauncher(Scheduler scheduler, long millis) implements Runnable {
@Override
public void run() {
//匹配秒部分由用户定义决定,始终不匹配年
scheduler.taskTable.executeTaskIfMatch(this.scheduler, this.millis);
//结束通知
scheduler.taskManager.notifyLauncherCompleted(this);
try{
//匹配秒部分由用户定义决定,始终不匹配年
scheduler.taskTable.executeTaskIfMatch(this.scheduler, this.millis);
} finally {
//结束通知
scheduler.taskManager.notifyLauncherCompleted(this);
}
}
}

View File

@@ -17,20 +17,21 @@
package cn.hutool.v7.cron;
import cn.hutool.v7.core.collection.ListUtil;
import cn.hutool.v7.core.date.DateUtil;
import cn.hutool.v7.cron.task.CronTask;
import cn.hutool.v7.cron.task.Task;
import cn.hutool.v7.log.Log;
import java.io.Serial;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* 任务管理器,提供任务的全生命周期管理,提供:
* <ul>
* <li>启动器管理</li>
* <li>执行器管理</li>
* <li>启动器管理由CronTimer按照固定周期每分钟或每秒钟调用用于检查CronTable中的任务是否匹配。</li>
* <li>执行器管理由CronTable匹配后调用用于启动具体的任务。</li>
* </ul>
*
* @author Looly
@@ -73,7 +74,7 @@ public class TaskManager implements Serializable {
/**
* 启动 TaskLauncher
*
* @param millis 触发事件的毫秒数
* @param millis 触发事件的时间戳
* @return {@link TaskLauncher}
*/
protected TaskLauncher spawnLauncher(final long millis) {
@@ -115,7 +116,7 @@ public class TaskManager implements Serializable {
* @param task {@link Task}
* @return {@link TaskExecutor}
*/
public TaskExecutor spawnExecutor(final CronTask task) {
protected TaskExecutor spawnExecutor(final CronTask task) {
final TaskExecutor executor = new TaskExecutor(this.scheduler, task);
synchronized (this.executors) {
this.executors.add(executor);
@@ -129,7 +130,7 @@ public class TaskManager implements Serializable {
*
* @param executor 执行器 {@link TaskExecutor}
*/
public void notifyExecutorCompleted(final TaskExecutor executor) {
protected void notifyExecutorCompleted(final TaskExecutor executor) {
synchronized (executors) {
executors.remove(executor);
}

View File

@@ -303,7 +303,7 @@ public class TaskTable implements Serializable {
* @param millis 时间毫秒
* @since 3.1.1
*/
protected void executeTaskIfMatchInternal(final Scheduler scheduler, final long millis) {
private void executeTaskIfMatchInternal(final Scheduler scheduler, final long millis) {
final int size = size();
for (int i = 0; i < size; i++) {
if (this.table.getMiddle(i).match(scheduler.config.timezone, millis, scheduler.config.matchSecond)) {

View File

@@ -21,7 +21,6 @@ import cn.hutool.v7.core.thread.ThreadUtil;
import cn.hutool.v7.cron.CronUtil;
import cn.hutool.v7.cron.TaskExecutor;
import cn.hutool.v7.cron.listener.TaskListener;
import cn.hutool.v7.cron.task.Task;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
@@ -33,7 +32,7 @@ public class CronTest {
@Test
@Disabled
public void customCronTest() {
CronUtil.schedule("*/2 * * * * *", (Task) () -> Console.log("Task executed."));
CronUtil.schedule("*/2 * * * * *", () -> Console.log("Task executed."));
// 支持秒级别定时任务
CronUtil.setMatchSecond(true);