JobManager.java 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package com.cyksj.task;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.core.task.TaskRejectedException;
  4. import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
  5. import org.springframework.scheduling.support.TaskUtils;
  6. import org.springframework.stereotype.Component;
  7. import java.util.concurrent.RejectedExecutionException;
  8. import java.util.concurrent.ScheduledExecutorService;
  9. import java.util.concurrent.ScheduledFuture;
  10. import java.util.concurrent.TimeUnit;
  11. /**
  12. * @author valor.
  13. * @date 2019-07-25 16:30
  14. */
  15. @Component
  16. public class JobManager {
  17. @Autowired
  18. private ThreadPoolTaskScheduler taskScheduler;
  19. /**
  20. * 定时任务管理
  21. * todo oom
  22. */
  23. // private static volatile Map<String, ScheduledFuture<?>> jobs;
  24. private static volatile JobManager jobManager;
  25. private JobManager() { }
  26. public static JobManager getInstance() {
  27. if (null == jobManager) {
  28. synchronized (JobManager.class) {
  29. if (null == jobManager) {
  30. jobManager = new JobManager();
  31. }
  32. }
  33. }
  34. return jobManager;
  35. }
  36. // private Map<String, ScheduledFuture<?>> getJobs() {
  37. // if (null == jobs) {
  38. // synchronized (JobManager.class) {
  39. // if (null == jobs) {
  40. // jobs = new ConcurrentHashMap<>(16);
  41. // }
  42. // }
  43. // }
  44. // return jobs;
  45. // }
  46. /**
  47. * 任务监视器
  48. */
  49. // public List<String> monitor() {
  50. // Set<Map.Entry<String, ScheduledFuture<?>>> entrySet = this.getJobs().entrySet();
  51. //
  52. // List<String> list = new ArrayList<>(entrySet.size());
  53. //
  54. // entrySet.removeIf(entry -> {
  55. // ScheduledFuture<?> value = entry.getValue();
  56. // if (null == value || value.isCancelled() || value.isDone()) {
  57. // return true;
  58. // }
  59. // list.add(entry.getKey());
  60. // return false;
  61. // });
  62. //
  63. // return list;
  64. // }
  65. /**
  66. * 判断任务是否存在
  67. */
  68. // public boolean hasJob(String jobName) {
  69. // return this.getJobs().containsKey(jobName) && null != this.getJobs().get(jobName);
  70. // }
  71. /**
  72. * 取消任务
  73. */
  74. // public void cancel(String jobName) {
  75. // boolean f = this.hasJob(jobName);
  76. // if (f) {
  77. // this.getJobs().get(jobName).cancel(false);
  78. // this.getJobs().remove(jobName);
  79. // }
  80. // }
  81. /**
  82. * 添加任务
  83. */
  84. public void addJob(long startTime, Runnable task) {
  85. ScheduledExecutorService executor = this.taskScheduler.getScheduledExecutor();
  86. try {
  87. Runnable runnable = TaskUtils.decorateTaskWithErrorHandler(task, TaskUtils.LOG_AND_PROPAGATE_ERROR_HANDLER, false);
  88. ScheduledFuture<?> future = executor.schedule(runnable, startTime, TimeUnit.MILLISECONDS);
  89. // this.getJobs().put(jobName, future);
  90. } catch (RejectedExecutionException ex) {
  91. throw new TaskRejectedException("Executor [" + executor + "] did not accept task: " + task, ex);
  92. }
  93. }
  94. }