优化schedule

This commit is contained in:
redkale
2023-12-19 22:23:43 +08:00
parent e8eea11ca6
commit 7a37df319c
2 changed files with 20 additions and 39 deletions

View File

@@ -21,7 +21,6 @@ import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ScheduledFuture; import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.ScheduledThreadPoolExecutor; import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.ReentrantLock; import java.util.concurrent.locks.ReentrantLock;
import java.util.function.UnaryOperator; import java.util.function.UnaryOperator;
@@ -62,8 +61,6 @@ public class ScheduleManagerService implements ScheduleManager, Service {
private final ConcurrentHashMap<WeakReference, List<ScheduledTask>> refTaskMap = new ConcurrentHashMap<>(); private final ConcurrentHashMap<WeakReference, List<ScheduledTask>> refTaskMap = new ConcurrentHashMap<>();
private final AtomicBoolean inited = new AtomicBoolean();
private final ReentrantLock lock = new ReentrantLock(); private final ReentrantLock lock = new ReentrantLock();
@Resource(required = false) @Resource(required = false)
@@ -76,7 +73,7 @@ public class ScheduleManagerService implements ScheduleManager, Service {
private boolean enabled = true; private boolean enabled = true;
private AnyValue config; protected AnyValue config;
protected ScheduleManagerService(UnaryOperator<String> propertyFunc) { protected ScheduleManagerService(UnaryOperator<String> propertyFunc) {
this.propertyFunc = propertyFunc; this.propertyFunc = propertyFunc;
@@ -102,20 +99,14 @@ public class ScheduleManagerService implements ScheduleManager, Service {
conf = AnyValue.create(); conf = AnyValue.create();
} }
this.config = conf; this.config = conf;
init(); this.enabled = config.getBoolValue("enabled", true);
} if (this.enabled) {
if (this.propertyFunc == null && application != null) {
private void init() { UnaryOperator<String> func = application.getEnvironment()::getPropertyValue;
if (inited.compareAndSet(false, true)) { this.propertyFunc = func;
this.enabled = config.getBoolValue("enabled", true);
if (this.enabled) {
if (this.propertyFunc == null && application != null) {
UnaryOperator<String> func = application.getEnvironment()::getPropertyValue;
this.propertyFunc = func;
}
this.scheduler = new ScheduledThreadPoolExecutor(Utility.cpus(), Utility.newThreadFactory("Scheduled-Task-Thread-%s"));
this.scheduler.setRemoveOnCancelPolicy(true);
} }
this.scheduler = new ScheduledThreadPoolExecutor(Utility.cpus(), Utility.newThreadFactory("Redkale-Scheduled-Task-Thread-%s"));
this.scheduler.setRemoveOnCancelPolicy(true);
} }
} }
@@ -124,11 +115,6 @@ public class ScheduleManagerService implements ScheduleManager, Service {
if (scheduler != null) { if (scheduler != null) {
scheduler.shutdown(); scheduler.shutdown();
} }
inited.set(false);
}
public boolean isInited() {
return inited.get();
} }
@Override @Override
@@ -151,19 +137,20 @@ public class ScheduleManagerService implements ScheduleManager, Service {
continue; continue;
} }
if (tasks.containsKey(method.getName())) { if (tasks.containsKey(method.getName())) {
//跳过已处理的继承方法
continue; continue;
} }
if (method.getParameterCount() != 0 if (method.getParameterCount() != 0
&& (method.getParameterCount() == 1 && method.getParameterTypes()[0] == ScheduleEvent.class)) { && (method.getParameterCount() == 1 && method.getParameterTypes()[0] == ScheduleEvent.class)) {
throw new RedkaleException("@" + Scheduled.class.getSimpleName() throw new RedkaleException("@" + Scheduled.class.getSimpleName() + " must be on non-parameter or "
+ " must be on non-parameter or " + ScheduleEvent.class.getSimpleName() + "-parameter method, but on " + method); + ScheduleEvent.class.getSimpleName() + "-parameter method, but on " + method);
} }
ScheduledTask task = schedule(ref, method, taskCount); ScheduledTask task = schedule(ref, method, taskCount);
if (task == null) { //时间没配置: task=null
continue; //时间都没配置 if (task != null) {
tasks.put(method.getName(), task);
RedkaleClassLoader.putReflectionMethod(clazz.getName(), method);
} }
tasks.put(method.getName(), task);
RedkaleClassLoader.putReflectionMethod(clazz.getName(), method);
} }
} while ((clazz = clazz.getSuperclass()) != Object.class); } while ((clazz = clazz.getSuperclass()) != Object.class);
//开始执行定时任务 //开始执行定时任务
@@ -181,20 +168,14 @@ public class ScheduleManagerService implements ScheduleManager, Service {
public void unschedule(Object service) { public void unschedule(Object service) {
lock.lock(); lock.lock();
try { try {
Map.Entry<WeakReference, List<ScheduledTask>> entry = null;
for (Map.Entry<WeakReference, List<ScheduledTask>> item : refTaskMap.entrySet()) { for (Map.Entry<WeakReference, List<ScheduledTask>> item : refTaskMap.entrySet()) {
if (item.getKey().get() == service) { if (item.getKey().get() == service) {
entry = item; refTaskMap.remove(item.getKey());
break; for (ScheduledTask task : item.getValue()) {
task.cancel();
}
} }
} }
if (entry == null) {
return;
}
refTaskMap.remove(entry.getKey());
for (ScheduledTask task : entry.getValue()) {
task.cancel();
}
} finally { } finally {
lock.unlock(); lock.unlock();
} }

View File

@@ -57,7 +57,7 @@ public class ScheduleModuleEngine extends ModuleEngine {
//设置定时管理器 //设置定时管理器
this.config = application.getAppConfig().getAnyValue("schedule"); this.config = application.getAppConfig().getAnyValue("schedule");
this.scheduleManager = createManager(this.config); this.scheduleManager = createManager(this.config);
if (this.config != null && !application.isCompileMode()) { if (!application.isCompileMode()) {
this.resourceFactory.inject(this.scheduleManager); this.resourceFactory.inject(this.scheduleManager);
if (this.scheduleManager instanceof Service) { if (this.scheduleManager instanceof Service) {
((Service) this.scheduleManager).init(this.config); ((Service) this.scheduleManager).init(this.config);