|
|
|
@ -3,8 +3,6 @@ package xyz.wbsite.jmacro;
|
|
|
|
|
import xyz.wbsite.jmacro.util.Logger;
|
|
|
|
|
|
|
|
|
|
import java.time.LocalTime;
|
|
|
|
|
import java.time.format.DateTimeFormatter;
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.Arrays;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Map;
|
|
|
|
@ -39,8 +37,6 @@ public class JScheduler {
|
|
|
|
|
*/
|
|
|
|
|
private final Queue<ScheduledFuture<?>> tasksHolder = new ConcurrentLinkedQueue<>();
|
|
|
|
|
|
|
|
|
|
private static final DateTimeFormatter TIME_FORMATTER = DateTimeFormatter.ofPattern("HH:mm:ss");
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 添加固定延迟循环任务
|
|
|
|
|
*
|
|
|
|
@ -82,20 +78,44 @@ public class JScheduler {
|
|
|
|
|
* <p>
|
|
|
|
|
* 例如 "08:30:00"
|
|
|
|
|
*
|
|
|
|
|
* @param task 任务
|
|
|
|
|
* @param time 时间点
|
|
|
|
|
* @param task 任务
|
|
|
|
|
* @param timeArray 时间点数组
|
|
|
|
|
*/
|
|
|
|
|
public void schedule(Runnable task, List<String> time) {
|
|
|
|
|
if (time.isEmpty()) {
|
|
|
|
|
public void schedule(Runnable task, List<String> timeArray) {
|
|
|
|
|
if (timeArray.isEmpty()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 转换为数字集合(秒级精度)
|
|
|
|
|
ConcurrentSkipListSet<Integer> timeSet = new ConcurrentSkipListSet<>();
|
|
|
|
|
Runnable trigger = () -> {
|
|
|
|
|
for (String string : time) {
|
|
|
|
|
for (String time : timeArray) {
|
|
|
|
|
// time 格式兼容处理
|
|
|
|
|
String trimmedTime = time.trim();
|
|
|
|
|
String[] timeParts = trimmedTime.split(":");
|
|
|
|
|
|
|
|
|
|
// 验证格式是否合法(必须是2或3部分)
|
|
|
|
|
if (timeParts.length < 2 || timeParts.length > 3) {
|
|
|
|
|
Logger.error("无效时间格式: " + time + ",需符合HH:mm:ss、H:mm:ss、HH:mm、H:mm或H:m格式");
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 解析时、分、秒(秒默认为0)
|
|
|
|
|
int hours = Integer.parseInt(timeParts[0]);
|
|
|
|
|
int minutes = Integer.parseInt(timeParts[1]);
|
|
|
|
|
int seconds = (timeParts.length == 3) ? Integer.parseInt(timeParts[2]) : 0;
|
|
|
|
|
|
|
|
|
|
// 验证时间数值范围
|
|
|
|
|
if (hours < 0 || hours > 23 || minutes < 0 || minutes > 59 || seconds < 0 || seconds > 59) {
|
|
|
|
|
Logger.error("时间数值超出范围: " + time + ",时(0-23)、分(0-59)、秒(0-59)");
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 格式化为标准HH:mm:ss(自动补零)
|
|
|
|
|
String format = String.format("%02d:%02d:%02d", hours, minutes, seconds);
|
|
|
|
|
|
|
|
|
|
int nowSeconds = LocalTime.now().toSecondOfDay();
|
|
|
|
|
int secondOfDay = LocalTime.parse(string, TIME_FORMATTER).toSecondOfDay();
|
|
|
|
|
int secondOfDay = LocalTime.parse(format).toSecondOfDay();
|
|
|
|
|
if (secondOfDay < nowSeconds) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
@ -148,29 +168,6 @@ public class JScheduler {
|
|
|
|
|
executor.shutdown();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 解析多时间点字符串
|
|
|
|
|
*
|
|
|
|
|
* @param timePoints 时间点字符串(逗号分隔)
|
|
|
|
|
* @return 解析后的时间列表
|
|
|
|
|
*/
|
|
|
|
|
private List<LocalTime> parseTimePoints(String timePoints) {
|
|
|
|
|
List<LocalTime> result = new ArrayList<>();
|
|
|
|
|
if (timePoints == null || timePoints.trim().isEmpty()) {
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
String[] parts = timePoints.split(",");
|
|
|
|
|
for (String part : parts) {
|
|
|
|
|
try {
|
|
|
|
|
result.add(LocalTime.parse(part.trim(), TIME_FORMATTER));
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
System.err.println("无效的时间格式: " + part.trim());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 有限次数任务包装器
|
|
|
|
|
*/
|
|
|
|
|