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 81f90d1b5..667fbade2 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 @@ -17,7 +17,7 @@ * along with this program. If not, see . */ /** - * 工具相关 - 轻量可控的延时任务,任务到达指定时间后会执行、任务可取消、任务可增加或减少延时时间、任务可被覆盖、可设置任务监听回调,相关参考轻量可控的延时任务。 + * 工具相关 - 轻量可控的延时任务,任务到达指定时间后会执行、任务可取消、任务可增加或减少延时时间、任务可被覆盖、可设置任务监听回调。 *

* 轻量可控的延时任务 - 简介 *

diff --git a/pom.xml b/pom.xml
index e1a3dbb49..3dec8d562 100644
--- a/pom.xml
+++ b/pom.xml
@@ -17,6 +17,7 @@
 
     
         
+        common/common-micro-kit
         common/common-kit
         common/common-core
         
@@ -32,8 +33,6 @@
         
         net-bolt/bolt-client
 
-        
-        widget/light-timer-task
         
         widget/light-domain-event
         
@@ -44,8 +43,6 @@
         widget/light-client
         
         widget/light-game-room
-
-        common/common-micro-kit
         widget/other-tool
     
 
@@ -69,16 +66,12 @@
         
         4.0.5
 
-        
-        3.17.7
         
         2.0.46
 
         
         1.11.9
 
-        
-        1.2.4.Final
         
         3.4.4
 
diff --git a/widget/light-timer-task/README.md b/widget/light-timer-task/README.md
deleted file mode 100644
index 5a0f78e97..000000000
--- a/widget/light-timer-task/README.md
+++ /dev/null
@@ -1,5 +0,0 @@
-light-timer-task 任务延时器
-
-see:
-
-https://www.yuque.com/iohao/game/niflk0
diff --git a/widget/light-timer-task/pom.xml b/widget/light-timer-task/pom.xml
deleted file mode 100644
index 2a88cf5c7..000000000
--- a/widget/light-timer-task/pom.xml
+++ /dev/null
@@ -1,37 +0,0 @@
-
-
-    
-        ioGame
-        com.iohao.game
-        21.15
-        ../../pom.xml
-    
-    4.0.0
-
-    light-timer-task
-
-    
-        
-        
-            org.cache2k
-            cache2k-core
-            ${cache2k.version}
-        
-
-        
-        
-            org.cache2k
-            cache2k-api
-            ${cache2k.version}
-        
-
-        
-            com.iohao.game
-            common-micro-kit
-            ${project.parent.version}
-        
-    
-
-
\ No newline at end of file
diff --git a/widget/light-timer-task/src/main/java/com/iohao/game/widget/light/timer/task/AbstractTimerTask.java b/widget/light-timer-task/src/main/java/com/iohao/game/widget/light/timer/task/AbstractTimerTask.java
deleted file mode 100644
index 2168e234c..000000000
--- a/widget/light-timer-task/src/main/java/com/iohao/game/widget/light/timer/task/AbstractTimerTask.java
+++ /dev/null
@@ -1,146 +0,0 @@
-/*
- * ioGame
- * Copyright (C) 2021 - present  渔民小镇 (262610965@qq.com、luoyizhu@gmail.com) . All Rights Reserved.
- * # iohao.com . 渔民小镇
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License as
- * published by the Free Software Foundation, either version 3 of the
- * License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License
- * along with this program.  If not, see .
- */
-package com.iohao.game.widget.light.timer.task;
-
-import com.iohao.game.common.kit.exception.ThrowKit;
-import com.iohao.game.common.kit.id.IdKit;
-import lombok.AccessLevel;
-import lombok.Getter;
-import lombok.Setter;
-import lombok.experimental.Accessors;
-import lombok.experimental.FieldDefaults;
-
-import java.io.Serial;
-import java.util.Objects;
-import java.util.concurrent.atomic.AtomicBoolean;
-
-/**
- * 定时任务执行 抽象类
- *
- * @author 渔民小镇
- * @date 2021-12-25
- */
-@Getter
-@Accessors(chain = true)
-@FieldDefaults(level = AccessLevel.PROTECTED)
-@SuppressWarnings("unchecked")
-public abstract class AbstractTimerTask implements TimerTask {
-    @Serial
-    private static final long serialVersionUID = -8201895378376640589L;
-
-    /**
-     * 多少毫秒后触发 execute 方法
-     * 
-     *     例如设置 5000, 那么5秒后将触发执行方法
-     * 
- * - * @see TimerTask#execute() - */ - @Setter - long delayExecutionTime; - /** true 任务活跃中 */ - AtomicBoolean activity = new AtomicBoolean(true); - - /** 缓存唯一 key */ - @Setter - String cacheKey; - - /** 任务启动时间 */ - long startTimeMillis; - - /** - * 子类提供的 定时任务存储器 - * - * @return 定时任务存储器 - */ - abstract protected TimerTaskRegion getTimerTaskRegion(); - - /** - * 取消定时任务 - */ - @Override - public void cancel() { - TimerTaskRegion timerTaskRegion = this.getTimerTaskRegion(); - timerTaskRegion.removeTimerTask(this.cacheKey); - } - - /** - * 启动任务延时器 - * - * @return me - */ - @Override - public T task() { - - if (!activity.get()) { - return (T) this; - } - - if (Objects.isNull(this.cacheKey)) { - // 随机分配一个 key - this.cacheKey = IdKit.sid(); - } - - if (this.delayExecutionTime <= 0) { - ThrowKit.ofRuntimeException("必须配置执行时间"); - } - - // 任务启动时间 = 当前时间 + 预期延后执行时间 -// this.startTimeMillis = System.currentTimeMillis() + delayExecutionTime; - - TimerTaskRegion timerTaskRegion = this.getTimerTaskRegion(); - timerTaskRegion.put(cacheKey, this); - - return (T) this; - } - - /** - * 暂停任务一段时间 - *
-     *     连续调用暂停时间不会累加, 需要到任务加入到队列中
-     * 
- * - * @param stopTimeMillis 暂停的时间 - */ - @Override - public void pause(long stopTimeMillis) { - // 取消任务 - this.cancel(); - - // 当前时间 - long currentTimeMillis = System.currentTimeMillis(); - - // 已经使用的时间 = 延迟时间 - (任务启动时间 - 当前时间) - long runTime = this.delayExecutionTime - (this.startTimeMillis - currentTimeMillis); - - // 新的延迟时间 = 原有延迟时间 - 已经使用的时间 + 需要暂停的时间 - this.delayExecutionTime = this.delayExecutionTime - runTime + stopTimeMillis; - - // 重新启动任务 - this.task(); - } - - @Override - public long getCacheExpiryTime() { - long currentTimeMillis = System.currentTimeMillis(); - // 过期时间 = 当前时间 + 延迟时间 - this.startTimeMillis = currentTimeMillis + this.delayExecutionTime; - return this.startTimeMillis; - } -} \ No newline at end of file diff --git a/widget/light-timer-task/src/main/java/com/iohao/game/widget/light/timer/task/Cache2Kit.java b/widget/light-timer-task/src/main/java/com/iohao/game/widget/light/timer/task/Cache2Kit.java deleted file mode 100644 index 840a06ad6..000000000 --- a/widget/light-timer-task/src/main/java/com/iohao/game/widget/light/timer/task/Cache2Kit.java +++ /dev/null @@ -1,146 +0,0 @@ -/* - * ioGame - * Copyright (C) 2021 - present 渔民小镇 (262610965@qq.com、luoyizhu@gmail.com) . All Rights Reserved. - * # iohao.com . 渔民小镇 - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ -package com.iohao.game.widget.light.timer.task; - -import lombok.Setter; -import lombok.experimental.UtilityClass; -import org.cache2k.Cache; -import org.cache2k.Cache2kBuilder; -import org.cache2k.event.CacheEntryExpiredListener; - -import java.util.Objects; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.ConcurrentMap; - -/** - * 缓存管理器工具 - * - * @author 渔民小镇 - * @date 2021-12-25 - */ -@UtilityClass -public class Cache2Kit { - - /** - * 存储设置 - *
-     *     不是必须的
-     *     如果你需要在系统停止时, 将定时任务存储起来 (比如存储到 redis 中). 那么这里提供了一个接口实现
-     *     当然, 也可以自己实现存储
-     * 
- */ - @Setter - private StoreSetting storeSetting = new StoreSetting() { - }; - - /** - * 创建一个新的缓存管理器 - * - * @return 缓存管理器 - */ - public Cache createCache() { - return createCacheBuilder() - .build(); - } - - /** - * 创建一个缓存管理构建器 - * - * @return 缓存管理构建器 - */ - public Cache2kBuilder createCacheBuilder() { - return Cache2kBuilder.of(String.class, TimerTask.class) - .sharpExpiry(true) - .eternal(false) - .entryCapacity(10240) - // 过期时间 - 在此时间后过期 - .expiryPolicy((key, value, loadTime, oldEntry) -> value.getCacheExpiryTime()) - // 过期时触发的监听事件 - .addListener((CacheEntryExpiredListener) (cache, task) -> { - TimerTask timerTask = task.getValue(); - ((AbstractTimerTask) timerTask).activity.set(false); - // 执行业务方法 - timerTask.execute(); - // 移除任务 - timerTask.cancel(); - }); - } - - /** - * 序列化 - * - * @param key key - * @param cache 缓存管理器 - */ - public void serialize(String key, Cache cache) { - ConcurrentMap taskMap = cache.asMap(); - if (!taskMap.isEmpty()) { - ConcurrentMap map = new ConcurrentHashMap<>(taskMap); - storeSetting.put(key, map); - } else { - storeSetting.remove(key); - } - } - - /** - * 反序列化 - * - * @param key key - * @param region 缓存管理器 - */ - public void deserialize(String key, Cache region) { - ConcurrentMap map = storeSetting.get(key); - if (Objects.nonNull(map) && !map.isEmpty()) { - region.putAll(map); - storeSetting.remove(key); - } - } - - /** - * 定时任务存储设置 - */ - public interface StoreSetting { - /** - * 序列表保存 - * - * @param key 缓存 key - * @param map 任务map - */ - default void put(String key, ConcurrentMap map) { - } - - /** - * 移除序列化数据 - * - * @param key 缓存 key - */ - default void remove(String key) { - } - - /** - * 获取序列化数据 - * - * @param key 缓存 key - * @return 任务 map - */ - default ConcurrentMap get(String key) { - return null; - } - } -} diff --git a/widget/light-timer-task/src/main/java/com/iohao/game/widget/light/timer/task/TimerTask.java b/widget/light-timer-task/src/main/java/com/iohao/game/widget/light/timer/task/TimerTask.java deleted file mode 100644 index 7b3cdf8cd..000000000 --- a/widget/light-timer-task/src/main/java/com/iohao/game/widget/light/timer/task/TimerTask.java +++ /dev/null @@ -1,62 +0,0 @@ -/* - * ioGame - * Copyright (C) 2021 - present 渔民小镇 (262610965@qq.com、luoyizhu@gmail.com) . All Rights Reserved. - * # iohao.com . 渔民小镇 - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ -package com.iohao.game.widget.light.timer.task; - -import org.cache2k.expiry.ValueWithExpiryTime; - -import java.io.Serializable; - -/** - * 定时任务 - * - * @author 渔民小镇 - * @date 2021-12-25 - */ -public interface TimerTask extends ValueWithExpiryTime, Serializable { - /** - * 执行方法 - *
-     *     时间到就执行, 否则就不执行
-     * 
- */ - void execute(); - - /** - * 启动任务延时器 - * - * @return me - */ - T task(); - - /** - * 取消定时任务 - */ - void cancel(); - - /** - * 暂停任务一段时间 - *
-     *     连续调用暂停时间不会累加, 需要到任务加入到队列中
-     * 
- * - * @param stopTimeMillis 暂停的时间 - */ - void pause(long stopTimeMillis); - -} diff --git a/widget/light-timer-task/src/main/java/com/iohao/game/widget/light/timer/task/TimerTaskRegion.java b/widget/light-timer-task/src/main/java/com/iohao/game/widget/light/timer/task/TimerTaskRegion.java deleted file mode 100644 index d3b7ef5ae..000000000 --- a/widget/light-timer-task/src/main/java/com/iohao/game/widget/light/timer/task/TimerTaskRegion.java +++ /dev/null @@ -1,95 +0,0 @@ -/* - * ioGame - * Copyright (C) 2021 - present 渔民小镇 (262610965@qq.com、luoyizhu@gmail.com) . All Rights Reserved. - * # iohao.com . 渔民小镇 - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ -package com.iohao.game.widget.light.timer.task; - -import org.cache2k.Cache; - -import java.util.Objects; - -/** - * 定时任务域 - *
- *     负责定时任务的: 存储 获取
- * 
- * - * @author 渔民小镇 - * @date 2021-12-25 - */ -public interface TimerTaskRegion { - /** - * 定时任务管理 - * - * @return 定时任务管理 - */ - Cache getCache(); - - /** - * region name - * - * @return regionName - */ - String name(); - - /** - * 通过 key 删除定时任务 - * - * @param key key - */ - default void removeTimerTask(String key) { - this.getCache().remove(key); - } - - /** - * getTimerTask - * - * @param key key - * @return TimerTask - */ - default TimerTask getTimerTask(String key) { - return this.getCache().get(key); - } - - /** - * put 定时任务 - * - * @param cacheKey cacheKey - * @param timerTask 定时任务 - */ - default void put(String cacheKey, TimerTask timerTask) { - this.getCache().put(cacheKey, timerTask); - } - - /** - * 暂停 定时任务域 下的所有 定时任务 - *
-     *     这个方法方这里不太好, 暂时放这吧
-     * 
- * - * @param timeMillis 时间 - */ - default void pauseTask(long timeMillis) { - Cache cache = this.getCache(); - for (String key : cache.keys()) { - TimerTask timerTask = cache.get(key); - if (Objects.nonNull(timerTask)) { - timerTask.pause(timeMillis); - } - } - } -} diff --git a/widget/light-timer-task/src/main/java/com/iohao/game/widget/light/timer/task/TimerTaskSerializable.java b/widget/light-timer-task/src/main/java/com/iohao/game/widget/light/timer/task/TimerTaskSerializable.java deleted file mode 100644 index f567404ff..000000000 --- a/widget/light-timer-task/src/main/java/com/iohao/game/widget/light/timer/task/TimerTaskSerializable.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - * ioGame - * Copyright (C) 2021 - present 渔民小镇 (262610965@qq.com、luoyizhu@gmail.com) . All Rights Reserved. - * # iohao.com . 渔民小镇 - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ -package com.iohao.game.widget.light.timer.task; - -/** - * 定时任务序列化接口 - * - * @author 渔民小镇 - * @date 2021-12-25 - * @see Cache2Kit#setStoreSetting(Cache2Kit.StoreSetting) - */ -public interface TimerTaskSerializable extends TimerTaskRegion { - /** - * 序列化到 redis 中 - */ - default void serialize() { - Cache2Kit.serialize(this.name(), this.getCache()); - } - - /** - * 反序列化 - */ - default void deserialize() { - Cache2Kit.deserialize(this.name(), this.getCache()); - } -} diff --git a/widget/light-timer-task/src/main/java/com/iohao/game/widget/light/timer/task/package-info.java b/widget/light-timer-task/src/main/java/com/iohao/game/widget/light/timer/task/package-info.java deleted file mode 100644 index 1a67eebe7..000000000 --- a/widget/light-timer-task/src/main/java/com/iohao/game/widget/light/timer/task/package-info.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - * ioGame - * Copyright (C) 2021 - present 渔民小镇 (262610965@qq.com、luoyizhu@gmail.com) . All Rights Reserved. - * # iohao.com . 渔民小镇 - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ -/** - * 扩展模块 - timer-task 任务延时器 - *

- * 介绍 - *

- * 可对任务进行延时执行、暂停、取消等操作,并不是类似 Quartz 的任务调度,功能特点:
- * 1. 单一职责原则
- * 2. 将来某个时间执行开发者定义的任务(一个任务延时器, 达到指定时间则执行业务 - 新增)
- * 3. 暂停任务延时器 (暂停时,将不在计时 - 变更)
- * 4. 该任务延时器也支持被移除(在满足某个条件后 - 移除)
- *
- * 任务延时器内部采用内存级的缓存库的方式实现,巧妙的运用了内存级缓存库的过期机制,来现实任务的延时机制。
- * 
- * 应用场景举例 - *
- * 在游戏开始前,房间创建后需要在1分钟后自动解散房间(将来某个时间执行该任务)。
- * 在房间解散前,每当有一个玩家加入房间,则解散房间延迟(暂停任务)30秒。
- * 当房间人数大于5人时,则取消房间自动解散规则(显示的取消任务)
- * 
- * - * @author 渔民小镇 - * @date 2021-12-26 - */ -package com.iohao.game.widget.light.timer.task; \ No newline at end of file diff --git a/widget/light-timer-task/src/test/java/com/iohao/game/widget/light/timer/task/MultipleTaskTest.java b/widget/light-timer-task/src/test/java/com/iohao/game/widget/light/timer/task/MultipleTaskTest.java deleted file mode 100644 index cbbc5c355..000000000 --- a/widget/light-timer-task/src/test/java/com/iohao/game/widget/light/timer/task/MultipleTaskTest.java +++ /dev/null @@ -1,75 +0,0 @@ -/* - * ioGame - * Copyright (C) 2021 - present 渔民小镇 (262610965@qq.com、luoyizhu@gmail.com) . All Rights Reserved. - * # iohao.com . 渔民小镇 - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ -package com.iohao.game.widget.light.timer.task; - -import com.iohao.game.widget.light.timer.task.example.StudentTask; -import com.iohao.game.widget.light.timer.task.example.TimerTaskEnum; -import lombok.extern.slf4j.Slf4j; -import org.junit.Test; - -import java.util.concurrent.ConcurrentMap; -import java.util.concurrent.TimeUnit; - -/** - * @author 渔民小镇 - * @date 2022-09-21 - */ -@Slf4j -public class MultipleTaskTest { - @Test - public void moreTask() throws InterruptedException { - /* - * 默认情况下,延时器的执行上限是 10_000 , 如果你的业务中有更大的需求量, - * 可以通过 Cache2Kit.createCacheBuilder 构建器来调整。 - * 如果有少许误差,可以研究 cache2k 的内核,或对 cache2k 进行升级。 - */ - int loop = 20_000; - - for (int i = 0; i < loop; i++) { - new StudentTask() - .setDelayExecutionTime(1000) - .task(); - } - - log.info("----start"); - print(); - Thread.sleep(13000); - - log.info("StudentTask.longAdder : {}", StudentTask.longAdder); - } - - void print() { - new Thread(() -> { - while (true) { - ConcurrentMap stringTimerTaskConcurrentMap = TimerTaskEnum.STUDENT.getCache().asMap(); - - try { - log.info("Thread -- StudentTask.longAdder : {}", StudentTask.longAdder); - log.info("Thread -- 剩余任务数量 : {}", stringTimerTaskConcurrentMap.size()); - - TimeUnit.MILLISECONDS.sleep(500); - } catch (InterruptedException e) { - log.error(e.getMessage(), e); - } - - } - - }).start(); - } -} diff --git a/widget/light-timer-task/src/test/java/com/iohao/game/widget/light/timer/task/TimerTaskTest.java b/widget/light-timer-task/src/test/java/com/iohao/game/widget/light/timer/task/TimerTaskTest.java deleted file mode 100644 index e6e5edae6..000000000 --- a/widget/light-timer-task/src/test/java/com/iohao/game/widget/light/timer/task/TimerTaskTest.java +++ /dev/null @@ -1,131 +0,0 @@ -/* - * ioGame - * Copyright (C) 2021 - present 渔民小镇 (262610965@qq.com、luoyizhu@gmail.com) . All Rights Reserved. - * # iohao.com . 渔民小镇 - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ -package com.iohao.game.widget.light.timer.task; - -import com.iohao.game.widget.light.timer.task.example.HelloTask; -import com.iohao.game.widget.light.timer.task.example.TimerTaskEnum; -import lombok.extern.slf4j.Slf4j; -import org.junit.Test; - -/** - * @author 渔民小镇 - * @date 2021-12-25 - */ -@Slf4j -public class TimerTaskTest { - - @Test - public void demoTimerTask() throws InterruptedException { - - new HelloTask() - // 设置业务内容 - .setSayContent("卡莉斯塔去哪儿了!!") - // 1秒后触发 - .setDelayExecutionTime(1000) - //启动任务延时器 - .task(); - - log.info("----start"); - Thread.sleep(3000); - } - - @Test - public void pause() throws InterruptedException { - log.info("----暂停任务"); - TimerTask task = new HelloTask() - // 设置业务内容 - .setSayContent("塔姆哪儿了!") - // 1秒后触发 - .setDelayExecutionTime(1000) - //启动任务延时器 - .task(); - - log.info("----start"); - // 暂停任务 1 秒 - task.pause(1000); - log.info("----end"); - - Thread.sleep(3000); - } - - @Test - public void task() throws InterruptedException { - log.info("两秒后执行 定时任务"); - - new HelloTask() - .setSayContent("卡莉斯塔去哪儿了!") - .setDelayExecutionTime(2000) - .task(); - - for (int i = 0; i < 10; i++) { - new HelloTask() - .setSayContent("卡莉斯塔去哪儿了!" + i) - .setDelayExecutionTime(2000) - .task(); - } - - Thread.sleep(3000); - System.out.println("------------"); - } - - @Test - public void coverTask() throws InterruptedException { - log.info("覆盖任务延时器"); - - // 设置缓存key。 - String cacheKey = "1"; - - new HelloTask() - .setSayContent("卡莉斯塔去哪儿了!") - .setDelayExecutionTime(2000) - // 如果不指定key, 会默认给个 uuid - .setCacheKey(cacheKey) - .task(); - - Thread.sleep(1000); - - // 该任务延时器会覆盖上一个同值 key 的任务延时器 - new HelloTask() - .setSayContent("卡莉斯塔去哪儿了! ~~~~~ ") - .setDelayExecutionTime(2000) - .setCacheKey(cacheKey) - .task(); - - Thread.sleep(3000); - } - - @Test - public void removeTask() throws InterruptedException { - log.info("这个示例是取消 定时任务"); - String cacheKey = "abc"; - - new HelloTask() - .setSayContent("卡莉斯塔去哪儿了!") - .setDelayExecutionTime(2000) - .setCacheKey(cacheKey) - .task(); - - Thread.sleep(1000); - log.info("睡眠一秒后, 满足某个业务条件, 我不想执行定时任务了"); - // 根据 key 移除任务延时器 - TimerTaskEnum.HELLO.removeTimerTask(cacheKey); - Thread.sleep(3000); - } - -} \ No newline at end of file diff --git a/widget/light-timer-task/src/test/java/com/iohao/game/widget/light/timer/task/example/HelloTask.java b/widget/light-timer-task/src/test/java/com/iohao/game/widget/light/timer/task/example/HelloTask.java deleted file mode 100644 index 8b0c70599..000000000 --- a/widget/light-timer-task/src/test/java/com/iohao/game/widget/light/timer/task/example/HelloTask.java +++ /dev/null @@ -1,51 +0,0 @@ -/* - * ioGame - * Copyright (C) 2021 - present 渔民小镇 (262610965@qq.com、luoyizhu@gmail.com) . All Rights Reserved. - * # iohao.com . 渔民小镇 - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ -package com.iohao.game.widget.light.timer.task.example; - -import com.iohao.game.widget.light.timer.task.AbstractTimerTask; -import com.iohao.game.widget.light.timer.task.TimerTaskRegion; -import lombok.Setter; -import lombok.experimental.Accessors; -import lombok.extern.slf4j.Slf4j; - -/** - * 定时任务示例 业务类 - * - * @author 渔民小镇 - * @date 2021-12-25 - */ -@Slf4j -@Accessors(chain = true) -public class HelloTask extends AbstractTimerTask { - - /** 说话内容 */ - @Setter - private String sayContent; - - @Override - public void execute() { - log.info("对着河道说: {}", sayContent); - } - - - @Override - protected TimerTaskRegion getTimerTaskRegion() { - return TimerTaskEnum.HELLO; - } -} diff --git a/widget/light-timer-task/src/test/java/com/iohao/game/widget/light/timer/task/example/StudentTask.java b/widget/light-timer-task/src/test/java/com/iohao/game/widget/light/timer/task/example/StudentTask.java deleted file mode 100644 index 2f77ee6d1..000000000 --- a/widget/light-timer-task/src/test/java/com/iohao/game/widget/light/timer/task/example/StudentTask.java +++ /dev/null @@ -1,46 +0,0 @@ -/* - * ioGame - * Copyright (C) 2021 - present 渔民小镇 (262610965@qq.com、luoyizhu@gmail.com) . All Rights Reserved. - * # iohao.com . 渔民小镇 - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ -package com.iohao.game.widget.light.timer.task.example; - -import com.iohao.game.widget.light.timer.task.AbstractTimerTask; -import com.iohao.game.widget.light.timer.task.TimerTaskRegion; -import lombok.experimental.Accessors; -import lombok.extern.slf4j.Slf4j; - -import java.util.concurrent.atomic.LongAdder; - -/** - * @author 渔民小镇 - * @date 2022-09-21 - */ -@Slf4j -@Accessors(chain = true) -public class StudentTask extends AbstractTimerTask { - public static LongAdder longAdder = new LongAdder(); - - @Override - protected TimerTaskRegion getTimerTaskRegion() { - return TimerTaskEnum.STUDENT; - } - - @Override - public void execute() { - longAdder.add(1); - } -} diff --git a/widget/light-timer-task/src/test/java/com/iohao/game/widget/light/timer/task/example/TimerTaskEnum.java b/widget/light-timer-task/src/test/java/com/iohao/game/widget/light/timer/task/example/TimerTaskEnum.java deleted file mode 100644 index ad4dafc83..000000000 --- a/widget/light-timer-task/src/test/java/com/iohao/game/widget/light/timer/task/example/TimerTaskEnum.java +++ /dev/null @@ -1,55 +0,0 @@ -/* - * ioGame - * Copyright (C) 2021 - present 渔民小镇 (262610965@qq.com、luoyizhu@gmail.com) . All Rights Reserved. - * # iohao.com . 渔民小镇 - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ -package com.iohao.game.widget.light.timer.task.example; - -import com.iohao.game.widget.light.timer.task.Cache2Kit; -import com.iohao.game.widget.light.timer.task.TimerTask; -import com.iohao.game.widget.light.timer.task.TimerTaskRegion; -import lombok.Getter; -import org.cache2k.Cache; - -/** - * 任务延时器域 - 枚举 - *
- *     1. 如果枚举类实现 TimerSerializable 接口,
- *     可以将任务序列化和反序列化到数据库中[redis] (实现该接口不是必须的)
- *
- *     2. 必须提供 cache 属性, 用于管理定时任务类
- * 
- * - * @author 渔民小镇 - * @date 2021-12-25 - */ -public enum TimerTaskEnum implements TimerTaskRegion { - /** - * 任务延时器管理域 - 说话 - *
-     *     key is uuid
-     *     value is {@link HelloTask}
-     * 
- */ - HELLO, - - STUDENT; - - /** 每个枚举对应一个缓存管理器 */ - @Getter - private final Cache cache = Cache2Kit.createCache(); -} -