Parcourir la source

1.统一异常处理
2.添加文件是否在uploadfile目录下检查方法

jyyw il y a 1 an
Parent
commit
0d01835add

+ 10 - 11
ipfcst-spare2/src/main/java/com/jiayue/ipfcst/spare2/base4/BaseRepository.java

@@ -1,10 +1,13 @@
 package com.jiayue.ipfcst.spare2.base4;
 
-import com.jiayue.ipfcst.spare2.util.FileUtil;
+import cn.hutool.core.io.FileUtil;
+import com.jiayue.ipfcst.spare2.util.ExceptionsUtil;
+import com.jiayue.ipfcst.spare2.util.FilesUtil;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.cache.ehcache.EhCacheCacheManager;
 import org.springframework.stereotype.Repository;
 
+import java.io.File;
 import java.io.Serializable;
 import java.util.List;
 
@@ -14,18 +17,14 @@ public class BaseRepository implements Serializable {
     @Autowired
     public EhCacheCacheManager ehCacheCacheManager;
 
-    public <T> Exception newImageFileContentEmptyException(Class<T> t) {
-        return new Exception("基础信息:镜像文件" + t.getSimpleName() + "内容为空,无法启动程序");
-    }
-
-    public <T> Exception newImageCacheContentEmptyException(Class<T> t) {
-        return new Exception("基础信息:镜像缓存" + t.getSimpleName() + "内容为空,无法启动程序");
-    }
-
     public <T> List<T> readImageToList(Class<T> clazz) throws Exception {
-        List<T> list = (List<T>) FileUtil.readBaseInfoImage(clazz.getSimpleName());
+        File file = FileUtil.file(FilesUtil.getImageFilePath() + File.separator + FilesUtil.imageFileName(clazz));
+        if (!file.exists() || !file.isFile()) {
+            throw ExceptionsUtil.newImageFileNotFondException(clazz);
+        }
+        List<T> list = (List<T>) FilesUtil.readBaseInfoImage(clazz.getSimpleName());
         if (null == list) {
-            throw newImageFileContentEmptyException(clazz);
+            throw ExceptionsUtil.newImageFileContentEmptyException(clazz);
         }
         return list;
     }

+ 5 - 2
ipfcst-spare2/src/main/java/com/jiayue/ipfcst/spare2/baseinfos/service/ImageLoadeService.java → ipfcst-spare2/src/main/java/com/jiayue/ipfcst/spare2/common/service/ImageInfoLoadeService.java

@@ -1,14 +1,17 @@
-package com.jiayue.ipfcst.spare2.baseinfos.service;
+package com.jiayue.ipfcst.spare2.common.service;
 
+import com.jiayue.ipfcst.common.data.entity.AGC_AVCInfo;
 import com.jiayue.ipfcst.common.data.entity.ElectricField;
 import com.jiayue.ipfcst.spare2.base4.BaseService;
+import com.jiayue.ipfcst.spare2.baseinfos.service.*;
+import com.jiayue.ipfcst.spare2.util.FilesUtil;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
 @Slf4j
 @Service
-public class ImageLoadeService extends BaseService {
+public class ImageInfoLoadeService extends BaseService {
 
     @Autowired
     AGC_AVCInfoService agcAvcInfoService;

+ 5 - 5
ipfcst-spare2/src/main/java/com/jiayue/ipfcst/spare2/job/ConsoleCheckUpJob.java

@@ -10,8 +10,8 @@ import cn.hutool.json.JSONUtil;
 import com.jiayue.ipfcst.common.data.dto.ProcessResult;
 import com.jiayue.ipfcst.common.data.util.ShellUtil;
 import com.jiayue.ipfcst.spare2.base4.BaseService;
-import com.jiayue.ipfcst.spare2.baseinfos.service.ImageLoadeService;
-import com.jiayue.ipfcst.spare2.util.FileUtil;
+import com.jiayue.ipfcst.spare2.common.service.ImageInfoLoadeService;
+import com.jiayue.ipfcst.spare2.util.FilesUtil;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.scheduling.annotation.EnableScheduling;
@@ -30,7 +30,7 @@ import java.util.Map;
 public class ConsoleCheckUpJob extends BaseService {
 
     @Autowired
-    ImageLoadeService imageLoadeService;
+    ImageInfoLoadeService imageInfoLoadeService;
 
     @Scheduled(cron = "0/30 * * * * *")
     public void MonitConsoleUp() {
@@ -68,7 +68,7 @@ public class ConsoleCheckUpJob extends BaseService {
         //console进程不存在或者console健康接口返回为异常,启动spare2文件生成逻辑
         if (error) {
             log.info("spare2程序生成文件");
-            imageLoadeService.imageLoad();
+            imageInfoLoadeService.imageLoad();
         } else {
             log.info("console程序生成文件");
         }
@@ -83,6 +83,6 @@ public class ConsoleCheckUpJob extends BaseService {
         cmds.add("sh");
         cmds.add("-c");
         cmds.add("pgrep -fl ipfcst-console");
-        return ShellUtil.runProcess(cmds, envs, new File(FileUtil.getBinPath()));
+        return ShellUtil.runProcess(cmds, envs, new File(FilesUtil.getBinPath()));
     }
 }

+ 3 - 3
ipfcst-spare2/src/main/java/com/jiayue/ipfcst/spare2/job/UploadAppUpJob.java

@@ -3,7 +3,7 @@ package com.jiayue.ipfcst.spare2.job;
 import cn.hutool.core.collection.CollectionUtil;
 import com.jiayue.ipfcst.common.data.dto.ProcessResult;
 import com.jiayue.ipfcst.common.data.util.ShellUtil;
-import com.jiayue.ipfcst.spare2.util.FileUtil;
+import com.jiayue.ipfcst.spare2.util.FilesUtil;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.scheduling.annotation.EnableScheduling;
 import org.springframework.scheduling.annotation.Scheduled;
@@ -43,11 +43,11 @@ public class UploadAppUpJob {
         cmds.add("sh");
         cmds.add("-c");
         cmds.add("pgrep -fl ipfcst-upload");
-        return ShellUtil.runProcess(cmds, envs, new File(FileUtil.getBinPath()));
+        return ShellUtil.runProcess(cmds, envs, new File(FilesUtil.getBinPath()));
     }
 
     private ProcessResult runApp() {
-        String script = FileUtil.getBinPath() + File.separator + "restart-upload.sh";
+        String script = FilesUtil.getBinPath() + File.separator + "restart-upload.sh";
         List<String> cmds = new ArrayList<>();
         cmds.add("sh");
         cmds.add("-c");

+ 26 - 0
ipfcst-spare2/src/main/java/com/jiayue/ipfcst/spare2/util/ExceptionsUtil.java

@@ -0,0 +1,26 @@
+package com.jiayue.ipfcst.spare2.util;
+
+import lombok.extern.slf4j.Slf4j;
+
+import java.io.Serializable;
+
+/**
+ * 异常处理及日志输出
+ */
+@Slf4j
+public class ExceptionsUtil implements Serializable {
+
+
+    public static <T> Exception newImageFileContentEmptyException(Class<T> t) {
+        return new Exception("基础信息:镜像文件" + t.getSimpleName() + "内容为空,无法启动程序");
+    }
+
+    public static <T> Exception newImageCacheContentEmptyException(Class<T> t) {
+        return new Exception("基础信息:镜像缓存" + t.getSimpleName() + "内容为空,无法启动程序");
+    }
+
+    public static <T> Exception newImageFileNotFondException(Class<T> t) {
+        return new Exception("基础信息:镜像文件" + FilesUtil.imageFileName(t) + "无法找到,无法启动程序");
+    }
+
+}

+ 13 - 8
ipfcst-spare2/src/main/java/com/jiayue/ipfcst/spare2/util/FileUtil.java → ipfcst-spare2/src/main/java/com/jiayue/ipfcst/spare2/util/FilesUtil.java

@@ -1,5 +1,6 @@
 package com.jiayue.ipfcst.spare2.util;
 
+import cn.hutool.core.io.FileUtil;
 import com.jiayue.ipfcst.common.data.util.CommonDataUtil;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.io.FileUtils;
@@ -22,18 +23,12 @@ import java.util.Date;
  * @version 3.0
  */
 @Slf4j
-public class FileUtil extends CommonDataUtil {
+public class FilesUtil extends CommonDataUtil {
 
     private static final String imageFileSuffix = "txt";
 
     public static <T> String imageFileName(Class<T> clazz) {
-        String name = clazz.getSimpleName() + "." + imageFileSuffix;
-        String full = getImageFilePath() + File.separator + name;
-        File imageFile = new File(full);
-        if (!imageFile.exists()) {
-            log.error("ImageFile : {} dosen`t exist", name);
-        }
-        return name;
+        return clazz.getSimpleName() + "." + imageFileSuffix;
     }
 
     /**
@@ -119,4 +114,14 @@ public class FileUtil extends CommonDataUtil {
         }
         return new Date().getTime();
     }
+
+    /**
+     * 检查uploadfile目录下是否生成过文件{name}
+     * @param name
+     * @return
+     */
+    public static boolean checkUpGened(String name) {
+        return FileUtil.exist(getFileUploadPath(), name);
+    }
+
 }