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

View File

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

View File

@ -303,7 +303,7 @@ public class TaskTable implements Serializable {
* @param millis 时间毫秒 * @param millis 时间毫秒
* @since 3.1.1 * @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(); final int size = size();
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {
if (this.table.getMiddle(i).match(scheduler.config.timezone, millis, scheduler.config.matchSecond)) { 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.CronUtil;
import cn.hutool.v7.cron.TaskExecutor; import cn.hutool.v7.cron.TaskExecutor;
import cn.hutool.v7.cron.listener.TaskListener; 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.Disabled;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
@ -33,7 +32,7 @@ public class CronTest {
@Test @Test
@Disabled @Disabled
public void customCronTest() { public void customCronTest() {
CronUtil.schedule("*/2 * * * * *", (Task) () -> Console.log("Task executed.")); CronUtil.schedule("*/2 * * * * *", () -> Console.log("Task executed."));
// 支持秒级别定时任务 // 支持秒级别定时任务
CronUtil.setMatchSecond(true); CronUtil.setMatchSecond(true);