Skip to content

Commit

Permalink
🐳 #291 简化相关方法名
Browse files Browse the repository at this point in the history
  • Loading branch information
iohao committed Sep 6, 2024
1 parent 0e00624 commit 31da7b8
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import java.time.Duration;

/**
* 轻量可控的延时任务,任务到达指定时间后会执行、任务可取消、任务可增加延时时间
* <a href="https://www.yuque.com/iohao/game/nykaacfzg4h1ynii">轻量可控的延时任务</a>,任务到达指定时间后会执行、任务可取消、任务可增加延时时间
*
* @author 渔民小镇
* @date 2024-09-01
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
*/
@UtilityClass
public class DelayTaskKit {

/** 轻量可控延时任务域接口 */
@Getter
DelayTaskRegion delayTaskRegion = new SimpleDelayTaskRegion();

Expand All @@ -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) {
Expand All @@ -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);
}

/**
Expand All @@ -70,8 +70,8 @@ public void cancelDelayTask(String taskId) {
* @param taskId taskId
* @return Optional DelayTask
*/
public Optional<DelayTask> optionalDelayTask(String taskId) {
return delayTaskRegion.optionalDelayTask(taskId);
public Optional<DelayTask> optional(String taskId) {
return delayTaskRegion.optional(taskId);
}

/**
Expand All @@ -80,8 +80,8 @@ public Optional<DelayTask> optionalDelayTask(String taskId) {
* @param taskId taskId
* @param consumer 给定操作
*/
public void ifPresentDelayTask(String taskId, Consumer<DelayTask> consumer) {
DelayTaskKit.optionalDelayTask(taskId).ifPresent(consumer);
public void ifPresent(String taskId, Consumer<DelayTask> consumer) {
DelayTaskKit.optional(taskId).ifPresent(consumer);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,21 @@ public interface DelayTaskRegion {
* @param taskId taskId
* @return DelayTask Optional
*/
Optional<DelayTask> optionalDelayTask(String taskId);
Optional<DelayTask> optional(String taskId);

/**
* 根据 taskId 取消可控延时任务的执行。
*
* @param taskId taskId
*/
void cancelDelayTask(String taskId);
void cancel(String taskId);

/**
* 统计当前延时任务的数量
*
* @return 当前延时任务数量
*/
int countDelayTask();
int count();

/**
* 创建一个可控的延时任务,并设置任务监听回调。
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -99,7 +99,7 @@ private void extractedFlowTaskListener(DelayTaskExecutor task) {
try {
// 移除不活跃的延时任务
if (!task.isActive()) {
delayTaskRegion.cancelDelayTask(task.getTaskId());
this.delayTaskRegion.cancel(task.getTaskId());
return;
}

Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -193,7 +193,7 @@ public Executor getExecutor() {
@Override
public DelayTask task() {
if (this.isActive()) {
delayTaskRegion.runDelayTask(this);
this.delayTaskRegion.runDelayTask(this);
}

return this;
Expand All @@ -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() +
'}';
}
Expand All @@ -222,7 +222,7 @@ class SimpleDelayTaskRegion implements DelayTaskRegion, DelayTaskRegionEnhance {

@Override
public void forEach(Consumer<DelayTaskExecutor> consumer) {
taskMap.values().forEach(consumer);
this.taskMap.values().forEach(consumer);
}

@Override
Expand All @@ -231,20 +231,21 @@ public void runDelayTask(DelayTaskExecutor delayTaskExecutor) {
}

@Override
public Optional<DelayTask> optionalDelayTask(String taskId) {
return Optional.ofNullable(this.taskMap.get(taskId));
public Optional<DelayTask> 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();
}

Expand Down Expand Up @@ -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
Expand All @@ -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 +
"} ";
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@
*
* log.info("0.5 秒后, 因为满足某个业务条件, 不想执行定时任务了");
* TimeUnit.MILLISECONDS.sleep(500);
* DelayTaskKit.cancelDelayTask(taskId); // 通过 taskId 取消任务
* DelayTaskKit.cancel(taskId); // 通过 taskId 取消任务
* }
*
* @Test
Expand All @@ -151,14 +151,14 @@
* .task(); // 启动任务
*
* // 在后续的业务中,可以通过 taskId 查找该延时任务
* Optional<DelayTask> optionalDelayTask = DelayTaskKit.optionalDelayTask(newTaskId);
* Optional<DelayTask> 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 秒的延时时间
* });
* }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -146,15 +146,15 @@ public void cancelDelayTask() throws InterruptedException {
// 启动任务
.task();

Assert.assertEquals(1, delayTaskRegion.countDelayTask());
Assert.assertEquals(1, delayTaskRegion.count());

log.info("0.5 秒后, 因为满足某个业务条件, 不想执行定时任务了");
TimeUnit.MILLISECONDS.sleep(500);
// 取消任务
delayTask.cancel();

Assert.assertFalse(delayTask.isActive());
Assert.assertEquals(0, delayTaskRegion.countDelayTask());
Assert.assertEquals(0, delayTaskRegion.count());

// -----------取消 - 延时任务;通过 taskId 来取消-----------

Expand All @@ -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
Expand All @@ -189,14 +189,14 @@ public void optionalDelayTask() {
.task();

// 在后续的业务中,可以通过 taskId 查找该延时任务
Optional<DelayTask> optionalDelayTask = DelayTaskKit.optionalDelayTask(newTaskId);
Optional<DelayTask> 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 秒的延时时间
});
}
Expand Down

0 comments on commit 31da7b8

Please sign in to comment.