diff --git a/alis-component/src/main/java/org/alis/aliscomponent/controller/DelayController.java b/alis-component/src/main/java/org/alis/aliscomponent/controller/DelayController.java index 20af719..c3efd6b 100644 --- a/alis-component/src/main/java/org/alis/aliscomponent/controller/DelayController.java +++ b/alis-component/src/main/java/org/alis/aliscomponent/controller/DelayController.java @@ -1,9 +1,8 @@ package org.alis.aliscomponent.controller; import lombok.extern.slf4j.Slf4j; +import org.alis.aliscomponent.executor.CacheEventDelayExecutor; import org.alis.aliscomponent.executor.DelayExecutor; -import org.alis.aliscomponent.executor.DelayQueueDelayExecutor; -import org.alis.aliscomponent.utils.DelayExecutors; import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; @@ -22,7 +21,7 @@ import java.util.concurrent.TimeUnit; @Slf4j public class DelayController { - private final DelayExecutor delayExecutor = new DelayQueueDelayExecutor(); + private final DelayExecutor delayExecutor = new CacheEventDelayExecutor(); private final DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); @@ -30,8 +29,8 @@ public class DelayController { public String delay() { delayExecutor.start(); String format = timeFormatter.format(LocalDateTime.now()); - log.info(format); - delayExecutor.scheduleWithFixedDelay(() -> System.out.println(timeFormatter.format(LocalDateTime.now()) + " : 哈哈哈哈!!--!!"), 0L, 3L, TimeUnit.SECONDS); + delayExecutor.scheduled(() -> System.out.println(format + " : 哈哈哈哈!!--!!"), + 3L, TimeUnit.SECONDS); return "ok"; } diff --git a/alis-component/src/main/java/org/alis/aliscomponent/executor/CacheEventDelayExecutor.java b/alis-component/src/main/java/org/alis/aliscomponent/executor/CacheEventDelayExecutor.java new file mode 100644 index 0000000..022c05e --- /dev/null +++ b/alis-component/src/main/java/org/alis/aliscomponent/executor/CacheEventDelayExecutor.java @@ -0,0 +1,73 @@ +package org.alis.aliscomponent.executor; + +import cn.hutool.cache.CacheUtil; +import cn.hutool.core.thread.ThreadUtil; +import cn.hutool.core.util.IdUtil; +import com.github.benmanes.caffeine.cache.Cache; +import com.github.benmanes.caffeine.cache.Caffeine; +import com.github.benmanes.caffeine.cache.RemovalListener; +import com.github.benmanes.caffeine.cache.Scheduler; +import lombok.Getter; +import lombok.ToString; + +import java.util.Objects; +import java.util.concurrent.TimeUnit; + +/** + * @author robin + * @date 2022/1/7 18:14 + */ +public class CacheEventDelayExecutor implements EventDelayExecutor { + + + + private final Cache CACHE = Caffeine.newBuilder().maximumSize(10000L).expireAfterWrite(20L, TimeUnit.MILLISECONDS) + .executor(ThreadUtil.newExecutor()) + .scheduler(Scheduler.systemScheduler()) + .removalListener((RemovalListener) (key, value, cause) -> { + if (Objects.nonNull(value) && value.isExpire()) { + value.getTask().run(); + }else{ + rePut(key,value); + } + }).build(); + + + + + @Override + public void scheduled(Runnable task, Long delay, TimeUnit timeUnit) { + CACHE.put(IdUtil.randomUUID(), new EventTask(task, timeUnit.toMillis(delay))); + } + + @Override + public void start() { + + } + + @Override + public void stop() { + CACHE.invalidateAll(); + } + + @ToString + public static class EventTask { + @Getter + private final Runnable task; + + private final long expire; + + public EventTask(Runnable runnable, long expire) { + this.task = runnable; + this.expire = System.currentTimeMillis() + expire; + } + + public boolean isExpire() { + return expire - System.currentTimeMillis() <= 0; + } + } + + private void rePut(String key ,EventTask task){ + CACHE.put(key,task); + } +} diff --git a/alis-component/src/main/java/org/alis/aliscomponent/executor/EventDelayExecutor.java b/alis-component/src/main/java/org/alis/aliscomponent/executor/EventDelayExecutor.java new file mode 100644 index 0000000..9708d11 --- /dev/null +++ b/alis-component/src/main/java/org/alis/aliscomponent/executor/EventDelayExecutor.java @@ -0,0 +1,40 @@ +package org.alis.aliscomponent.executor; + +import java.util.concurrent.TimeUnit; + +/** + * @author robin + * @date 2022/1/7 18:10 + */ +public interface EventDelayExecutor extends DelayExecutor { + + /** + * 不建议使用 + * @deprecated + * @param command 具体运行的任务 + * @param initialDelay 执行延迟的时间 + * @param delay 延迟时间 + * @param unit 时间单位 + */ + @Override + @Deprecated + default void scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit) { + throw new UnsupportedOperationException("event pattern not support"); + } + + /** + * 不建议使用 + * @deprecated + * @param command 具体运行的任务 + * @param initialDelay 执行延迟的时间 + * @param delay 延迟时间 + * @param unit 时间单位 + */ + @Override + @Deprecated + default void scheduleAtFixedRate(Runnable command, long initialDelay, long delay, TimeUnit unit) { + throw new UnsupportedOperationException("event pattern not support"); + } + + +} diff --git a/alis-component/src/main/java/org/alis/aliscomponent/utils/DelayExecutors.java b/alis-component/src/main/java/org/alis/aliscomponent/utils/DelayExecutors.java index 2ff64cc..822e34b 100644 --- a/alis-component/src/main/java/org/alis/aliscomponent/utils/DelayExecutors.java +++ b/alis-component/src/main/java/org/alis/aliscomponent/utils/DelayExecutors.java @@ -1,7 +1,7 @@ package org.alis.aliscomponent.utils; -import io.netty.util.HashedWheelTimer; -import io.netty.util.Timer; +import org.alis.aliscomponent.executor.DelayExecutor; +import org.alis.aliscomponent.executor.NettyTimerWheelDelayExecutor; import java.util.concurrent.TimeUnit; @@ -13,9 +13,15 @@ public final class DelayExecutors { private DelayExecutors() { } - private static final Timer TIMER = new HashedWheelTimer(); + private static final DelayExecutor EXECUTOR = new NettyTimerWheelDelayExecutor(); + + + static { + EXECUTOR.start(); + } public static void scheduled(Runnable runnable, Long time, TimeUnit timeUnit) { - TIMER.newTimeout(timeout -> runnable.run(), time, timeUnit); + EXECUTOR.scheduled(runnable, time, timeUnit); } + }