项目作者: northernsong

项目描述 :
基于spring schedule的分布式拓展
高级语言: Java
项目地址: git://github.com/northernsong/schedule.git
创建时间: 2019-12-06T01:32:48Z
项目社区:https://github.com/northernsong/schedule

开源协议:Apache License 2.0

下载


schedule

基于spring schedule的分布式拓展

分布式执行

继承ScheduledAnnotationBeanPostProcessor, 通过重新top.zeroone.job.annotation.JobScheduledAnnotationBeanPostProcessor.createRunnable方法,在任务执行时通过redis锁判断是否执行

  1. @Override
  2. public void run() {
  3. if (this.scheduledLock.ifLockDoNot()) {
  4. if (this.timeLock != null) {
  5. // 有些任务,短时间内不必重复执行.因没有等待执行,所以没有显式的解锁
  6. if (!this.timeLock.tryLock()) {
  7. log.debug("任务{}, time tryLock return false, 停止执行", this.name);
  8. return;
  9. }
  10. }
  11. if (this.lock.tryLock()) {
  12. try {
  13. invokeMethod();
  14. } finally {
  15. this.lock.unlock();
  16. }
  17. } else {
  18. if (log.isDebugEnabled()) {
  19. log.debug("任务{}, tryLock return false, 停止执行", this.name);
  20. }
  21. }
  22. } else {
  23. invokeMethod();
  24. }
  25. }

实时查看任务

依赖spring-boot-actuator,自定义端点来查看和控制任务的执行
top.zeroone.job.manager.JobManagerSchedulingConfigurer

spring-boot-actuator 有原生的监控schedule任务,但没有停止

feature

  1. 任务执行参数
  2. 任务可以托管admin来统一控制管理,而不是在执行的服务上配置执行时间
  3. 在任务取消后,可以再重新执行
  4. 任务的执行状态可以查看(任务的feature)

设计