Scheduled优化

This commit is contained in:
redkale
2023-12-07 14:48:54 +08:00
parent cdcc42c90a
commit 42a1229f1f
2 changed files with 24 additions and 6 deletions

View File

@@ -28,7 +28,9 @@ public @interface Scheduled {
String name() default "";
/**
* cron表达式
* cron表达式, 特殊值: <br>
* yearly、annually、monthly、weekly、daily、midnight、hourly、minutely
* 1m、2m、3m、5m、10m、15m、30m、1h、2h、3h、6h
*
* @return cron表达式
*/

View File

@@ -56,8 +56,8 @@ public class ScheduledFactory {
protected ScheduledFactory(UnaryOperator<String> propertyFunc) {
this.propertyFunc = propertyFunc;
this.scheduler = new ScheduledThreadPoolExecutor(Utility.cpus(), Utility.newThreadFactory("Scheduled-Task-Thread-%s"));
this.scheduler.setRemoveOnCancelPolicy(true);
this.scheduler = new ScheduledThreadPoolExecutor(Utility.cpus(), Utility.newThreadFactory("Scheduled-Task-Thread-%s"));
this.scheduler.setRemoveOnCancelPolicy(true);
}
public static ScheduledFactory create(UnaryOperator<String> propertyFunc) {
@@ -149,9 +149,9 @@ public class ScheduledFactory {
CronExpression cronExpr = CronExpression.parse(cron);
return new CronTask(ref, name, method, cronExpr, zoneId);
} else {
long fixedDelayLong = Long.parseLong(fixedDelay);
long fixedRateLong = Long.parseLong(fixedRate);
long initialDelayLong = Long.parseLong(initialDelay);
long fixedDelayLong = getLongValue(fixedDelay);
long fixedRateLong = getLongValue(fixedRate);
long initialDelayLong = getLongValue(initialDelay);
return new FixedTask(ref, name, method, fixedDelayLong, fixedRateLong, initialDelayLong, timeUnit);
}
}
@@ -187,6 +187,22 @@ public class ScheduledFactory {
return propertyFunc.apply(value);
}
//支持5*60乘法表达式
protected long getLongValue(String value) {
if (value.indexOf('*') > -1) {
long rs = 1;
boolean flag = false;
for (String v : value.split("\\*")) {
if (!v.trim().isEmpty()) {
rs *= Long.parseLong(v.trim());
flag = true;
}
}
return flag ? rs : -1;
}
return Long.parseLong(value);
}
public void destroy() {
if (scheduler != null) {
scheduler.shutdown();