Scheduled优化
This commit is contained in:
@@ -28,7 +28,9 @@ public @interface Scheduled {
|
|||||||
String name() default "";
|
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表达式
|
* @return cron表达式
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -56,8 +56,8 @@ public class ScheduledFactory {
|
|||||||
|
|
||||||
protected ScheduledFactory(UnaryOperator<String> propertyFunc) {
|
protected ScheduledFactory(UnaryOperator<String> propertyFunc) {
|
||||||
this.propertyFunc = propertyFunc;
|
this.propertyFunc = propertyFunc;
|
||||||
this.scheduler = new ScheduledThreadPoolExecutor(Utility.cpus(), Utility.newThreadFactory("Scheduled-Task-Thread-%s"));
|
this.scheduler = new ScheduledThreadPoolExecutor(Utility.cpus(), Utility.newThreadFactory("Scheduled-Task-Thread-%s"));
|
||||||
this.scheduler.setRemoveOnCancelPolicy(true);
|
this.scheduler.setRemoveOnCancelPolicy(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static ScheduledFactory create(UnaryOperator<String> propertyFunc) {
|
public static ScheduledFactory create(UnaryOperator<String> propertyFunc) {
|
||||||
@@ -149,9 +149,9 @@ public class ScheduledFactory {
|
|||||||
CronExpression cronExpr = CronExpression.parse(cron);
|
CronExpression cronExpr = CronExpression.parse(cron);
|
||||||
return new CronTask(ref, name, method, cronExpr, zoneId);
|
return new CronTask(ref, name, method, cronExpr, zoneId);
|
||||||
} else {
|
} else {
|
||||||
long fixedDelayLong = Long.parseLong(fixedDelay);
|
long fixedDelayLong = getLongValue(fixedDelay);
|
||||||
long fixedRateLong = Long.parseLong(fixedRate);
|
long fixedRateLong = getLongValue(fixedRate);
|
||||||
long initialDelayLong = Long.parseLong(initialDelay);
|
long initialDelayLong = getLongValue(initialDelay);
|
||||||
return new FixedTask(ref, name, method, fixedDelayLong, fixedRateLong, initialDelayLong, timeUnit);
|
return new FixedTask(ref, name, method, fixedDelayLong, fixedRateLong, initialDelayLong, timeUnit);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -187,6 +187,22 @@ public class ScheduledFactory {
|
|||||||
return propertyFunc.apply(value);
|
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() {
|
public void destroy() {
|
||||||
if (scheduler != null) {
|
if (scheduler != null) {
|
||||||
scheduler.shutdown();
|
scheduler.shutdown();
|
||||||
|
|||||||
Reference in New Issue
Block a user