From 32b90339d3c6e47d9b8fe50103237f3fe69961b4 Mon Sep 17 00:00:00 2001 From: walker Date: Fri, 26 May 2023 18:33:47 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8A=A5=E8=AD=A6=E7=9B=B8=E5=85=B3=E6=8A=BD?= =?UTF-8?q?=E8=B1=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../club/joylink/xiannccda/alert/Alert.java | 92 +++++++++++++++++++ .../joylink/xiannccda/alert/AlertEvent.java | 23 +++++ .../joylink/xiannccda/alert/AlertInfo.java | 30 ++++++ .../xiannccda/alert/AlertListener.java | 5 + .../xiannccda/alert/AlertMonitoringTask.java | 12 +++ .../joylink/xiannccda/alert/AlertTest.java | 65 +++++++++++++ xian-ncc-da-message | 2 +- 7 files changed, 228 insertions(+), 1 deletion(-) create mode 100644 src/main/java/club/joylink/xiannccda/alert/Alert.java create mode 100644 src/main/java/club/joylink/xiannccda/alert/AlertEvent.java create mode 100644 src/main/java/club/joylink/xiannccda/alert/AlertInfo.java create mode 100644 src/main/java/club/joylink/xiannccda/alert/AlertListener.java create mode 100644 src/main/java/club/joylink/xiannccda/alert/AlertMonitoringTask.java create mode 100644 src/test/java/club/joylink/xiannccda/alert/AlertTest.java diff --git a/src/main/java/club/joylink/xiannccda/alert/Alert.java b/src/main/java/club/joylink/xiannccda/alert/Alert.java new file mode 100644 index 0000000..a638011 --- /dev/null +++ b/src/main/java/club/joylink/xiannccda/alert/Alert.java @@ -0,0 +1,92 @@ +package club.joylink.xiannccda.alert; + +import java.util.ArrayList; +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 Alert { + + /** + * 报警主题监听Map(事件触发式报警) + */ + static Map> AlertConsumerMap = new ConcurrentHashMap<>(); + /** + * 报警监控任务(循环监测式报警) + */ + static List Amts = new ArrayList<>(); + /** + * 任务执行线程池 + */ + static ScheduledExecutorService Executor = Executors.newScheduledThreadPool(2); + static boolean started = false; + static final int interval = 100; // 执行器默认间隔,单位ms + + public static void registerAlertMonitoringTask(AlertMonitoringTask task) { + Amts.add(task); + taskStart(); + } + + synchronized static void taskStart() { + if (!started) { + started = true; + Executor.scheduleAtFixedRate(() -> Amts.forEach((task) -> task.run()), + 0, interval, TimeUnit.MILLISECONDS); + } + } + + /** + * 获取指定报警主题监听器 + * + * @param topic + * @return + */ + public static List getAlertConsumers(String topic) { + List list = AlertConsumerMap.get(topic); + if (list == null) { + list = new ArrayList<>(); + AlertConsumerMap.put(topic, list); + } + return list; + } + + /** + * 添加指定报警主题监听器 + * + * @param topic + * @param consumer + */ + public static void on(String topic, AlertListener consumer) { + List list = getAlertConsumers(topic); + list.add(consumer); + } + + /** + * 移除指定报警主题监听器 + * + * @param topic + * @param listener + */ + public static void off(String topic, AlertListener listener) { + List list = getAlertConsumers(topic); + list.remove(listener); + } + + /** + * 调用指定报警主题监听器 + * + * @param topic + * @param event + */ + public static void emit(String topic, AlertEvent event) { + List list = getAlertConsumers(topic); + list.forEach(consumer -> consumer.accept(event)); + } + +} diff --git a/src/main/java/club/joylink/xiannccda/alert/AlertEvent.java b/src/main/java/club/joylink/xiannccda/alert/AlertEvent.java new file mode 100644 index 0000000..09557ec --- /dev/null +++ b/src/main/java/club/joylink/xiannccda/alert/AlertEvent.java @@ -0,0 +1,23 @@ +package club.joylink.xiannccda.alert; + +import java.time.LocalDateTime; +import java.util.EventObject; + +public abstract class AlertEvent extends EventObject { + + private final LocalDateTime dateTime; + /** + * Constructs a prototypical Event. + * + * @param source the object on which the Event initially occurred + * @throws IllegalArgumentException if source is null + */ + public AlertEvent(Object source) { + super(source); + this.dateTime = LocalDateTime.now(); + } + public AlertEvent(Object source, LocalDateTime time) { + super(source); + this.dateTime = time; + } +} diff --git a/src/main/java/club/joylink/xiannccda/alert/AlertInfo.java b/src/main/java/club/joylink/xiannccda/alert/AlertInfo.java new file mode 100644 index 0000000..3d4644a --- /dev/null +++ b/src/main/java/club/joylink/xiannccda/alert/AlertInfo.java @@ -0,0 +1,30 @@ +package club.joylink.xiannccda.alert; + +import java.time.LocalDateTime; + +/** + * 报警信息 + */ +public interface AlertInfo { + + /** + * 报警级别 + * + * @return + */ + String getLevel(); + + /** + * 报警时间 + * + * @return + */ + LocalDateTime getAlertTime(); + + /** + * 报警信息 + * + * @return + */ + String getInfo(); +} diff --git a/src/main/java/club/joylink/xiannccda/alert/AlertListener.java b/src/main/java/club/joylink/xiannccda/alert/AlertListener.java new file mode 100644 index 0000000..557578d --- /dev/null +++ b/src/main/java/club/joylink/xiannccda/alert/AlertListener.java @@ -0,0 +1,5 @@ +package club.joylink.xiannccda.alert; + +public interface AlertListener { + void accept(AE event); +} diff --git a/src/main/java/club/joylink/xiannccda/alert/AlertMonitoringTask.java b/src/main/java/club/joylink/xiannccda/alert/AlertMonitoringTask.java new file mode 100644 index 0000000..232e2e1 --- /dev/null +++ b/src/main/java/club/joylink/xiannccda/alert/AlertMonitoringTask.java @@ -0,0 +1,12 @@ +package club.joylink.xiannccda.alert; + +/** + * 报警监测任务 + */ +public interface AlertMonitoringTask { + + /** + * 任务执行逻辑 + */ + void run(); +} diff --git a/src/test/java/club/joylink/xiannccda/alert/AlertTest.java b/src/test/java/club/joylink/xiannccda/alert/AlertTest.java new file mode 100644 index 0000000..b71132e --- /dev/null +++ b/src/test/java/club/joylink/xiannccda/alert/AlertTest.java @@ -0,0 +1,65 @@ +package club.joylink.xiannccda.alert; + +import org.junit.jupiter.api.Test; + +public class AlertTest { + + @Test + void testEvent() { + SuppliesRemainInsufficientAlert.init(); // 注册 + // 发布,此时监听会相应 + Alert.emit("supplies remain update", new SuppliesCountUpdatedEvent(this, 1)); + SuppliesRemainInsufficientAlert.clear(); // 移除 + // 发布,没有监听,不会处理 + Alert.emit("supplies remain update", new SuppliesCountUpdatedEvent(this, 2)); + +// Alert.registerAlertMonitoringTask(() -> { +// System.out.println("监测列车晚点"); +// }); + } + + static class TrainDelayAlert implements AlertMonitoringTask { + + int delayTime = 2; // 晚点报警触发时间 + + @Override + public void run() { + + } + } + + + static class SuppliesCountUpdatedEvent extends AlertEvent { + + final int remain; + + public SuppliesCountUpdatedEvent(Object source, int remain) { + super(source); + this.remain = remain; + } + } + + /** + * 应急物资匮乏报警 + */ + class SuppliesRemainInsufficientAlert { + + static final int SuppliesAlertMin = 10; + static AlertListener listener; + + static void init() { + listener = (AlertListener) event -> { + if (event.remain < SuppliesAlertMin) { + // 构建并发布物资匮乏报警 + System.out.println("物资匮乏报警"); + } + }; + Alert.on("supplies remain update", listener); + } + + static void clear() { + Alert.off("supplies remain update", listener); + } + } + +} diff --git a/xian-ncc-da-message b/xian-ncc-da-message index 51743f0..9aa5460 160000 --- a/xian-ncc-da-message +++ b/xian-ncc-da-message @@ -1 +1 @@ -Subproject commit 51743f0de38e3abf76bc2f6456000a25acbff33e +Subproject commit 9aa54608cd592ff96fc506e310fb19caff67f809