瀏覽代碼

增加全局任务激活状态处理

欧阳劲驰 2 周之前
父節點
當前提交
90ce49af6b

+ 5 - 0
bespoke-gateway-app/src/main/resources/application-jldma.yml

@@ -27,3 +27,8 @@ calling:
       url: https://iot.cscec3b-iti.com
       access-key: sKUJoJbisVJg7ei8NSKtPQ
       secret-key: HtSU0Ul2NdJqLhK9uTa5BLetgzst0N4j9sIJ7dys
+#任务
+scheduling:
+  activates:
+    collect-iot-platform: off
+    sync-devices: off

+ 4 - 0
bespoke-gateway-app/src/main/resources/application.yml

@@ -69,6 +69,10 @@ mybatis:
 security:
   expiration: P7D
   secret: TRICP_ALAM_DMA
+#任务
+scheduling:
+  activates:
+    refresh-all-keys: off
 #对接
 calling:
   connect-timeout: PT30S

+ 6 - 6
bespoke-gateway-core/src/main/java/com/shkpr/service/bespokegateway/core/config/SchedulingConfig.java

@@ -1,6 +1,7 @@
 package com.shkpr.service.bespokegateway.core.config;
 
-import lombok.extern.slf4j.Slf4j;
+import com.shkpr.service.bespokegateway.core.properties.SchedulingProperties;
+import org.springframework.boot.context.properties.EnableConfigurationProperties;
 import org.springframework.boot.task.TaskSchedulerCustomizer;
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;
@@ -14,17 +15,16 @@ import org.springframework.scheduling.annotation.EnableScheduling;
  */
 @Configuration
 @EnableScheduling
-@Slf4j
+@EnableConfigurationProperties(SchedulingProperties.class)
 public class SchedulingConfig {
     /**
      * @return 调度器定制
      */
     @Bean
-    public TaskSchedulerCustomizer taskSchedulerCustomizer() {
+    public TaskSchedulerCustomizer taskSchedulerCustomizer(SchedulingProperties properties) {
         return scheduler -> {
-            scheduler.setAwaitTerminationSeconds(5);
-            scheduler.setWaitForTasksToCompleteOnShutdown(true);
+            scheduler.setAwaitTerminationSeconds(properties.getAwaitTerminationSeconds());
+            scheduler.setWaitForTasksToCompleteOnShutdown(properties.getWaitForTasksToCompleteOnShutdown());
         };
-
     }
 }

+ 13 - 3
bespoke-gateway-core/src/main/java/com/shkpr/service/bespokegateway/core/manager/IntegrationKeyManager.java

@@ -7,6 +7,7 @@ import com.shkpr.service.bespokegateway.core.domain.CallingEndpoint;
 import com.shkpr.service.bespokegateway.core.domain.IntegrationKey;
 import com.shkpr.service.bespokegateway.core.domain.IntegrationKeyLoader;
 import com.shkpr.service.bespokegateway.core.properties.CallingProperties;
+import com.shkpr.service.bespokegateway.core.properties.SchedulingProperties;
 import org.apache.commons.collections4.MapUtils;
 import org.springframework.scheduling.annotation.Scheduled;
 import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
@@ -26,12 +27,18 @@ import java.util.concurrent.ConcurrentHashMap;
 @Component
 public class IntegrationKeyManager {
     /**
+     * 刷新密钥任务
+     */
+    private static final String REFRESH_ALL_KEYS_SCHEDULING = "refresh-all-keys";
+    /**
      * log
      */
     private static final String CLASS_NAME = "IntegrationKeyManager";
     private static final String BIZ_TYPE = LogFlagBusiType.BUSI_ALL.toStrValue();
 
     final
+    SchedulingProperties schedulingProperties;
+    final
     ThreadPoolTaskExecutor taskScheduler;
     final
     CallingProperties callingProperties;
@@ -41,7 +48,8 @@ public class IntegrationKeyManager {
     //密钥缓存
     private final Map<String, IntegrationKey> keyCache = new ConcurrentHashMap<>();
 
-    public IntegrationKeyManager(ThreadPoolTaskExecutor taskScheduler, CallingProperties callingProperties, List<IntegrationKeyLoader> loaders) {
+    public IntegrationKeyManager(SchedulingProperties schedulingProperties, ThreadPoolTaskExecutor taskScheduler, CallingProperties callingProperties, List<IntegrationKeyLoader> loaders) {
+        this.schedulingProperties = schedulingProperties;
         this.taskScheduler = taskScheduler;
         this.callingProperties = callingProperties;
         // 注册所有密钥加载器
@@ -53,7 +61,8 @@ public class IntegrationKeyManager {
      */
     @PostConstruct
     public void init() {
-        taskScheduler.execute(this::refreshAllKeys);
+        if (schedulingProperties.isTaskActive(REFRESH_ALL_KEYS_SCHEDULING))
+            taskScheduler.execute(this::refreshAllKeys);
     }
 
     /**
@@ -61,7 +70,8 @@ public class IntegrationKeyManager {
      */
     @Scheduled(cron = "0 0 0 * * ?")
     public void minuteTask() {
-        taskScheduler.execute(this::refreshAllKeys);
+        if (schedulingProperties.isTaskActive(REFRESH_ALL_KEYS_SCHEDULING))
+            taskScheduler.execute(this::refreshAllKeys);
     }
 
     /**

+ 50 - 0
bespoke-gateway-core/src/main/java/com/shkpr/service/bespokegateway/core/properties/SchedulingProperties.java

@@ -0,0 +1,50 @@
+package com.shkpr.service.bespokegateway.core.properties;
+
+import lombok.Getter;
+import lombok.Setter;
+import org.apache.commons.lang3.StringUtils;
+import org.springframework.boot.context.properties.ConfigurationProperties;
+
+import java.util.Map;
+
+/**
+ * 调度属性
+ *
+ * @author 欧阳劲驰
+ * @since 1.0.0
+ */
+@Getter
+@Setter
+@ConfigurationProperties(prefix = "scheduling")
+public class SchedulingProperties {
+    /**
+     * 调度器关闭时等待任务完成的最大秒数
+     * 默认值:5秒
+     */
+    private Integer awaitTerminationSeconds = 5;
+
+    /**
+     * 调度器关闭时是否等待正在执行的任务完成
+     * 默认值:true(优雅关闭)
+     */
+    private Boolean waitForTasksToCompleteOnShutdown = true;
+
+    /**
+     * 激活状态
+     * key: 任务标识符
+     * value: 任务是否激活(true-激活,false-禁用)
+     */
+    private Map<String, Boolean> activates;
+
+    /**
+     * 检查指定任务是否处于激活状态
+     * <p>如果任务未在配置中定义,默认返回false</p>
+     *
+     * @param key 任务标识符
+     * @return 如果任务激活返回true,否则返回false
+     *
+     */
+    public boolean isTaskActive(String key) {
+        return StringUtils.isNotBlank(key) && activates.getOrDefault(key, false);
+    }
+}

+ 0 - 2
bespoke-gateway-jldma/src/main/java/com/shkpr/service/bespokegateway/jldma/components/DataCollector.java

@@ -57,7 +57,6 @@ public class DataCollector {
     final
     CallingUtil callingUtil;
 
-
     public DataCollector(CallingProperties callingProperties, IntegrationKeyManager integrationKeyManager, DeviceRegistry deviceRegistry, InfluxDbUtil influxDbUtil, CallingUtil callingUtil) {
         this.callingProperties = callingProperties;
         this.integrationKeyManager = integrationKeyManager;
@@ -66,7 +65,6 @@ public class DataCollector {
         this.callingUtil = callingUtil;
     }
 
-
     /**
      * 采集物联网平台
      *

+ 16 - 2
bespoke-gateway-jldma/src/main/java/com/shkpr/service/bespokegateway/jldma/constants/IotPlatformMetadata.java

@@ -23,8 +23,8 @@ public abstract class IotPlatformMetadata {
     /**
      * 获取历史数据参数
      *
-     * @param beginTime     开始时间
-     * @param endTime       结束时间
+     * @param beginTime 开始时间
+     * @param endTime   结束时间
      * @return 参数
      */
     public static Map<String, Object> getHistoryDataParams(String sn, LocalDateTime beginTime, LocalDateTime endTime) {
@@ -145,4 +145,18 @@ public abstract class IotPlatformMetadata {
         //区号
         AreaCode AREA_CODE = AreaCode.JIAN_LI;
     }
+
+    /**
+     * 任务key
+     */
+    public interface SchedulingKeys {
+        /**
+         * 采集数据
+         */
+        String COLLECT_IOT_PLATFORM = "collect-iot-platform";
+        /**
+         * 同步设备
+         */
+        String SYNC_DEVICES = "sync-devices";
+    }
 }

+ 10 - 3
bespoke-gateway-jldma/src/main/java/com/shkpr/service/bespokegateway/jldma/manager/DataManager.java

@@ -1,6 +1,8 @@
 package com.shkpr.service.bespokegateway.jldma.manager;
 
+import com.shkpr.service.bespokegateway.core.properties.SchedulingProperties;
 import com.shkpr.service.bespokegateway.jldma.components.DataCollector;
+import com.shkpr.service.bespokegateway.jldma.constants.IotPlatformMetadata;
 import org.springframework.scheduling.annotation.Scheduled;
 import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
 import org.springframework.stereotype.Component;
@@ -16,11 +18,14 @@ import javax.annotation.PostConstruct;
 @Component
 public class DataManager {
     final
+    SchedulingProperties schedulingProperties;
+    final
     ThreadPoolTaskExecutor taskScheduler;
     final
     DataCollector dataCollector;
 
-    public DataManager(ThreadPoolTaskExecutor taskScheduler, DataCollector dataCollector) {
+    public DataManager(SchedulingProperties schedulingProperties, ThreadPoolTaskExecutor taskScheduler, DataCollector dataCollector) {
+        this.schedulingProperties = schedulingProperties;
         this.taskScheduler = taskScheduler;
         this.dataCollector = dataCollector;
     }
@@ -31,7 +36,8 @@ public class DataManager {
     @PostConstruct
     public void init() {
         //采集物联网平台
-        taskScheduler.execute(() -> dataCollector.collectIotPlatform(30 * 24));
+        if (schedulingProperties.isTaskActive(IotPlatformMetadata.SchedulingKeys.COLLECT_IOT_PLATFORM))
+            taskScheduler.execute(() -> dataCollector.collectIotPlatform(30 * 24));
     }
 
     /**
@@ -40,6 +46,7 @@ public class DataManager {
     @Scheduled(cron = "0 * * * * ?")
     public void minuteTask() {
         //采集物联网平台
-        taskScheduler.execute(() -> dataCollector.collectIotPlatform(1));
+        if (schedulingProperties.isTaskActive(IotPlatformMetadata.SchedulingKeys.COLLECT_IOT_PLATFORM))
+            taskScheduler.execute(() -> dataCollector.collectIotPlatform(1));
     }
 }

+ 10 - 3
bespoke-gateway-jldma/src/main/java/com/shkpr/service/bespokegateway/jldma/manager/InfoSyncManager.java

@@ -1,6 +1,8 @@
 package com.shkpr.service.bespokegateway.jldma.manager;
 
+import com.shkpr.service.bespokegateway.core.properties.SchedulingProperties;
 import com.shkpr.service.bespokegateway.jldma.components.InfoSynchronizer;
+import com.shkpr.service.bespokegateway.jldma.constants.IotPlatformMetadata;
 import org.springframework.scheduling.annotation.Scheduled;
 import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
 import org.springframework.stereotype.Component;
@@ -16,11 +18,14 @@ import javax.annotation.PostConstruct;
 @Component
 public class InfoSyncManager {
     final
+    SchedulingProperties schedulingProperties;
+    final
     ThreadPoolTaskExecutor taskScheduler;
     final
     InfoSynchronizer infoSynchronizer;
 
-    public InfoSyncManager(ThreadPoolTaskExecutor taskScheduler, InfoSynchronizer infoSynchronizer) {
+    public InfoSyncManager(SchedulingProperties schedulingProperties, ThreadPoolTaskExecutor taskScheduler, InfoSynchronizer infoSynchronizer) {
+        this.schedulingProperties = schedulingProperties;
         this.taskScheduler = taskScheduler;
         this.infoSynchronizer = infoSynchronizer;
     }
@@ -31,7 +36,8 @@ public class InfoSyncManager {
     @PostConstruct
     public void init() {
         //同步设备信息
-        taskScheduler.execute(infoSynchronizer::syncDevices);
+        if (schedulingProperties.isTaskActive(IotPlatformMetadata.SchedulingKeys.SYNC_DEVICES))
+            taskScheduler.execute(infoSynchronizer::syncDevices);
     }
 
     /**
@@ -40,6 +46,7 @@ public class InfoSyncManager {
     @Scheduled(cron = "0 */10 * * * *")
     public void minuteTask() {
         //同步设备信息
-        taskScheduler.execute(infoSynchronizer::syncDevices);
+        if (schedulingProperties.isTaskActive(IotPlatformMetadata.SchedulingKeys.SYNC_DEVICES))
+            taskScheduler.execute(infoSynchronizer::syncDevices);
     }
 }