diff --git a/common/common-micro-kit/src/main/java/com/iohao/game/common/kit/concurrent/timer/delay/DelayTask.java b/common/common-micro-kit/src/main/java/com/iohao/game/common/kit/concurrent/timer/delay/DelayTask.java index bf36b133..6f946180 100644 --- a/common/common-micro-kit/src/main/java/com/iohao/game/common/kit/concurrent/timer/delay/DelayTask.java +++ b/common/common-micro-kit/src/main/java/com/iohao/game/common/kit/concurrent/timer/delay/DelayTask.java @@ -23,7 +23,7 @@ import java.time.Duration; /** - * 轻量可控的延时任务,任务到达指定时间后会执行、任务可取消、任务可增加延时时间 + * 轻量可控的延时任务,任务到达指定时间后会执行、任务可取消、任务可增加延时时间 * * @author 渔民小镇 * @date 2024-09-01 diff --git a/common/common-micro-kit/src/main/java/com/iohao/game/common/kit/concurrent/timer/delay/DelayTaskKit.java b/common/common-micro-kit/src/main/java/com/iohao/game/common/kit/concurrent/timer/delay/DelayTaskKit.java index d5a48c31..78e395bb 100644 --- a/common/common-micro-kit/src/main/java/com/iohao/game/common/kit/concurrent/timer/delay/DelayTaskKit.java +++ b/common/common-micro-kit/src/main/java/com/iohao/game/common/kit/concurrent/timer/delay/DelayTaskKit.java @@ -35,7 +35,7 @@ */ @UtilityClass public class DelayTaskKit { - + /** 轻量可控延时任务域接口 */ @Getter DelayTaskRegion delayTaskRegion = new SimpleDelayTaskRegion(); @@ -47,7 +47,7 @@ public class DelayTaskKit { public void setDelayTaskRegion(DelayTaskRegion delayTaskRegion) { Objects.requireNonNull(delayTaskRegion); - DelayTaskRegion delayTaskRegionOld = DelayTaskKit.delayTaskRegion; + var delayTaskRegionOld = DelayTaskKit.delayTaskRegion; DelayTaskKit.delayTaskRegion = delayTaskRegion; if (delayTaskRegionOld instanceof DelayTaskRegionEnhance stop) { @@ -60,8 +60,8 @@ public void setDelayTaskRegion(DelayTaskRegion delayTaskRegion) { * * @param taskId taskId */ - public void cancelDelayTask(String taskId) { - delayTaskRegion.cancelDelayTask(taskId); + public void cancel(String taskId) { + delayTaskRegion.cancel(taskId); } /** @@ -70,8 +70,8 @@ public void cancelDelayTask(String taskId) { * @param taskId taskId * @return Optional DelayTask */ - public Optional optionalDelayTask(String taskId) { - return delayTaskRegion.optionalDelayTask(taskId); + public Optional optional(String taskId) { + return delayTaskRegion.optional(taskId); } /** @@ -80,8 +80,8 @@ public Optional optionalDelayTask(String taskId) { * @param taskId taskId * @param consumer 给定操作 */ - public void ifPresentDelayTask(String taskId, Consumer consumer) { - DelayTaskKit.optionalDelayTask(taskId).ifPresent(consumer); + public void ifPresent(String taskId, Consumer consumer) { + DelayTaskKit.optional(taskId).ifPresent(consumer); } /** diff --git a/common/common-micro-kit/src/main/java/com/iohao/game/common/kit/concurrent/timer/delay/DelayTaskRegion.java b/common/common-micro-kit/src/main/java/com/iohao/game/common/kit/concurrent/timer/delay/DelayTaskRegion.java index 0857f31a..b53ee663 100644 --- a/common/common-micro-kit/src/main/java/com/iohao/game/common/kit/concurrent/timer/delay/DelayTaskRegion.java +++ b/common/common-micro-kit/src/main/java/com/iohao/game/common/kit/concurrent/timer/delay/DelayTaskRegion.java @@ -37,21 +37,21 @@ public interface DelayTaskRegion { * @param taskId taskId * @return DelayTask Optional */ - Optional optionalDelayTask(String taskId); + Optional optional(String taskId); /** * 根据 taskId 取消可控延时任务的执行。 * * @param taskId taskId */ - void cancelDelayTask(String taskId); + void cancel(String taskId); /** * 统计当前延时任务的数量 * * @return 当前延时任务数量 */ - int countDelayTask(); + int count(); /** * 创建一个可控的延时任务,并设置任务监听回调。 diff --git a/common/common-micro-kit/src/main/java/com/iohao/game/common/kit/concurrent/timer/delay/InternalDelayAbout.java b/common/common-micro-kit/src/main/java/com/iohao/game/common/kit/concurrent/timer/delay/InternalDelayAbout.java index 41c84492..2a9a6842 100644 --- a/common/common-micro-kit/src/main/java/com/iohao/game/common/kit/concurrent/timer/delay/InternalDelayAbout.java +++ b/common/common-micro-kit/src/main/java/com/iohao/game/common/kit/concurrent/timer/delay/InternalDelayAbout.java @@ -84,8 +84,8 @@ final class DelayIntervalTaskListener implements IntervalTaskListener { @Override public void onUpdate() { - delayTaskRegion.forEach(task -> { - Executor executor = task.getExecutor(); + this.delayTaskRegion.forEach(task -> { + var executor = task.getExecutor(); if (Objects.nonNull(executor)) { executor.execute(() -> extractedFlowTaskListener(task)); @@ -99,7 +99,7 @@ private void extractedFlowTaskListener(DelayTaskExecutor task) { try { // 移除不活跃的延时任务 if (!task.isActive()) { - delayTaskRegion.cancelDelayTask(task.getTaskId()); + this.delayTaskRegion.cancel(task.getTaskId()); return; } @@ -139,15 +139,15 @@ public boolean isActive() { @Override public void cancel() { - if (isActive()) { + if (this.isActive()) { this.active.set(false); - this.delayTaskRegion.cancelDelayTask(taskId); + this.delayTaskRegion.cancel(this.taskId); } } @Override public long getMillis() { - long sum = this.timeMillis.sum(); + var sum = this.timeMillis.sum(); return sum < 0 ? 0 : sum; } @@ -193,7 +193,7 @@ public Executor getExecutor() { @Override public DelayTask task() { if (this.isActive()) { - delayTaskRegion.runDelayTask(this); + this.delayTaskRegion.runDelayTask(this); } return this; @@ -202,8 +202,8 @@ public DelayTask task() { @Override public String toString() { return "SimpleDelayTask{" + - "taskId='" + taskId + '\'' + - ", active=" + active + + "taskId='" + this.taskId + '\'' + + ", active=" + this.active + ", timeMillis=" + getMillis() + '}'; } @@ -222,7 +222,7 @@ class SimpleDelayTaskRegion implements DelayTaskRegion, DelayTaskRegionEnhance { @Override public void forEach(Consumer consumer) { - taskMap.values().forEach(consumer); + this.taskMap.values().forEach(consumer); } @Override @@ -231,20 +231,21 @@ public void runDelayTask(DelayTaskExecutor delayTaskExecutor) { } @Override - public Optional optionalDelayTask(String taskId) { - return Optional.ofNullable(this.taskMap.get(taskId)); + public Optional optional(String taskId) { + var task = this.taskMap.get(taskId); + return Optional.ofNullable(task); } @Override - public void cancelDelayTask(String taskId) { - var task = taskMap.remove(taskId); + public void cancel(String taskId) { + var task = this.taskMap.remove(taskId); if (Objects.nonNull(task) && task.isActive()) { task.cancel(); } } @Override - public int countDelayTask() { + public int count() { return this.taskMap.size(); } @@ -279,7 +280,7 @@ final class DebugDelayTask extends SimpleDelayTask { @Override public void onUpdate() { super.onUpdate(); - log.info("剩余任务数量 {},{}", this.delayTaskRegion.countDelayTask(), this); + log.info("剩余任务数量 {},{}", this.delayTaskRegion.count(), this); } @Override @@ -293,10 +294,10 @@ public boolean triggerUpdate() { @Override public String toString() { return "DebugDelayTask{" + - "taskId='" + taskId + '\'' + - ", active=" + active + - ", timeMillis=" + timeMillis + - ", sumMillis=" + sumMillis + + "taskId='" + this.taskId + '\'' + + ", active=" + this.active + + ", timeMillis=" + this.timeMillis + + ", sumMillis=" + this.sumMillis + "} "; } } diff --git a/common/common-micro-kit/src/main/java/com/iohao/game/common/kit/concurrent/timer/delay/package-info.java b/common/common-micro-kit/src/main/java/com/iohao/game/common/kit/concurrent/timer/delay/package-info.java index 667fbade..c634d472 100644 --- a/common/common-micro-kit/src/main/java/com/iohao/game/common/kit/concurrent/timer/delay/package-info.java +++ b/common/common-micro-kit/src/main/java/com/iohao/game/common/kit/concurrent/timer/delay/package-info.java @@ -135,7 +135,7 @@ * * log.info("0.5 秒后, 因为满足某个业务条件, 不想执行定时任务了"); * TimeUnit.MILLISECONDS.sleep(500); - * DelayTaskKit.cancelDelayTask(taskId); // 通过 taskId 取消任务 + * DelayTaskKit.cancel(taskId); // 通过 taskId 取消任务 * } * * @Test @@ -151,14 +151,14 @@ * .task(); // 启动任务 * * // 在后续的业务中,可以通过 taskId 查找该延时任务 - * Optional optionalDelayTask = DelayTaskKit.optionalDelayTask(newTaskId); + * Optional optionalDelayTask = DelayTaskKit.optional(newTaskId); * if (optionalDelayTask.isPresent()) { * DelayTask delayTask = optionalDelayTask.get(); * log.info("{}", delayTask); * } * * // 通过 taskId 查找延时任务,存在则执行给定逻辑 - * DelayTaskKit.ifPresentDelayTask(newTaskId, delayTask -> { + * DelayTaskKit.ifPresent(newTaskId, delayTask -> { * delayTask.plusTimeMillis(500); // 增加 0.5 秒的延时时间 * }); * } diff --git a/common/common-micro-kit/src/test/java/com/iohao/game/common/kit/concurrent/timer/delay/DelayTaskTest.java b/common/common-micro-kit/src/test/java/com/iohao/game/common/kit/concurrent/timer/delay/DelayTaskTest.java index 47b66a66..416f54d1 100644 --- a/common/common-micro-kit/src/test/java/com/iohao/game/common/kit/concurrent/timer/delay/DelayTaskTest.java +++ b/common/common-micro-kit/src/test/java/com/iohao/game/common/kit/concurrent/timer/delay/DelayTaskTest.java @@ -31,7 +31,7 @@ public void setUp() { @After public void tearDown() throws Exception { TimeUnit.SECONDS.sleep(3); - log.info("--------剩余任务数量 {}", delayTaskRegion.countDelayTask()); + log.info("--------剩余任务数量 {}", delayTaskRegion.count()); } @Test @@ -146,7 +146,7 @@ public void cancelDelayTask() throws InterruptedException { // 启动任务 .task(); - Assert.assertEquals(1, delayTaskRegion.countDelayTask()); + Assert.assertEquals(1, delayTaskRegion.count()); log.info("0.5 秒后, 因为满足某个业务条件, 不想执行定时任务了"); TimeUnit.MILLISECONDS.sleep(500); @@ -154,7 +154,7 @@ public void cancelDelayTask() throws InterruptedException { delayTask.cancel(); Assert.assertFalse(delayTask.isActive()); - Assert.assertEquals(0, delayTaskRegion.countDelayTask()); + Assert.assertEquals(0, delayTaskRegion.count()); // -----------取消 - 延时任务;通过 taskId 来取消----------- @@ -168,14 +168,14 @@ public void cancelDelayTask() throws InterruptedException { // 启动任务 .task(); - Assert.assertEquals(1, delayTaskRegion.countDelayTask()); + Assert.assertEquals(1, delayTaskRegion.count()); log.info("0.5 秒后, 因为满足某个业务条件, 不想执行定时任务了"); TimeUnit.MILLISECONDS.sleep(500); // 通过 taskId 取消任务 - DelayTaskKit.cancelDelayTask(taskId); + DelayTaskKit.cancel(taskId); - Assert.assertEquals(0, delayTaskRegion.countDelayTask()); + Assert.assertEquals(0, delayTaskRegion.count()); } @Test @@ -189,14 +189,14 @@ public void optionalDelayTask() { .task(); // 在后续的业务中,可以通过 taskId 查找该延时任务 - Optional optionalDelayTask = DelayTaskKit.optionalDelayTask(newTaskId); + Optional optionalDelayTask = DelayTaskKit.optional(newTaskId); if (optionalDelayTask.isPresent()) { DelayTask delayTask = optionalDelayTask.get(); log.info("{}", delayTask); } // 通过 taskId 查找延时任务,存在则执行给定逻辑 - DelayTaskKit.ifPresentDelayTask(newTaskId, delayTask -> { + DelayTaskKit.ifPresent(newTaskId, delayTask -> { delayTask.plusTimeMillis(500); // 增加 0.5 秒的延时时间 }); }