优化schedule
This commit is contained in:
@@ -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,33 +99,22 @@ public class ScheduleManagerService implements ScheduleManager, Service {
|
|||||||
conf = AnyValue.create();
|
conf = AnyValue.create();
|
||||||
}
|
}
|
||||||
this.config = conf;
|
this.config = conf;
|
||||||
init();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void init() {
|
|
||||||
if (inited.compareAndSet(false, true)) {
|
|
||||||
this.enabled = config.getBoolValue("enabled", true);
|
this.enabled = config.getBoolValue("enabled", true);
|
||||||
if (this.enabled) {
|
if (this.enabled) {
|
||||||
if (this.propertyFunc == null && application != null) {
|
if (this.propertyFunc == null && application != null) {
|
||||||
UnaryOperator<String> func = application.getEnvironment()::getPropertyValue;
|
UnaryOperator<String> func = application.getEnvironment()::getPropertyValue;
|
||||||
this.propertyFunc = func;
|
this.propertyFunc = func;
|
||||||
}
|
}
|
||||||
this.scheduler = new ScheduledThreadPoolExecutor(Utility.cpus(), Utility.newThreadFactory("Scheduled-Task-Thread-%s"));
|
this.scheduler = new ScheduledThreadPoolExecutor(Utility.cpus(), Utility.newThreadFactory("Redkale-Scheduled-Task-Thread-%s"));
|
||||||
this.scheduler.setRemoveOnCancelPolicy(true);
|
this.scheduler.setRemoveOnCancelPolicy(true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void destroy(AnyValue conf) {
|
public void destroy(AnyValue conf) {
|
||||||
if (scheduler != null) {
|
if (scheduler != null) {
|
||||||
scheduler.shutdown();
|
scheduler.shutdown();
|
||||||
}
|
}
|
||||||
inited.set(false);
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean isInited() {
|
|
||||||
return inited.get();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -151,20 +137,21 @@ 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);
|
tasks.put(method.getName(), task);
|
||||||
RedkaleClassLoader.putReflectionMethod(clazz.getName(), method);
|
RedkaleClassLoader.putReflectionMethod(clazz.getName(), method);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
} while ((clazz = clazz.getSuperclass()) != Object.class);
|
} while ((clazz = clazz.getSuperclass()) != Object.class);
|
||||||
//开始执行定时任务
|
//开始执行定时任务
|
||||||
if (enabled && !tasks.isEmpty()) {
|
if (enabled && !tasks.isEmpty()) {
|
||||||
@@ -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()) {
|
||||||
}
|
|
||||||
}
|
|
||||||
if (entry == null) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
refTaskMap.remove(entry.getKey());
|
|
||||||
for (ScheduledTask task : entry.getValue()) {
|
|
||||||
task.cancel();
|
task.cancel();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
} finally {
|
} finally {
|
||||||
lock.unlock();
|
lock.unlock();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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);
|
||||||
|
|||||||
Reference in New Issue
Block a user