From b50c73f30ec2fa777690a23723356b80901c1a1d Mon Sep 17 00:00:00 2001 From: joylink_zhangsai <1021828630@qq.com> Date: Fri, 16 Jun 2023 17:51:37 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BA=8B=E4=BB=B6=E6=A1=86=E6=9E=B6v0.2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../alert/core/AlertEventEmitter.java | 60 +++++++ .../xiannccda/alert/core/AlertHandler.java | 29 ---- .../xiannccda/alert/core/AlertInfo.java | 4 - .../alert/core/AlertMonitoringTask.java | 29 +--- .../alert/core/AlertProcessHandler.java | 23 --- .../alert/core/AlertProcessService.java | 33 ---- .../xiannccda/alert/core/AlertService.java | 38 ----- .../alert/core/AlertSourceEventListener.java | 4 +- .../alert/core/EventTaskManager.java | 155 ------------------ .../alert/xian3/Xian3AlertHandler.java | 27 --- .../Xian3AlertOccurredProcessHandler.java | 27 --- .../alert/xian3/Xian3AlertParam.java | 5 - .../alert/xian3/Xian3SupplyShortageAlert.java | 32 ++++ ...n3Alert.java => Xian3TrainDelayAlert.java} | 18 +- .../joylink/xiannccda/core/EventEmitter.java | 57 +++++++ .../club/joylink/xiannccda/core/Listener.java | 6 + .../alert/AlertEventEmitterTest.java | 100 +++++++++++ .../xiannccda/alert/EventTaskManagerTest.java | 114 ------------- 18 files changed, 261 insertions(+), 500 deletions(-) create mode 100644 src/main/java/club/joylink/xiannccda/alert/core/AlertEventEmitter.java delete mode 100644 src/main/java/club/joylink/xiannccda/alert/core/AlertHandler.java delete mode 100644 src/main/java/club/joylink/xiannccda/alert/core/AlertProcessHandler.java delete mode 100644 src/main/java/club/joylink/xiannccda/alert/core/AlertProcessService.java delete mode 100644 src/main/java/club/joylink/xiannccda/alert/core/AlertService.java delete mode 100644 src/main/java/club/joylink/xiannccda/alert/core/EventTaskManager.java delete mode 100644 src/main/java/club/joylink/xiannccda/alert/xian3/Xian3AlertHandler.java delete mode 100644 src/main/java/club/joylink/xiannccda/alert/xian3/Xian3AlertOccurredProcessHandler.java delete mode 100644 src/main/java/club/joylink/xiannccda/alert/xian3/Xian3AlertParam.java create mode 100644 src/main/java/club/joylink/xiannccda/alert/xian3/Xian3SupplyShortageAlert.java rename src/main/java/club/joylink/xiannccda/alert/xian3/{Xian3Alert.java => Xian3TrainDelayAlert.java} (66%) create mode 100644 src/main/java/club/joylink/xiannccda/core/EventEmitter.java create mode 100644 src/main/java/club/joylink/xiannccda/core/Listener.java create mode 100644 src/test/java/club/joylink/xiannccda/alert/AlertEventEmitterTest.java delete mode 100644 src/test/java/club/joylink/xiannccda/alert/EventTaskManagerTest.java diff --git a/src/main/java/club/joylink/xiannccda/alert/core/AlertEventEmitter.java b/src/main/java/club/joylink/xiannccda/alert/core/AlertEventEmitter.java new file mode 100644 index 0000000..d68a7e0 --- /dev/null +++ b/src/main/java/club/joylink/xiannccda/alert/core/AlertEventEmitter.java @@ -0,0 +1,60 @@ +package club.joylink.xiannccda.alert.core; + +import club.joylink.xiannccda.core.EventEmitter; +import java.util.HashMap; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; + +/** + * 事件任务管理器 + */ +public class AlertEventEmitter extends EventEmitter { + + private static final Map MANAGER_MAP = new HashMap<>(); + + /** + * 报警监控任务(循环监测式报警) + */ + Map amtMap = new ConcurrentHashMap<>(); + /** + * 任务执行线程池 + */ + ScheduledExecutorService Executor = Executors.newScheduledThreadPool(1); + boolean started = false; + final int interval = 100; // 执行器默认间隔,单位ms + + private AlertEventEmitter(String id) { + super(id); + } + + public synchronized static AlertEventEmitter getInstance(String id) { + return MANAGER_MAP.computeIfAbsent(id, k -> new AlertEventEmitter(id)); + } + + public synchronized static AlertEventEmitter removeInstance(String id) { + return MANAGER_MAP.remove(id); + } + + public void addTask(AlertMonitoringTask... tasks) { + if (tasks != null) { + for (AlertMonitoringTask task : tasks) { + amtMap.put(task.getName(), task); + } + } + } + + public void removeTask(String name) { + amtMap.remove(name); + } + + public synchronized void taskStart() { + if (!started) { + started = true; + Executor.scheduleAtFixedRate(() -> amtMap.values().forEach(AlertMonitoringTask::run), + 0, interval, TimeUnit.MILLISECONDS); + } + } +} diff --git a/src/main/java/club/joylink/xiannccda/alert/core/AlertHandler.java b/src/main/java/club/joylink/xiannccda/alert/core/AlertHandler.java deleted file mode 100644 index a141b3a..0000000 --- a/src/main/java/club/joylink/xiannccda/alert/core/AlertHandler.java +++ /dev/null @@ -1,29 +0,0 @@ -package club.joylink.xiannccda.alert.core; - -import club.joylink.xiannccda.util.JsonUtils; -import java.util.List; -import java.util.Map; - -/** - * 报警处理器 - */ -public interface AlertHandler { - /** - * 获取该处理器可以处理的报警类型 - */ - Class getHandleableClass(); - - Class

getUpdateParamClass(); - - default boolean update(AlertInfo alert, Map param) { - if (alert.getClass() == getHandleableClass()) { - P p = JsonUtils.read(JsonUtils.writeValueAsString(param), getUpdateParamClass()); - update(getHandleableClass().cast(alert), p); - return true; - } else { - return false; - } - } - - void update(A alert, P param); -} diff --git a/src/main/java/club/joylink/xiannccda/alert/core/AlertInfo.java b/src/main/java/club/joylink/xiannccda/alert/core/AlertInfo.java index 02bae38..76a5047 100644 --- a/src/main/java/club/joylink/xiannccda/alert/core/AlertInfo.java +++ b/src/main/java/club/joylink/xiannccda/alert/core/AlertInfo.java @@ -30,8 +30,4 @@ public interface AlertInfo { */ String getInfo(); - /** - * 报警当前阶段 - */ - AlertStage getStage(); } diff --git a/src/main/java/club/joylink/xiannccda/alert/core/AlertMonitoringTask.java b/src/main/java/club/joylink/xiannccda/alert/core/AlertMonitoringTask.java index 6ea831b..98b65d9 100644 --- a/src/main/java/club/joylink/xiannccda/alert/core/AlertMonitoringTask.java +++ b/src/main/java/club/joylink/xiannccda/alert/core/AlertMonitoringTask.java @@ -6,37 +6,10 @@ import java.util.function.Supplier; /** * 报警监测任务 */ -public interface AlertMonitoringTask { +public interface AlertMonitoringTask { String getName(); - /** - * 设置任务所需信息的提供者 - */ - void setInfoSupplier(Supplier supplier); - - /** - * 获取任务所需信息的提供者 - */ - Supplier getInfoSupplier(); - - /** - * 获取任务所需信息 - */ - default D getInfo(){ - return getInfoSupplier().get(); - }; - - /** - * 设置满足报警条件时的回调函数 - */ - void setCallBack(Consumer callBack); - - /** - * 获取满足报警条件时的回调函数 - */ - Consumer getCallBack(); - /** * 任务执行逻辑 */ diff --git a/src/main/java/club/joylink/xiannccda/alert/core/AlertProcessHandler.java b/src/main/java/club/joylink/xiannccda/alert/core/AlertProcessHandler.java deleted file mode 100644 index 24bee12..0000000 --- a/src/main/java/club/joylink/xiannccda/alert/core/AlertProcessHandler.java +++ /dev/null @@ -1,23 +0,0 @@ -package club.joylink.xiannccda.alert.core; - -import club.joylink.xiannccda.util.JsonUtils; -import java.util.Map; - -public interface AlertProcessHandler { - Class getAlertClass(); - AP getAlertStage(); - Class

getParamClass(); - - default void accept(AlertInfo alertInfo, Map param) { - if (alertInfo.getClass() == getAlertClass()) { - if (param == null) { - accept(getAlertClass().cast(alertInfo), getAlertStage(), null); - } else if (getParamClass() != null){ - P p = JsonUtils.read(JsonUtils.writeValueAsString(param), getParamClass()); - accept(getAlertClass().cast(alertInfo), getAlertStage(), p); - } - } - } - - void accept(A alert, AP alertStage, P param); -} diff --git a/src/main/java/club/joylink/xiannccda/alert/core/AlertProcessService.java b/src/main/java/club/joylink/xiannccda/alert/core/AlertProcessService.java deleted file mode 100644 index b33d612..0000000 --- a/src/main/java/club/joylink/xiannccda/alert/core/AlertProcessService.java +++ /dev/null @@ -1,33 +0,0 @@ -package club.joylink.xiannccda.alert.core; - -import com.google.common.collect.HashBasedTable; -import java.util.List; -import java.util.Map; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Service; -import org.springframework.util.CollectionUtils; - -@Service -public class AlertProcessService { - - private final HashBasedTable, AlertStage, AlertProcessHandler> table = HashBasedTable.create(); - - @Autowired - public AlertProcessService(List> handlerList) { - if (!CollectionUtils.isEmpty(handlerList)) { - handlerList.forEach(handler -> { - Class alertClass = handler.getAlertClass(); - AlertStage alertStage = handler.getAlertStage(); - table.put(alertClass, alertStage, handler); - }); - } - } - - private void update(AlertInfo alertInfo, AlertStage alertStage, Map param) { - AlertProcessHandler handler = table.get(alertInfo.getClass(), alertStage); - if (handler != null) { - handler.accept(alertInfo, param); - } - } - -} diff --git a/src/main/java/club/joylink/xiannccda/alert/core/AlertService.java b/src/main/java/club/joylink/xiannccda/alert/core/AlertService.java deleted file mode 100644 index 5bdf821..0000000 --- a/src/main/java/club/joylink/xiannccda/alert/core/AlertService.java +++ /dev/null @@ -1,38 +0,0 @@ -package club.joylink.xiannccda.alert.core; - -import club.joylink.xiannccda.util.JsonUtils; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.function.Function; -import java.util.stream.Collectors; -import org.springframework.stereotype.Service; -import org.springframework.util.CollectionUtils; - -@Service -public class AlertService { - - private final Map, AlertHandler> handlerMap; - - public AlertService(List> handlerList) { - if (!CollectionUtils.isEmpty(handlerList)) { - this.handlerMap = handlerList.stream() - .collect(Collectors.toMap(AlertHandler::getHandleableClass, Function.identity())); - } else { - handlerMap = new HashMap<>(); - } - } - - public void update(String sid, String alertId, Map param) { - EventTaskManager alertManager = EventTaskManager.getInstance(sid); - AlertInfo alert = alertManager.getAlert(alertId); - update(alert, param); - } - - private void update(AlertInfo alert, Map params) { - AlertHandler handler = handlerMap.get(alert.getClass()); - if (handler != null) { - handler.update(alert, params); - } - } -} diff --git a/src/main/java/club/joylink/xiannccda/alert/core/AlertSourceEventListener.java b/src/main/java/club/joylink/xiannccda/alert/core/AlertSourceEventListener.java index 2eed977..dfd6658 100644 --- a/src/main/java/club/joylink/xiannccda/alert/core/AlertSourceEventListener.java +++ b/src/main/java/club/joylink/xiannccda/alert/core/AlertSourceEventListener.java @@ -1,9 +1,11 @@ package club.joylink.xiannccda.alert.core; +import club.joylink.xiannccda.core.Listener; + /** * 报警源头事件监听器 */ -public interface AlertSourceEventListener { +public interface AlertSourceEventListener extends Listener { void accept(AE event); } diff --git a/src/main/java/club/joylink/xiannccda/alert/core/EventTaskManager.java b/src/main/java/club/joylink/xiannccda/alert/core/EventTaskManager.java deleted file mode 100644 index d90225a..0000000 --- a/src/main/java/club/joylink/xiannccda/alert/core/EventTaskManager.java +++ /dev/null @@ -1,155 +0,0 @@ -package club.joylink.xiannccda.alert.core; - -import club.joylink.xiannccda.alert.xian3.Xian3Alert; -import club.joylink.xiannccda.alert.xian3.Xian3Alert.Stage; -import com.google.common.collect.HashBasedTable; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.Executors; -import java.util.concurrent.ScheduledExecutorService; -import java.util.concurrent.TimeUnit; - -/** - * 事件任务管理器 - */ -public class EventTaskManager { - - private static final Map MANAGER_MAP = new HashMap<>(); - - private String id; - - /** - * 报警主题监听Map(事件触发式报警) - */ - Map> AlertConsumerMap = new ConcurrentHashMap<>(); - /** - * 报警监控任务(循环监测式报警) - */ - List Amts = new ArrayList<>(); - Map amtMap = new ConcurrentHashMap<>(); - /** - * 任务执行线程池 - */ - ScheduledExecutorService Executor = Executors.newScheduledThreadPool(1); - boolean started = false; - final int interval = 100; // 执行器默认间隔,单位ms - - //------------------------- 报警后 ----------------------------------- - private Map alertMap = new HashMap<>(); - - private final HashBasedTable, AlertStage, AlertProcessHandler> handlerTable = HashBasedTable.create(); - - private EventTaskManager(String id) { - this.id = id; - } - - public static EventTaskManager newInstance(String id) { - return MANAGER_MAP.compute(id, (k, v) -> new EventTaskManager(id)); - } - - public static EventTaskManager getInstance(String id) { - return MANAGER_MAP.computeIfAbsent(id, k -> new EventTaskManager(id)); - } - - public void registerAlertMonitoringTask(AlertMonitoringTask task) { - Amts.add(task); - taskStart(); - } - - public void unregisterAlertMonitoringTask(AlertMonitoringTask task) { - Amts.remove(task); - } - - synchronized void taskStart() { - if (!started) { - started = true; - Executor.scheduleAtFixedRate(() -> Amts.forEach(AlertMonitoringTask::run), - 0, interval, TimeUnit.MILLISECONDS); - } - } - - /** - * 获取指定报警主题监听器 - * - * @param topic - * @return - */ - public List getAlertConsumers(String topic) { - List list = AlertConsumerMap.computeIfAbsent( - topic, k -> new ArrayList<>()); - return list; - } - - /** - * 添加指定报警主题监听器 - * - * @param topic - * @param consumer - */ - public void on(String topic, AlertSourceEventListener consumer) { - List list = getAlertConsumers(topic); - list.add(consumer); - } - - /** - * 移除指定报警主题监听器 - * - * @param topic - * @param listener - */ - public void off(String topic, AlertSourceEventListener listener) { - List list = getAlertConsumers(topic); - list.remove(listener); - } - - /** - * 调用指定报警主题监听器 - * - * @param topic - * @param event - */ - public void emit(String topic, AlertSourceEvent event) { - List list = getAlertConsumers(topic); - list.forEach(consumer -> consumer.accept(event)); - } - - public void registerAlertHandler(AlertProcessHandler alertProcessHandler) { - AlertStage alertStage = alertProcessHandler.getAlertStage(); - handlerTable.put(alertProcessHandler.getAlertClass(), alertStage, alertProcessHandler); - } - - public void unregisterAlertHandler(Class alertClass, AlertStage alertStage) { - handlerTable.remove(alertClass, alertStage); - } - - public void alertOccurred(AlertInfo alertInfo) { - alertFlow(alertInfo, Stage.OCCURRED, null); - } - - /** - * 报警信息流转 - */ - public void alertFlow(AlertInfo alert, AlertStage alertStage, Map param) { - AlertProcessHandler handler = handlerTable.get(alert.getClass(), - alertStage); - if (handler != null) { - handler.accept(alert, param); - } - } - - public AlertInfo getAlert(String alertId) { - return alertMap.get(alertId); - } - - public E getAlert(String alertId, Class cls) { - AlertInfo alert = alertMap.get(alertId); - if (cls.isInstance(alert)) { - return cls.cast(alert); - } - throw new RuntimeException(); - } - -} diff --git a/src/main/java/club/joylink/xiannccda/alert/xian3/Xian3AlertHandler.java b/src/main/java/club/joylink/xiannccda/alert/xian3/Xian3AlertHandler.java deleted file mode 100644 index 68f8504..0000000 --- a/src/main/java/club/joylink/xiannccda/alert/xian3/Xian3AlertHandler.java +++ /dev/null @@ -1,27 +0,0 @@ -package club.joylink.xiannccda.alert.xian3; - -import club.joylink.xiannccda.alert.core.AlertHandler; -import java.util.Map; -import org.springframework.stereotype.Component; - -/** - * 西安3报警处理器 - */ -@Component -public class Xian3AlertHandler implements AlertHandler { - - @Override - public Class getHandleableClass() { - return Xian3Alert.class; - } - - @Override - public Class getUpdateParamClass() { - return Xian3AlertParam.class; - } - - @Override - public void update(Xian3Alert alert, Xian3AlertParam param) { - - } -} diff --git a/src/main/java/club/joylink/xiannccda/alert/xian3/Xian3AlertOccurredProcessHandler.java b/src/main/java/club/joylink/xiannccda/alert/xian3/Xian3AlertOccurredProcessHandler.java deleted file mode 100644 index de37d2a..0000000 --- a/src/main/java/club/joylink/xiannccda/alert/xian3/Xian3AlertOccurredProcessHandler.java +++ /dev/null @@ -1,27 +0,0 @@ -package club.joylink.xiannccda.alert.xian3; - -import club.joylink.xiannccda.alert.core.AlertProcessHandler; -import club.joylink.xiannccda.alert.xian3.Xian3Alert.Stage; - -public class Xian3AlertOccurredProcessHandler implements AlertProcessHandler { - - @Override - public Class getAlertClass() { - return Xian3Alert.class; - } - - @Override - public Stage getAlertStage() { - return Stage.OCCURRED; - } - - @Override - public Class getParamClass() { - return Xian3AlertParam.class; - } - - @Override - public void accept(Xian3Alert alert, Stage alertStage, Xian3AlertParam param) { - System.out.println("西安三故障发生"); - } -} diff --git a/src/main/java/club/joylink/xiannccda/alert/xian3/Xian3AlertParam.java b/src/main/java/club/joylink/xiannccda/alert/xian3/Xian3AlertParam.java deleted file mode 100644 index e510b91..0000000 --- a/src/main/java/club/joylink/xiannccda/alert/xian3/Xian3AlertParam.java +++ /dev/null @@ -1,5 +0,0 @@ -package club.joylink.xiannccda.alert.xian3; - -public class Xian3AlertParam { - -} diff --git a/src/main/java/club/joylink/xiannccda/alert/xian3/Xian3SupplyShortageAlert.java b/src/main/java/club/joylink/xiannccda/alert/xian3/Xian3SupplyShortageAlert.java new file mode 100644 index 0000000..279ca4f --- /dev/null +++ b/src/main/java/club/joylink/xiannccda/alert/xian3/Xian3SupplyShortageAlert.java @@ -0,0 +1,32 @@ +package club.joylink.xiannccda.alert.xian3; + +import club.joylink.xiannccda.alert.core.AlertInfo; +import java.time.LocalDateTime; + +public class Xian3SupplyShortageAlert implements AlertInfo { + private String id; + + public Xian3SupplyShortageAlert(String id) { + this.id = id; + } + + @Override + public String getId() { + return id; + } + + @Override + public String getLevel() { + return null; + } + + @Override + public LocalDateTime getAlertTime() { + return null; + } + + @Override + public String getInfo() { + return null; + } +} diff --git a/src/main/java/club/joylink/xiannccda/alert/xian3/Xian3Alert.java b/src/main/java/club/joylink/xiannccda/alert/xian3/Xian3TrainDelayAlert.java similarity index 66% rename from src/main/java/club/joylink/xiannccda/alert/xian3/Xian3Alert.java rename to src/main/java/club/joylink/xiannccda/alert/xian3/Xian3TrainDelayAlert.java index fdd0f09..15417de 100644 --- a/src/main/java/club/joylink/xiannccda/alert/xian3/Xian3Alert.java +++ b/src/main/java/club/joylink/xiannccda/alert/xian3/Xian3TrainDelayAlert.java @@ -7,16 +7,14 @@ import java.time.LocalDateTime; /** * 西安三号线报警 */ -public class Xian3Alert implements AlertInfo { +public class Xian3TrainDelayAlert implements AlertInfo { private String id; - private Stage stage; private String level; private LocalDateTime alertTime; private String info; - public Xian3Alert(String id) { + public Xian3TrainDelayAlert(String id) { this.id = id; - this.stage = Stage.OCCURRED; } @Override @@ -38,16 +36,4 @@ public class Xian3Alert implements AlertInfo { public String getInfo() { return info; } - - @Override - public AlertStage getStage() { - return stage; - } - - public enum Stage implements AlertStage { - OCCURRED, - CONFIRM, - PROCESSING, - RESOLVED, - } } diff --git a/src/main/java/club/joylink/xiannccda/core/EventEmitter.java b/src/main/java/club/joylink/xiannccda/core/EventEmitter.java new file mode 100644 index 0000000..5234854 --- /dev/null +++ b/src/main/java/club/joylink/xiannccda/core/EventEmitter.java @@ -0,0 +1,57 @@ +package club.joylink.xiannccda.core; + +import java.lang.reflect.Method; +import java.lang.reflect.ParameterizedType; +import java.lang.reflect.Type; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import lombok.Getter; +import org.springframework.util.CollectionUtils; + +/** + * 事件总线。 + *

+ * 负责事件的监听器的管理和事件的发布 + */ +public class EventEmitter { + private Map, List>> listenerMap = new HashMap<>(); + + @Getter + private String id; + + public EventEmitter(String id) { + this.id = id; + } + + public void addListener(Listener listener) { + for (Method declaredMethod : listener.getClass().getDeclaredMethods()) { + Class eventType = declaredMethod.getParameterTypes()[0]; + List> listeners = listenerMap.computeIfAbsent(eventType, K -> new ArrayList<>()); + listeners.add(listener); + } + } + + public void removeListeners(Class eventType) { + listenerMap.remove(eventType); + } + + public void removeListener(Listener listener) { + for (Method declaredMethod : listener.getClass().getDeclaredMethods()) { + Class eventType = declaredMethod.getParameterTypes()[0]; + List> listeners = listenerMap.get(eventType); + if (!CollectionUtils.isEmpty(listeners)) { + listeners.remove(listener); + } + } + } + + public void publish(E event) { + List> listeners = listenerMap.get(event.getClass()); + if (!CollectionUtils.isEmpty(listeners)) { + listeners.forEach(listener -> ((Listener) listener).accept(event)); + } + } +} diff --git a/src/main/java/club/joylink/xiannccda/core/Listener.java b/src/main/java/club/joylink/xiannccda/core/Listener.java new file mode 100644 index 0000000..ef10a01 --- /dev/null +++ b/src/main/java/club/joylink/xiannccda/core/Listener.java @@ -0,0 +1,6 @@ +package club.joylink.xiannccda.core; + +public interface Listener { + + void accept(E event); +} diff --git a/src/test/java/club/joylink/xiannccda/alert/AlertEventEmitterTest.java b/src/test/java/club/joylink/xiannccda/alert/AlertEventEmitterTest.java new file mode 100644 index 0000000..cee6f2a --- /dev/null +++ b/src/test/java/club/joylink/xiannccda/alert/AlertEventEmitterTest.java @@ -0,0 +1,100 @@ +package club.joylink.xiannccda.alert; + +import club.joylink.xiannccda.alert.core.AlertEventEmitter; +import club.joylink.xiannccda.alert.core.AlertSourceEvent; +import club.joylink.xiannccda.alert.core.AlertSourceEventListener; +import club.joylink.xiannccda.alert.core.AlertMonitoringTask; +import club.joylink.xiannccda.alert.xian3.Xian3SupplyShortageAlert; +import club.joylink.xiannccda.alert.xian3.Xian3TrainDelayAlert; +import club.joylink.xiannccda.core.Listener; +import java.lang.reflect.ParameterizedType; +import java.lang.reflect.Type; +import java.util.ArrayList; +import java.util.List; +import lombok.Getter; +import org.junit.jupiter.api.Test; + +public class AlertEventEmitterTest { + + @Test + void testEvent() { + String sid = "xian3"; + AlertEventEmitter manager = AlertEventEmitter.getInstance(sid); + + //可能报警事件监测 + manager.addTask(new TrainDelayAlertMonitoringTask(sid)); + //可能报警时间监听 + manager.addListener(new SuppliesRemainInsufficientListener(sid)); + //报警事件监听 + manager.addListener(new Listener() { + @Override + public void accept(Xian3TrainDelayAlert event) { + System.out.println("列车延误报警"); + } + }); + manager.addListener(new Listener() { + @Override + public void accept(Xian3SupplyShortageAlert event) { + System.out.println("物资紧缺报警"); + } + }); + + manager.taskStart(); + manager.publish(new SuppliesCountUpdatedEvent(this, 2)); + } + + static class TrainDelayAlertMonitoringTask implements AlertMonitoringTask { + String sId; + List trains = new ArrayList<>(); + List trips = new ArrayList<>(); + + public TrainDelayAlertMonitoringTask(String sId) { + this.sId = sId; + } + + @Override + public String getName() { + return "Train_Delay"; + } + + @Override + public void run() { + AlertEventEmitter alertEventBus = AlertEventEmitter.getInstance(sId); + alertEventBus.publish(new Xian3TrainDelayAlert("1")); + } + } + + + static class SuppliesCountUpdatedEvent extends AlertSourceEvent { + + @Getter + final int remain; + + public SuppliesCountUpdatedEvent(Object source, int remain) { + super(source); + this.remain = remain; + } + } + + /** + * 应急物资匮乏报警 + */ + static class SuppliesRemainInsufficientListener implements Listener{ + + String sId; + + public SuppliesRemainInsufficientListener(String sId) { + this.sId = sId; + } + + int SuppliesAlertMin = 10; + + @Override + public void accept(SuppliesCountUpdatedEvent event) { + if (event.getRemain() <= SuppliesAlertMin) { + AlertEventEmitter.getInstance(sId).publish(new Xian3SupplyShortageAlert("2")); + } + } + } + +} diff --git a/src/test/java/club/joylink/xiannccda/alert/EventTaskManagerTest.java b/src/test/java/club/joylink/xiannccda/alert/EventTaskManagerTest.java deleted file mode 100644 index 28bc6c0..0000000 --- a/src/test/java/club/joylink/xiannccda/alert/EventTaskManagerTest.java +++ /dev/null @@ -1,114 +0,0 @@ -package club.joylink.xiannccda.alert; - -import club.joylink.xiannccda.alert.core.AlertInfo; -import club.joylink.xiannccda.alert.core.EventTaskManager; -import club.joylink.xiannccda.alert.core.AlertSourceEvent; -import club.joylink.xiannccda.alert.core.AlertSourceEventListener; -import club.joylink.xiannccda.alert.core.AlertMonitoringTask; -import club.joylink.xiannccda.alert.xian3.Xian3Alert; -import club.joylink.xiannccda.alert.xian3.Xian3AlertOccurredProcessHandler; -import java.util.List; -import java.util.function.Consumer; -import java.util.function.Supplier; -import org.junit.jupiter.api.Test; - -public class EventTaskManagerTest { - - @Test - void testEvent() { - EventTaskManager manager = EventTaskManager.getInstance("id"); - List list = List.of("1", "2", "3"); - - //报警处理 - manager.registerAlertHandler(new Xian3AlertOccurredProcessHandler()); - - //报警事件监测 - TrainDelayAlertMonitoringTask task = new TrainDelayAlertMonitoringTask(); - task.setInfoSupplier(() -> list); - task.setCallBack(manager::alertOccurred); - manager.registerAlertMonitoringTask(task); - - SuppliesRemainInsufficientAlert.init(); // 注册 - // 发布,此时监听会相应 - manager.emit("supplies remain update", new SuppliesCountUpdatedEvent(this, 1)); - SuppliesRemainInsufficientAlert.clear(); // 移除 - // 发布,没有监听,不会处理 - manager.emit("supplies remain update", new SuppliesCountUpdatedEvent(this, 2)); - - - } - - static class TrainDelayAlertMonitoringTask implements AlertMonitoringTask> { - private Supplier> supplier; - private Consumer callBack; - - @Override - public String getName() { - return "Train_Delay"; - } - - @Override - public void setInfoSupplier(Supplier> supplier) { - this.supplier = supplier; - } - - @Override - public Supplier> getInfoSupplier() { - return supplier; - } - - @Override - public void setCallBack(Consumer callBack) { - this.callBack = callBack; - - } - - @Override - public Consumer getCallBack() { - return callBack; - } - - @Override - public void run() { - List info = getInfo(); - if (info.contains("1")) { - getCallBack().accept(new Xian3Alert("xian3")); - } - } - } - - - static class SuppliesCountUpdatedEvent extends AlertSourceEvent { - - final int remain; - - public SuppliesCountUpdatedEvent(Object source, int remain) { - super(source); - this.remain = remain; - } - } - - /** - * 应急物资匮乏报警 - */ - class SuppliesRemainInsufficientAlert{ - - static final int SuppliesAlertMin = 10; - static AlertSourceEventListener listener; - - static void init() { - listener = (AlertSourceEventListener) event -> { - if (event.remain < SuppliesAlertMin) { - // 构建并发布物资匮乏报警 - System.out.println("物资匮乏报警"); - } - }; - EventTaskManager.getInstance("id").on("supplies remain update", listener); - } - - static void clear() { - EventTaskManager.getInstance("id").off("supplies remain update", listener); - } - } - -}