基于spring schedule的分布式拓展
基于spring schedule的分布式拓展
继承ScheduledAnnotationBeanPostProcessor
, 通过重新top.zeroone.job.annotation.JobScheduledAnnotationBeanPostProcessor.createRunnable
方法,在任务执行时通过redis
锁判断是否执行
@Override
public void run() {
if (this.scheduledLock.ifLockDoNot()) {
if (this.timeLock != null) {
// 有些任务,短时间内不必重复执行.因没有等待执行,所以没有显式的解锁
if (!this.timeLock.tryLock()) {
log.debug("任务{}, time tryLock return false, 停止执行", this.name);
return;
}
}
if (this.lock.tryLock()) {
try {
invokeMethod();
} finally {
this.lock.unlock();
}
} else {
if (log.isDebugEnabled()) {
log.debug("任务{}, tryLock return false, 停止执行", this.name);
}
}
} else {
invokeMethod();
}
}
依赖spring-boot-actuator
,自定义端点来查看和控制任务的执行top.zeroone.job.manager.JobManagerSchedulingConfigurer
spring-boot-actuator
有原生的监控schedule任务,但没有停止
admin
来统一控制管理,而不是在执行的服务上配置执行时间