报警相关抽象

This commit is contained in:
walker 2023-05-26 18:33:47 +08:00
parent 26e1b92726
commit 32b90339d3
7 changed files with 228 additions and 1 deletions

View File

@ -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<String, List<AlertListener>> AlertConsumerMap = new ConcurrentHashMap<>();
/**
* 报警监控任务(循环监测式报警)
*/
static List<AlertMonitoringTask> 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<AlertListener> getAlertConsumers(String topic) {
List<AlertListener> 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<AlertListener> list = getAlertConsumers(topic);
list.add(consumer);
}
/**
* 移除指定报警主题监听器
*
* @param topic
* @param listener
*/
public static void off(String topic, AlertListener listener) {
List<AlertListener> list = getAlertConsumers(topic);
list.remove(listener);
}
/**
* 调用指定报警主题监听器
*
* @param topic
* @param event
*/
public static void emit(String topic, AlertEvent event) {
List<AlertListener> list = getAlertConsumers(topic);
list.forEach(consumer -> consumer.accept(event));
}
}

View File

@ -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;
}
}

View File

@ -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();
}

View File

@ -0,0 +1,5 @@
package club.joylink.xiannccda.alert;
public interface AlertListener<AE extends AlertEvent> {
void accept(AE event);
}

View File

@ -0,0 +1,12 @@
package club.joylink.xiannccda.alert;
/**
* 报警监测任务
*/
public interface AlertMonitoringTask {
/**
* 任务执行逻辑
*/
void run();
}

View File

@ -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<SuppliesCountUpdatedEvent> listener;
static void init() {
listener = (AlertListener<SuppliesCountUpdatedEvent>) event -> {
if (event.remain < SuppliesAlertMin) {
// 构建并发布物资匮乏报警
System.out.println("物资匮乏报警");
}
};
Alert.on("supplies remain update", listener);
}
static void clear() {
Alert.off("supplies remain update", listener);
}
}
}

@ -1 +1 @@
Subproject commit 51743f0de38e3abf76bc2f6456000a25acbff33e Subproject commit 9aa54608cd592ff96fc506e310fb19caff67f809