mirror of
https://gitee.com/dromara/hutool.git
synced 2026-02-09 09:16:26 +08:00
add TaskManager
This commit is contained in:
@@ -18,12 +18,15 @@ package cn.hutool.v7.cron;
|
||||
|
||||
import cn.hutool.v7.core.exception.HutoolException;
|
||||
|
||||
import java.io.Serial;
|
||||
|
||||
/**
|
||||
* 定时任务异常
|
||||
*
|
||||
* @author Looly
|
||||
*/
|
||||
public class CronException extends HutoolException {
|
||||
@Serial
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
|
||||
@@ -98,7 +98,7 @@ public class CronTimer extends Thread implements Serializable {
|
||||
* @param millis 当前时间
|
||||
*/
|
||||
private void spawnLauncher(final long millis){
|
||||
this.scheduler.taskLauncherManager.spawnLauncher(millis);
|
||||
this.scheduler.taskManager.spawnLauncher(millis);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -86,14 +86,12 @@ public class Scheduler implements Serializable {
|
||||
private CronTimer timer;
|
||||
/** 定时任务表 */
|
||||
protected TaskTable taskTable = new TaskTable();
|
||||
/** 启动器管理器 */
|
||||
protected TaskLauncherManager taskLauncherManager;
|
||||
/** 执行器管理器 */
|
||||
protected TaskExecutorManager taskExecutorManager;
|
||||
/** 监听管理器列表 */
|
||||
protected TaskListenerManager listenerManager = new TaskListenerManager();
|
||||
/** 线程池,用于执行TaskLauncher和TaskExecutor */
|
||||
protected ExecutorService threadExecutor;
|
||||
/** 任务管理器 */
|
||||
protected TaskManager taskManager;
|
||||
/** 监听管理器列表 */
|
||||
protected TaskListenerManager listenerManager = new TaskListenerManager();
|
||||
|
||||
// --------------------------------------------------------- Getters and Setters start
|
||||
/**
|
||||
@@ -208,7 +206,7 @@ public class Scheduler implements Serializable {
|
||||
}
|
||||
// --------------------------------------------------------- Getters and Setters end
|
||||
|
||||
// -------------------------------------------------------------------- shcedule start
|
||||
// region ----- schedule
|
||||
/**
|
||||
* 批量加入配置文件中的定时任务<br>
|
||||
* 配置文件格式为: xxx.xxx.xxx.Class.method = * * * * *
|
||||
@@ -300,6 +298,7 @@ public class Scheduler implements Serializable {
|
||||
descheduleWithStatus(id);
|
||||
return this;
|
||||
}
|
||||
// endregion
|
||||
|
||||
/**
|
||||
* 移除Task,并返回是否移除成功
|
||||
@@ -386,7 +385,6 @@ public class Scheduler implements Serializable {
|
||||
this.taskTable = new TaskTable();
|
||||
return this;
|
||||
}
|
||||
// -------------------------------------------------------------------- shcedule end
|
||||
|
||||
/**
|
||||
* @return 是否已经启动
|
||||
@@ -422,8 +420,7 @@ public class Scheduler implements Serializable {
|
||||
ThreadFactoryBuilder.of().setNamePrefix("hutool-cron-").setDaemon(this.daemon).build()//
|
||||
).build();
|
||||
}
|
||||
this.taskLauncherManager = new TaskLauncherManager(this);
|
||||
this.taskExecutorManager = new TaskExecutorManager(this);
|
||||
this.taskManager = new TaskManager(this);
|
||||
|
||||
// Start CronTimer
|
||||
timer = new CronTimer(this);
|
||||
|
||||
@@ -39,7 +39,7 @@ public record TaskExecutor(Scheduler scheduler, CronTask cronTask) implements Ru
|
||||
} catch (final Exception e) {
|
||||
scheduler.listenerManager.notifyTaskFailed(this, e);
|
||||
} finally {
|
||||
scheduler.taskExecutorManager.notifyExecutorCompleted(this);
|
||||
scheduler.taskManager.notifyExecutorCompleted(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,22 +22,10 @@ package cn.hutool.v7.cron;
|
||||
* 检查完毕后启动器结束
|
||||
*
|
||||
* @author Looly
|
||||
* @param scheduler 调度器
|
||||
* @param millis 毫秒数
|
||||
*/
|
||||
public class TaskLauncher implements Runnable {
|
||||
|
||||
private final Scheduler scheduler;
|
||||
private final long millis;
|
||||
|
||||
/**
|
||||
* 构造
|
||||
*
|
||||
* @param scheduler {@link Scheduler}
|
||||
* @param millis 毫秒数
|
||||
*/
|
||||
public TaskLauncher(final Scheduler scheduler, final long millis) {
|
||||
this.scheduler = scheduler;
|
||||
this.millis = millis;
|
||||
}
|
||||
public record TaskLauncher(Scheduler scheduler, long millis) implements Runnable {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
@@ -45,6 +33,6 @@ public class TaskLauncher implements Runnable {
|
||||
scheduler.taskTable.executeTaskIfMatch(this.scheduler, this.millis);
|
||||
|
||||
//结束通知
|
||||
scheduler.taskLauncherManager.notifyLauncherCompleted(this);
|
||||
scheduler.taskManager.notifyLauncherCompleted(this);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,67 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2013-2025 Hutool Team and hutool.cn
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package cn.hutool.v7.cron;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 作业启动管理器
|
||||
*
|
||||
* @author Looly
|
||||
*
|
||||
*/
|
||||
public class TaskLauncherManager implements Serializable {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
protected Scheduler scheduler;
|
||||
/** 启动器列表 */
|
||||
protected final List<TaskLauncher> launchers = new ArrayList<>();
|
||||
|
||||
/**
|
||||
* 构造
|
||||
* @param scheduler {@link Scheduler}
|
||||
*/
|
||||
public TaskLauncherManager(final Scheduler scheduler) {
|
||||
this.scheduler = scheduler;
|
||||
}
|
||||
|
||||
/**
|
||||
* 启动 TaskLauncher
|
||||
* @param millis 触发事件的毫秒数
|
||||
* @return {@link TaskLauncher}
|
||||
*/
|
||||
protected TaskLauncher spawnLauncher(final long millis) {
|
||||
final TaskLauncher launcher = new TaskLauncher(this.scheduler, millis);
|
||||
synchronized (this.launchers) {
|
||||
this.launchers.add(launcher);
|
||||
}
|
||||
this.scheduler.threadExecutor.execute(launcher);
|
||||
return launcher;
|
||||
}
|
||||
|
||||
/**
|
||||
* 启动器启动完毕,启动完毕后从执行器列表中移除
|
||||
* @param launcher 启动器 {@link TaskLauncher}
|
||||
*/
|
||||
protected void notifyLauncherCompleted(final TaskLauncher launcher) {
|
||||
synchronized (launchers) {
|
||||
launchers.remove(launcher);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2013-2025 Hutool Team and hutool.cn
|
||||
* Copyright (c) 2025 Hutool Team and hutool.cn
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -19,26 +19,31 @@ package cn.hutool.v7.cron;
|
||||
import cn.hutool.v7.cron.task.CronTask;
|
||||
import cn.hutool.v7.cron.task.Task;
|
||||
|
||||
import java.io.Serial;
|
||||
import java.io.Serializable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 作业执行管理器<br>
|
||||
* 负责管理作业的启动、停止等
|
||||
*
|
||||
* <p>
|
||||
* 此类用于管理正在运行的作业情况,作业启动后加入任务列表,任务结束移除
|
||||
* </p>
|
||||
* 任务管理器,提供:
|
||||
* <ul>
|
||||
* <li>启动器管理</li>
|
||||
* <li>执行器管理</li>
|
||||
* </ul>
|
||||
*
|
||||
* @author Looly
|
||||
* @since 3.0.1
|
||||
* @since 7.0.0
|
||||
*/
|
||||
public class TaskExecutorManager implements Serializable {
|
||||
public class TaskManager implements Serializable {
|
||||
@Serial
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
protected Scheduler scheduler;
|
||||
protected final Scheduler scheduler;
|
||||
/**
|
||||
* 启动器列表
|
||||
*/
|
||||
protected final List<TaskLauncher> launchers = new ArrayList<>();
|
||||
/**
|
||||
* 执行器列表
|
||||
*/
|
||||
@@ -46,13 +51,39 @@ public class TaskExecutorManager implements Serializable {
|
||||
|
||||
/**
|
||||
* 构造
|
||||
*
|
||||
* @param scheduler {@link Scheduler}
|
||||
*/
|
||||
public TaskExecutorManager(final Scheduler scheduler) {
|
||||
public TaskManager(final Scheduler scheduler) {
|
||||
this.scheduler = scheduler;
|
||||
}
|
||||
|
||||
// region ----- TaskLauncher
|
||||
/**
|
||||
* 启动 TaskLauncher
|
||||
* @param millis 触发事件的毫秒数
|
||||
* @return {@link TaskLauncher}
|
||||
*/
|
||||
protected TaskLauncher spawnLauncher(final long millis) {
|
||||
final TaskLauncher launcher = new TaskLauncher(this.scheduler, millis);
|
||||
synchronized (this.launchers) {
|
||||
this.launchers.add(launcher);
|
||||
}
|
||||
this.scheduler.threadExecutor.execute(launcher);
|
||||
return launcher;
|
||||
}
|
||||
|
||||
/**
|
||||
* 启动器启动完毕,启动完毕后从执行器列表中移除
|
||||
* @param launcher 启动器 {@link TaskLauncher}
|
||||
*/
|
||||
protected void notifyLauncherCompleted(final TaskLauncher launcher) {
|
||||
synchronized (launchers) {
|
||||
launchers.remove(launcher);
|
||||
}
|
||||
}
|
||||
// endregion
|
||||
|
||||
// region ----- TaskExecutor
|
||||
/**
|
||||
* 获取所有正在执行的任务调度执行器
|
||||
*
|
||||
@@ -82,12 +113,11 @@ public class TaskExecutorManager implements Serializable {
|
||||
* 执行器执行完毕调用此方法,将执行器从执行器列表移除,此方法由{@link TaskExecutor}对象调用,用于通知管理器自身已完成执行
|
||||
*
|
||||
* @param executor 执行器 {@link TaskExecutor}
|
||||
* @return this
|
||||
*/
|
||||
public TaskExecutorManager notifyExecutorCompleted(final TaskExecutor executor) {
|
||||
public void notifyExecutorCompleted(final TaskExecutor executor) {
|
||||
synchronized (executors) {
|
||||
executors.remove(executor);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
// endregion
|
||||
}
|
||||
@@ -22,6 +22,7 @@ import cn.hutool.v7.cron.pattern.CronPattern;
|
||||
import cn.hutool.v7.cron.task.CronTask;
|
||||
import cn.hutool.v7.cron.task.Task;
|
||||
|
||||
import java.io.Serial;
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.locks.Lock;
|
||||
@@ -36,6 +37,7 @@ import java.util.concurrent.locks.ReentrantReadWriteLock;
|
||||
* @author Looly
|
||||
*/
|
||||
public class TaskTable implements Serializable {
|
||||
@Serial
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
@@ -305,7 +307,7 @@ public class TaskTable implements Serializable {
|
||||
final int size = size();
|
||||
for (int i = 0; i < size; i++) {
|
||||
if (this.table.getMiddle(i).match(scheduler.config.timezone, millis, scheduler.config.matchSecond)) {
|
||||
scheduler.taskExecutorManager.spawnExecutor(
|
||||
scheduler.taskManager.spawnExecutor(
|
||||
new CronTask(this.table.getLeft(i), this.table.getMiddle(i), this.table.getRight(i)));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,6 +19,7 @@ package cn.hutool.v7.cron.listener;
|
||||
import cn.hutool.v7.cron.TaskExecutor;
|
||||
import cn.hutool.v7.log.LogUtil;
|
||||
|
||||
import java.io.Serial;
|
||||
import java.io.Serializable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
@@ -29,6 +30,7 @@ import java.util.List;
|
||||
*
|
||||
*/
|
||||
public class TaskListenerManager implements Serializable {
|
||||
@Serial
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private final List<TaskListener> listeners = new ArrayList<>();
|
||||
|
||||
@@ -19,19 +19,10 @@ package cn.hutool.v7.cron.task;
|
||||
/**
|
||||
* {@link Runnable} 的 {@link Task}包装
|
||||
*
|
||||
* @param runnable 任务执行体
|
||||
* @author Looly
|
||||
*/
|
||||
public class RunnableTask implements Task {
|
||||
private final Runnable runnable;
|
||||
|
||||
/**
|
||||
* 构造
|
||||
*
|
||||
* @param runnable {@link Runnable}
|
||||
*/
|
||||
public RunnableTask(final Runnable runnable) {
|
||||
this.runnable = runnable;
|
||||
}
|
||||
public record RunnableTask(Runnable runnable) implements Task {
|
||||
|
||||
@Override
|
||||
public void execute() {
|
||||
|
||||
Reference in New Issue
Block a user