1. import java.util.Iterator;
    2. import java.util.Objects;
    3. import java.util.concurrent.*;
    4. import java.util.function.Consumer;
    5. import java.util.function.Function;
    6. /**
    7. * @author sunzsh
    8. * @param <T> 延迟业务数据的类型
    9. */
    10. public abstract class ContinuousDelayBaseService<T> {
    11. private static final DelayQueue<DelayBean> queue = new DelayQueue<>();
    12. private static ExecutorService asyncDoneExecutor = Executors.newCachedThreadPool();
    13. private static final Thread thread = new Thread(() -> {
    14. Thread.currentThread().setName("ContinuousDelay-main");
    15. while (true) {
    16. try {
    17. DelayBean delayBean = queue.take();
    18. asyncDoneExecutor.submit(() -> {
    19. Thread.currentThread().setName("ContinuousDelay-" + delayBean.id);
    20. delayBean.done.accept(delayBean.data);
    21. });
    22. } catch (InterruptedException e) {
    23. e.printStackTrace();
    24. }
    25. }
    26. });
    27. static {
    28. thread.start();
    29. }
    30. /**
    31. * 最后一次超时回调
    32. * @param value
    33. */
    34. protected abstract void done(T value);
    35. /**
    36. * @param delayMs 延迟毫秒数
    37. * @param callback4InitOrIfExists 回调函数:初次调用时,参数为空;重复调用时,参数为上次调用的返回值
    38. */
    39. public synchronized void delay(Long delayMs, Function<T, T> callback4InitOrIfExists) {
    40. DelayBean delayBean = this.getDelayBean();
    41. if (delayBean == null) {
    42. T newValue = callback4InitOrIfExists != null ? callback4InitOrIfExists.apply(null) : null;
    43. delayBean = new DelayBean(this.getClass().getName(), delayMs, newValue);
    44. delayBean.done = (value) -> this.done((T) value);
    45. } else {
    46. deleteById(this.getClass().getName());
    47. if (callback4InitOrIfExists != null) {
    48. Object newValue = callback4InitOrIfExists.apply((T) delayBean.data);
    49. delayBean.data = newValue;
    50. }
    51. delayBean.setDelay(delayMs);
    52. }
    53. queue.offer(delayBean);
    54. }
    55. /**
    56. *获取延时队列数据
    57. */
    58. private DelayBean getDelayBean() {
    59. Iterator<DelayBean> iterator = queue.iterator();
    60. while (iterator.hasNext()) {
    61. DelayBean next = iterator.next();
    62. if (Objects.equals(next.id, this.getClass().getName())) {
    63. return next;
    64. }
    65. }
    66. return null;
    67. }
    68. private boolean deleteById(String id) {
    69. return queue.remove(new DelayBean(id));
    70. }
    71. protected static class DelayBean implements Delayed {
    72. private String id;
    73. long delay; //延迟时间
    74. long expire; //到期时间
    75. Object data; //数据
    76. Consumer<Object> done;
    77. protected void setDelay(long delay) {
    78. expire = System.currentTimeMillis() + delay;
    79. }
    80. public DelayBean(String id) {
    81. this.id = id;
    82. }
    83. public DelayBean(String id, long delay, Object data){
    84. this.id = id;
    85. this.delay = delay;
    86. setDelay(delay); //到期时间 = 当前时间+延迟时间
    87. this.data = data;
    88. }
    89. /**
    90. * 需要实现的接口,获得延迟时间 用过期时间-当前时间
    91. * @param unit
    92. * @return
    93. */
    94. @Override
    95. public long getDelay(TimeUnit unit) {
    96. return unit.convert(this.expire - System.currentTimeMillis(), TimeUnit.MILLISECONDS);
    97. }
    98. /**
    99. * 用于延迟队列内部比较排序 当前时间的延迟时间 - 比较对象的延迟时间
    100. * @param o
    101. * @return
    102. */
    103. @Override
    104. public int compareTo(Delayed o) {
    105. return (int) (this.getDelay(TimeUnit.MILLISECONDS) - o.getDelay(TimeUnit.MILLISECONDS));
    106. }
    107. @Override
    108. public int hashCode() {
    109. return this.getClass().hashCode();
    110. }
    111. @Override
    112. public boolean equals(Object obj) {
    113. if (obj == null)
    114. return false;
    115. if (!(obj instanceof DelayBean)) {
    116. return false;
    117. }
    118. return Objects.equals(this.id, ((DelayBean)obj).id);
    119. }
    120. }
    121. }