Quellcode durchsuchen

增加启动加载镜像,定时读取本地镜像进行重启逻辑处理。

xusl vor 1 Jahr
Ursprung
Commit
1b86cca635

+ 5 - 5
ipfcst-upload/src/main/java/com/jiayue/ipfcst/upload/config/StartLoadImageFile.java

@@ -27,7 +27,7 @@ public class StartLoadImageFile {
                 // 加入缓存
                 ElectricField electricField = electricFieldList.get(0);
                 CacheConstant.electricField = electricField;
-                Long ct = CommonDataUtil.getImageFileCreateTime("ElectricField");
+                Long ct = CommonDataUtil.getFileModifyTime("ElectricField");
                 CacheConstant.imageFileTimeMap.put("ElectricField",ct);
             } else {
                 throw new Exception("镜像文件ElectricField内容为空,无法启动程序");
@@ -42,7 +42,7 @@ public class StartLoadImageFile {
             if (uploadObjectList != null) {
                 // 加入缓存
                 CacheConstant.uploadObjectList = uploadObjectList;
-                Long ct = CommonDataUtil.getImageFileCreateTime("UploadObject");
+                Long ct = CommonDataUtil.getFileModifyTime("UploadObject");
                 CacheConstant.imageFileTimeMap.put("UploadObject",ct);
             } else {
                 throw new Exception("镜像文件UploadObject内容为空,无法启动程序");
@@ -57,7 +57,7 @@ public class StartLoadImageFile {
             if (uploadFileChannelList != null) {
                 // 加入缓存
                 CacheConstant.uploadFileChannelList = uploadFileChannelList;
-                Long ct = CommonDataUtil.getImageFileCreateTime("UploadFileChannel");
+                Long ct = CommonDataUtil.getFileModifyTime("UploadFileChannel");
                 CacheConstant.imageFileTimeMap.put("UploadFileChannel",ct);
             } else {
                 throw new Exception("镜像文件UploadFileChannel内容为空,无法启动程序");
@@ -72,7 +72,7 @@ public class StartLoadImageFile {
             if (uploadFileCodeList != null) {
                 // 加入缓存
                 CacheConstant.uploadFileCodeList = uploadFileCodeList;
-                Long ct = CommonDataUtil.getImageFileCreateTime("UploadFileCode");
+                Long ct = CommonDataUtil.getFileModifyTime("UploadFileCode");
                 CacheConstant.imageFileTimeMap.put("UploadFileCode",ct);
             }
             else {
@@ -85,7 +85,7 @@ public class StartLoadImageFile {
             if (uploadURLList != null) {
                 // 加入缓存
                 CacheConstant.uploadURLList = uploadURLList;
-                Long ct = CommonDataUtil.getImageFileCreateTime("UploadURL");
+                Long ct = CommonDataUtil.getFileModifyTime("UploadURL");
                 CacheConstant.imageFileTimeMap.put("UploadURL",ct);
             }
             else {

+ 5 - 1
ipfcst-upload/src/main/java/com/jiayue/ipfcst/upload/constants/CacheConstant.java

@@ -24,6 +24,10 @@ public class CacheConstant {
     public static List<UploadFileCode> uploadFileCodeList = null;
     public static List<UploadURL> uploadURLList = null;
 
-    // 镜像文件创建时间<镜像名称,创建时间>
+    // 镜像文件修改时间<镜像名称,修改时间>
     public static ConcurrentMap<String, Long> imageFileTimeMap = new ConcurrentHashMap<>();
+    // 重启状态标识
+    public static boolean restartState = false;
+    // 上报文件状态<文件hashcode,上报对象id>
+    public static ConcurrentMap<String, String> uploadFileStateMap = new ConcurrentHashMap<>();
 }

+ 18 - 5
ipfcst-upload/src/main/java/com/jiayue/ipfcst/upload/job/ReadLocalFileJob.java

@@ -3,7 +3,9 @@ package com.jiayue.ipfcst.upload.job;
 
 import com.jiayue.ipfcst.common.data.util.CommonDataUtil;
 import com.jiayue.ipfcst.upload.constants.CacheConstant;
+import com.jiayue.ipfcst.upload.service.restart.RestartUploadService;
 import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.scheduling.annotation.EnableScheduling;
 import org.springframework.scheduling.annotation.Scheduled;
 import org.springframework.stereotype.Service;
@@ -19,6 +21,8 @@ import org.springframework.stereotype.Service;
 @Service
 @EnableScheduling
 public class ReadLocalFileJob {
+    @Autowired
+    RestartUploadService restartUploadService;
 
     @Scheduled(fixedDelay = 10000)
     public void execute() throws Exception {
@@ -27,50 +31,59 @@ public class ReadLocalFileJob {
         // 获取文件创建时间
         if (CommonDataUtil.imageFileExist("ElectricField")) {
             // 判断场站镜像是否改变
-            long ct = CommonDataUtil.getImageFileCreateTime("ElectricField");
+            long ct = CommonDataUtil.getFileModifyTime("ElectricField");
             if (CacheConstant.imageFileTimeMap.get("ElectricField").longValue() != ct) {
                 // 重启程序
+                restartUploadService.restart();
             }
         }
         if (CommonDataUtil.imageFileExist("UploadObject")) {
             // 判断场站镜像是否改变
-            long ct = CommonDataUtil.getImageFileCreateTime("UploadObject");
+            long ct = CommonDataUtil.getFileModifyTime("UploadObject");
             if (CacheConstant.imageFileTimeMap.get("UploadObject").longValue() != ct) {
                 // 重启程序
+                restartUploadService.restart();
             }
         }
         if (CommonDataUtil.imageFileExist("UploadFileChannel")) {
             // 判断场站镜像是否改变
-            long ct = CommonDataUtil.getImageFileCreateTime("UploadFileChannel");
+            long ct = CommonDataUtil.getFileModifyTime("UploadFileChannel");
             if (CacheConstant.imageFileTimeMap.get("UploadFileChannel").longValue() != ct) {
                 // 重启程序
+                restartUploadService.restart();
             }
         }
 
         if (CommonDataUtil.imageFileExist("UploadFileCode") && CacheConstant.imageFileTimeMap.get("UploadFileCode")!=null) {
-            long ct = CommonDataUtil.getImageFileCreateTime("UploadFileCode");
+            long ct = CommonDataUtil.getFileModifyTime("UploadFileCode");
             if (CacheConstant.imageFileTimeMap.get("UploadFileCode").longValue() != ct) {
                 // 重启程序
+                restartUploadService.restart();
             }
         }
         else if (!CommonDataUtil.imageFileExist("UploadFileCode") && CacheConstant.imageFileTimeMap.get("UploadFileCode")!=null){
             // 缓存有,镜像无,重启程序
+            restartUploadService.restart();
         }
         else if (CommonDataUtil.imageFileExist("UploadFileCode") && CacheConstant.imageFileTimeMap.get("UploadFileCode")==null){
             // 缓存无,镜像有,重启程序
+            restartUploadService.restart();
         }
 
         if (CommonDataUtil.imageFileExist("UploadURL") && CacheConstant.imageFileTimeMap.get("UploadURL")!=null) {
-            long ct = CommonDataUtil.getImageFileCreateTime("UploadURL");
+            long ct = CommonDataUtil.getFileModifyTime("UploadURL");
             if (CacheConstant.imageFileTimeMap.get("UploadURL").longValue() != ct) {
                 // 重启程序
+                restartUploadService.restart();
             }
         }
         else if (!CommonDataUtil.imageFileExist("UploadURL") && CacheConstant.imageFileTimeMap.get("UploadURL")!=null){
             // 缓存有,镜像无,重启程序
+            restartUploadService.restart();
         }
         else if (CommonDataUtil.imageFileExist("UploadURL") && CacheConstant.imageFileTimeMap.get("UploadURL")==null){
             // 缓存无,镜像有,重启程序
+            restartUploadService.restart();
         }
         log.info("读取镜像文件结束");
     }

+ 40 - 0
ipfcst-upload/src/main/java/com/jiayue/ipfcst/upload/service/restart/RestartUploadService.java

@@ -0,0 +1,40 @@
+package com.jiayue.ipfcst.upload.service.restart;
+
+import com.jiayue.ipfcst.upload.constants.CacheConstant;
+import lombok.SneakyThrows;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.stereotype.Service;
+
+import java.util.concurrent.TimeUnit;
+
+/**
+ * 重启程序服务类
+ *
+ * @author xsl
+ * @since 2024/03/27
+ */
+@Service
+@Slf4j
+public class RestartUploadService {
+    @SneakyThrows
+    public void restart() {
+        log.info("对upload执行重启任务");
+        // 设定重启状态
+        CacheConstant.restartState = true;
+        while(true){
+            // 检测通道是否可以重启
+            if (CacheConstant.uploadFileStateMap.size()==0){
+                break;
+            }
+            Thread.sleep(2000);
+        }
+
+        String cmd = "sh /home/syjy/ipfcstV3/bin/restart-upload.sh";
+        Process process = null;
+        process = Runtime.getRuntime().exec(cmd);
+        if(!process.waitFor(300, TimeUnit.SECONDS)){
+            log.info("执行重启upload超时!");
+            process.destroy();
+        }
+    }
+}

+ 66 - 21
ipfcst-upload/src/test/java/org/example/AppTest.java

@@ -1,21 +1,13 @@
 package org.example;
 
-import com.jiayue.ipfcst.common.core.util.JsonBeanUtil;
-import com.jiayue.ipfcst.common.data.entity.PvModuleModel;
-import com.jiayue.ipfcst.common.data.entity.UploadFileChannel;
-import com.jiayue.ipfcst.common.data.entity.UploadURL;
-import com.jiayue.ipfcst.common.data.entity.gathertunnelinfo.Gather104TcpTunnelInfo;
-import com.jiayue.ipfcst.common.data.util.CommonDataUtil;
+import com.jiayue.ipfcst.upload.constants.CacheConstant;
 import junit.framework.Test;
 import junit.framework.TestCase;
 import junit.framework.TestSuite;
 
-import java.io.BufferedReader;
-import java.io.File;
-import java.io.FileReader;
-import java.io.IOException;
-import java.util.List;
+import java.util.Iterator;
 import java.util.Map;
+import java.util.concurrent.atomic.AtomicInteger;
 
 /**
  * Unit test for simple App.
@@ -45,15 +37,68 @@ public class AppTest
      * Rigourous Test :-)
      */
     public void testApp() throws Exception{
-//        List<UploadURL> uploadURLList = (List<UploadURL>)CommonDataUtil.readBaseInfoImage("UploadURL");
-//        for (UploadURL uploadURL:uploadURLList){
-//            System.out.println(uploadURL.toString());
-//        }
-
-//        System.out.println(CommonDataUtil.imageFileExist("UploadURL"));
-//        System.out.println(CommonDataUtil.getImageFileCreateTime("UploadURL11111"));
-        Long a=112233456L;
-        long b=a;
-        System.out.println(b);
+
     }
+
+
+    public static void main(String[] args) {
+        Thread t1 = new Thread(()-> {
+            int i= 0;
+            while (true){
+                CacheConstant.uploadFileStateMap.put(i+"",i+"");
+                i++;
+                try {
+                    Thread.sleep(1000L);
+                } catch (InterruptedException e) {
+                    throw new RuntimeException(e);
+                }
+            }
+        });
+        t1.start();
+
+        try {
+            Thread.sleep(100L);
+        } catch (InterruptedException e) {
+            throw new RuntimeException(e);
+        }
+
+        Thread t2 = new Thread(()-> {
+            int j=0;
+            while (true){
+                Iterator<Map.Entry<String, String>> iterator = CacheConstant.uploadFileStateMap.entrySet().iterator();
+                while (iterator.hasNext()) {
+                    Map.Entry<String, String> entry = iterator.next();
+                    if (entry.getKey().equals(j+"")){
+                        iterator.remove();
+                    }
+                }
+                j++;
+                try {
+                    Thread.sleep(1000L);
+                } catch (InterruptedException e) {
+                    throw new RuntimeException(e);
+                }
+            }
+        });
+        t2.start();
+
+        while (true){
+            try {
+                Thread.sleep(3000L);
+                System.out.println("当前map数量:"+CacheConstant.uploadFileStateMap.size());
+                Iterator<Map.Entry<String, String>> contentMap = CacheConstant.uploadFileStateMap.entrySet().iterator();
+                while (contentMap.hasNext()) {
+                    Map.Entry<String, String> entry = contentMap.next();
+                    System.out.println("key:"+entry.getKey());
+                }
+                System.out.println("==========================");
+            } catch (InterruptedException e) {
+                throw new RuntimeException(e);
+            }
+        }
+
+
+
+    }
+
 }