|
@@ -0,0 +1,138 @@
|
|
|
|
|
+package com.zksy.api.scheduling;
|
|
|
|
|
+
|
|
|
|
|
+import org.slf4j.Logger;
|
|
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
|
|
+import org.springframework.beans.factory.DisposableBean;
|
|
|
|
|
+import org.springframework.cloud.context.environment.EnvironmentChangeEvent;
|
|
|
|
|
+import org.springframework.context.ApplicationContext;
|
|
|
|
|
+import org.springframework.context.ApplicationContextAware;
|
|
|
|
|
+import org.springframework.context.ApplicationEvent;
|
|
|
|
|
+import org.springframework.context.ApplicationListener;
|
|
|
|
|
+import org.springframework.context.EnvironmentAware;
|
|
|
|
|
+import org.springframework.context.event.ContextRefreshedEvent;
|
|
|
|
|
+import org.springframework.core.env.Environment;
|
|
|
|
|
+import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
|
|
|
|
|
+
|
|
|
|
|
+import java.util.Objects;
|
|
|
|
|
+import java.util.concurrent.ScheduledFuture;
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * Nacos刷新后重新读取离线参数并重排巡检,不重建有设备状态缓存的任务bean。
|
|
|
|
|
+ * 每个采集实例仅使用一条调度线程,刷新时不打断正在执行的巡检,也不会并发巡检。
|
|
|
|
|
+ */
|
|
|
|
|
+public abstract class RefreshableOfflineCheckTask implements EnvironmentAware, ApplicationContextAware,
|
|
|
|
|
+ ApplicationListener<ApplicationEvent>, DisposableBean {
|
|
|
|
|
+ private static final Logger log = LoggerFactory.getLogger(RefreshableOfflineCheckTask.class);
|
|
|
|
|
+ private static final String TIMEOUT_KEY = "device.offline.timeout-minutes";
|
|
|
|
|
+ private static final String INTERVAL_KEY = "device.offline.check-interval-ms";
|
|
|
|
|
+ private static final long DEFAULT_INTERVAL_MS = 300000;
|
|
|
|
|
+
|
|
|
|
|
+ /** 未显式配置时使用的服务默认值;音频通过构造器设为1560分钟。 */
|
|
|
|
|
+ protected int offlineTimeoutMinutes;
|
|
|
|
|
+ private final Object lifecycleLock = new Object();
|
|
|
|
|
+ private final PositiveSetting timeoutSetting;
|
|
|
|
|
+ private final PositiveSetting intervalSetting = new PositiveSetting(INTERVAL_KEY, DEFAULT_INTERVAL_MS, Long.MAX_VALUE);
|
|
|
|
|
+ private Environment environment;
|
|
|
|
|
+ private ApplicationContext applicationContext;
|
|
|
|
|
+ private ThreadPoolTaskScheduler scheduler;
|
|
|
|
|
+ private ScheduledFuture<?> scheduled;
|
|
|
|
|
+ private long scheduledIntervalMs;
|
|
|
|
|
+ private int scheduledTimeoutMinutes;
|
|
|
|
|
+ private boolean closed;
|
|
|
|
|
+
|
|
|
|
|
+ protected RefreshableOfflineCheckTask() { this(30); }
|
|
|
|
|
+
|
|
|
|
|
+ protected RefreshableOfflineCheckTask(int defaultTimeoutMinutes) {
|
|
|
|
|
+ offlineTimeoutMinutes = defaultTimeoutMinutes;
|
|
|
|
|
+ timeoutSetting = new PositiveSetting(TIMEOUT_KEY, defaultTimeoutMinutes, Integer.MAX_VALUE);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override public void setEnvironment(Environment environment) { this.environment = environment; }
|
|
|
|
|
+ @Override public void setApplicationContext(ApplicationContext context) { applicationContext = context; }
|
|
|
|
|
+
|
|
|
|
|
+ /** 每轮读取当前Environment,避免普通@Value字段在Nacos刷新后仍保留启动值。 */
|
|
|
|
|
+ protected final int effectiveOfflineTimeoutMinutes() {
|
|
|
|
|
+ synchronized (lifecycleLock) {
|
|
|
|
|
+ return (int) timeoutSetting.read(environment, offlineTimeoutMinutes);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override public final void onApplicationEvent(ApplicationEvent event) {
|
|
|
|
|
+ if (event instanceof ContextRefreshedEvent
|
|
|
|
|
+ && ((ContextRefreshedEvent) event).getApplicationContext() == applicationContext) {
|
|
|
|
|
+ configureSchedule(true);
|
|
|
|
|
+ } else if (event instanceof EnvironmentChangeEvent) {
|
|
|
|
|
+ EnvironmentChangeEvent changed = (EnvironmentChangeEvent) event;
|
|
|
|
|
+ if (changed.getKeys().contains(TIMEOUT_KEY) || changed.getKeys().contains(INTERVAL_KEY)) {
|
|
|
|
|
+ configureSchedule(false);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private void configureSchedule(boolean starting) {
|
|
|
|
|
+ synchronized (lifecycleLock) {
|
|
|
|
|
+ if (closed || (scheduler == null && !starting)) return;
|
|
|
|
|
+ int timeout = effectiveOfflineTimeoutMinutes();
|
|
|
|
|
+ long interval = intervalSetting.read(environment, DEFAULT_INTERVAL_MS);
|
|
|
|
|
+ if (scheduler != null && scheduledTimeoutMinutes == timeout && scheduledIntervalMs == interval) return;
|
|
|
|
|
+ if (scheduler == null) {
|
|
|
|
|
+ scheduler = new ThreadPoolTaskScheduler();
|
|
|
|
|
+ scheduler.setPoolSize(1);
|
|
|
|
|
+ scheduler.setThreadNamePrefix("offline-" + getClass().getPackageName().split("\\.")[2] + "-");
|
|
|
|
|
+ scheduler.setRemoveOnCancelPolicy(true);
|
|
|
|
|
+ scheduler.setAwaitTerminationSeconds(3);
|
|
|
|
|
+ scheduler.initialize();
|
|
|
|
|
+ }
|
|
|
|
|
+ if (scheduled != null) scheduled.cancel(false);
|
|
|
|
|
+ // 从现在开始按新值执行;缩短超时后不再等待旧间隔到期。
|
|
|
|
|
+ scheduled = scheduler.scheduleAtFixedRate(this::checkDeviceOffline, interval);
|
|
|
|
|
+ scheduledTimeoutMinutes = timeout;
|
|
|
|
|
+ scheduledIntervalMs = interval;
|
|
|
|
|
+ log.info("离线巡检配置生效: task={}, timeoutMinutes={}, checkIntervalMs={}",
|
|
|
|
|
+ getClass().getName(), timeout, interval);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public abstract void checkDeviceOffline();
|
|
|
|
|
+
|
|
|
|
|
+ @Override public final void destroy() {
|
|
|
|
|
+ ThreadPoolTaskScheduler previous;
|
|
|
|
|
+ synchronized (lifecycleLock) {
|
|
|
|
|
+ closed = true;
|
|
|
|
|
+ if (scheduled != null) scheduled.cancel(false);
|
|
|
|
|
+ previous = scheduler;
|
|
|
|
|
+ scheduler = null;
|
|
|
|
|
+ }
|
|
|
|
|
+ if (previous != null) previous.shutdown();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /** 非法配置保留最近有效值,防止0/负数令全部设备离线或让定时任务终止。 */
|
|
|
|
|
+ private static final class PositiveSetting {
|
|
|
|
|
+ private final String key;
|
|
|
|
|
+ private final long max;
|
|
|
|
|
+ private long lastValid;
|
|
|
|
|
+ private String lastInvalid;
|
|
|
|
|
+
|
|
|
|
|
+ private PositiveSetting(String key, long initial, long max) {
|
|
|
|
|
+ this.key = key; this.lastValid = initial; this.max = max;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private long read(Environment environment, long fallback) {
|
|
|
|
|
+ String raw = environment == null ? null : environment.getProperty(key);
|
|
|
|
|
+ if (raw == null) {
|
|
|
|
|
+ lastValid = fallback; lastInvalid = null; return fallback;
|
|
|
|
|
+ }
|
|
|
|
|
+ try {
|
|
|
|
|
+ long value = Long.parseLong(raw.trim());
|
|
|
|
|
+ if (value <= 0 || value > max) throw new NumberFormatException();
|
|
|
|
|
+ lastValid = value; lastInvalid = null; return value;
|
|
|
|
|
+ } catch (NumberFormatException e) {
|
|
|
|
|
+ if (!Objects.equals(raw, lastInvalid)) {
|
|
|
|
|
+ log.warn("离线巡检参数必须为有效正整数,沿用最近有效值: key={}, effectiveValue={}", key, lastValid);
|
|
|
|
|
+ lastInvalid = raw;
|
|
|
|
|
+ }
|
|
|
|
|
+ return lastValid;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+}
|