Explorar o código

upload程序启动加载镜像文件并放入缓存变量中

xusl hai 1 ano
pai
achega
bc70e188b8

+ 13 - 4
ipfcst-common/ipfcst-common-data/src/main/java/com/jiayue/ipfcst/common/data/util/CommonDataUtil.java

@@ -31,6 +31,13 @@ public class CommonDataUtil {
    * 读取基础镜像文件公共方法
    * @throws Exception
    */
+  public static boolean imageFileExist(String imageName) throws Exception {
+    return new File(getImageFilePath()+File.separator+imageName+".txt").exists();
+  }
+  /**
+   * 读取基础镜像文件公共方法
+   * @throws Exception
+   */
   public static List<? extends Object> readBaseInfoImage(String imageName) throws Exception {
     String imageFilePath = getImageFilePath();
     String path = "com.jiayue.ipfcst.common.data.entity.";
@@ -54,10 +61,11 @@ public class CommonDataUtil {
   /**
    * 获取文件创建时间
    *
-   * @param fullFileName
+   * @param imageName
    * @return
    */
-  public static Long getFileCreateTime(String fullFileName) throws Exception{
+  public static Long getImageFileCreateTime(String imageName) throws Exception{
+    String fullFileName = getImageFilePath()+File.separator+imageName+".txt";
     Path path = Paths.get(fullFileName);
     BasicFileAttributeView basicview = Files.getFileAttributeView(path, BasicFileAttributeView.class, LinkOption.NOFOLLOW_LINKS);
     BasicFileAttributes attr = basicview.readAttributes();
@@ -69,10 +77,11 @@ public class CommonDataUtil {
   /**
    * 获取文件创建时间
    *
-   * @param fullFileName
+   * @param imageName
    * @return
    */
-  public static Long getFileModifyTime(String fullFileName) throws Exception{
+  public static Long getFileModifyTime(String imageName) throws Exception{
+    String fullFileName = getImageFilePath()+File.separator+imageName+".txt";
     File file = new File(fullFileName);
     long modifiedTime = file.lastModified();
     return modifiedTime;

+ 2 - 2
ipfcst-upload/src/main/java/com/jiayue/ipfcst/upload/config/AppenderFactory.java

@@ -8,8 +8,8 @@ import ch.qos.logback.core.rolling.RollingFileAppender;
 import ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy;
 import ch.qos.logback.core.util.FileSize;
 import ch.qos.logback.core.util.OptionHelper;
+import com.jiayue.ipfcst.common.data.util.CommonDataUtil;
 import com.jiayue.ipfcst.upload.constants.CacheConstant;
-import com.jiayue.ipfcst.upload.util.FileUtil;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Component;
@@ -73,7 +73,7 @@ public class AppenderFactory {
     //设置文件创建时间及大小的类
     SizeAndTimeBasedRollingPolicy policy = new SizeAndTimeBasedRollingPolicy();
     //文件名格式
-    String fp = OptionHelper.substVars(FileUtil.getLogsPath() + "/" + "%d{yyyy-MM-dd}" + "/" + "upload-" + type + "-" + name + "-" + no + ".%d{yyyy-MM-dd}.%i.log", context);
+    String fp = OptionHelper.substVars(CommonDataUtil.getLogsPath() + "/" + "%d{yyyy-MM-dd}" + "/" + "upload-" + type + "-" + name + "-" + no + ".%d{yyyy-MM-dd}.%i.log", context);
     //最大日志文件大小
     policy.setMaxFileSize(FileSize.valueOf(this.maxFileSize));
     //设置文件名模式

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

@@ -0,0 +1,98 @@
+package com.jiayue.ipfcst.upload.config;
+
+import com.jiayue.ipfcst.common.data.entity.*;
+import com.jiayue.ipfcst.common.data.util.CommonDataUtil;
+import com.jiayue.ipfcst.upload.constants.CacheConstant;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.context.annotation.Bean;
+import org.springframework.stereotype.Component;
+import java.util.List;
+
+
+/**
+ * 启动加载镜像文件
+ *
+ * @author xsl
+ * @since 2024/03/26
+ */
+@Component
+@Slf4j
+public class StartLoadImageFile {
+    @Bean
+    public void loadFile() throws Exception {
+        log.info("upload启动加载镜像文件。。。");
+        if (CommonDataUtil.imageFileExist("ElectricField")){
+            List<ElectricField> electricFieldList = (List<ElectricField>) CommonDataUtil.readBaseInfoImage("ElectricField");
+            if (electricFieldList != null) {
+                // 加入缓存
+                ElectricField electricField = electricFieldList.get(0);
+                CacheConstant.electricField = electricField;
+                Long ct = CommonDataUtil.getImageFileCreateTime("ElectricField");
+                CacheConstant.imageFileTimeMap.put("ElectricField",ct);
+            } else {
+                throw new Exception("镜像文件ElectricField内容为空,无法启动程序");
+            }
+        }
+        else{
+            throw new Exception("缺失镜像文件ElectricField信息,无法启动程序");
+        }
+
+        if (CommonDataUtil.imageFileExist("UploadObject")){
+            List<UploadObject> uploadObjectList = (List<UploadObject>) CommonDataUtil.readBaseInfoImage("UploadObject");
+            if (uploadObjectList != null) {
+                // 加入缓存
+                CacheConstant.uploadObjectList = uploadObjectList;
+                Long ct = CommonDataUtil.getImageFileCreateTime("UploadObject");
+                CacheConstant.imageFileTimeMap.put("UploadObject",ct);
+            } else {
+                throw new Exception("镜像文件UploadObject内容为空,无法启动程序");
+            }
+        }
+        else{
+            throw new Exception("缺失镜像文件UploadObject信息,无法启动程序");
+        }
+
+        if (CommonDataUtil.imageFileExist("UploadFileChannel")){
+            List<UploadFileChannel> uploadFileChannelList = (List<UploadFileChannel>) CommonDataUtil.readBaseInfoImage("UploadFileChannel");
+            if (uploadFileChannelList != null) {
+                // 加入缓存
+                CacheConstant.uploadFileChannelList = uploadFileChannelList;
+                Long ct = CommonDataUtil.getImageFileCreateTime("UploadFileChannel");
+                CacheConstant.imageFileTimeMap.put("UploadFileChannel",ct);
+            } else {
+                throw new Exception("镜像文件UploadFileChannel内容为空,无法启动程序");
+            }
+        }
+        else{
+            throw new Exception("缺失镜像文件UploadFileChannel信息,无法启动程序");
+        }
+
+        if (CommonDataUtil.imageFileExist("UploadFileCode")){
+            List<UploadFileCode> uploadFileCodeList = (List<UploadFileCode>) CommonDataUtil.readBaseInfoImage("UploadFileCode");
+            if (uploadFileCodeList != null) {
+                // 加入缓存
+                CacheConstant.uploadFileCodeList = uploadFileCodeList;
+                Long ct = CommonDataUtil.getImageFileCreateTime("UploadFileCode");
+                CacheConstant.imageFileTimeMap.put("UploadFileCode",ct);
+            }
+            else {
+                throw new Exception("镜像文件UploadFileCode内容为空,无法启动程序");
+            }
+        }
+
+        if (CommonDataUtil.imageFileExist("UploadURL")){
+            List<UploadURL> uploadURLList = (List<UploadURL>) CommonDataUtil.readBaseInfoImage("UploadURL");
+            if (uploadURLList != null) {
+                // 加入缓存
+                CacheConstant.uploadURLList = uploadURLList;
+                Long ct = CommonDataUtil.getImageFileCreateTime("UploadURL");
+                CacheConstant.imageFileTimeMap.put("UploadURL",ct);
+            }
+            else {
+                throw new Exception("镜像文件UploadURL内容为空,无法启动程序");
+            }
+        }
+
+        log.info("upload启动加载镜像文件结束");
+    }
+}

+ 10 - 0
ipfcst-upload/src/main/java/com/jiayue/ipfcst/upload/constants/CacheConstant.java

@@ -1,8 +1,10 @@
 package com.jiayue.ipfcst.upload.constants;
 
 import ch.qos.logback.classic.Logger;
+import com.jiayue.ipfcst.common.data.entity.*;
 import lombok.extern.slf4j.Slf4j;
 
+import java.util.List;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.ConcurrentMap;
 
@@ -14,6 +16,14 @@ import java.util.concurrent.ConcurrentMap;
  */
 @Slf4j
 public class CacheConstant {
+    // 保存上报对象日志实例
     public static ConcurrentMap<String, Logger> uploadLogMap = new ConcurrentHashMap<>();
+    public static ElectricField electricField = null;
+    public static List<UploadObject> uploadObjectList = null;
+    public static List<UploadFileChannel> uploadFileChannelList = null;
+    public static List<UploadFileCode> uploadFileCodeList = null;
+    public static List<UploadURL> uploadURLList = null;
 
+    // 镜像文件创建时间<镜像名称,创建时间>
+    public static ConcurrentMap<String, Long> imageFileTimeMap = new ConcurrentHashMap<>();
 }

+ 0 - 349
ipfcst-upload/src/main/java/com/jiayue/ipfcst/upload/util/FileUtil.java

@@ -1,349 +0,0 @@
-package com.jiayue.ipfcst.upload.util;
-
-import cn.hutool.core.util.CharsetUtil;
-import lombok.extern.slf4j.Slf4j;
-import org.apache.commons.io.FileUtils;
-import org.apache.commons.io.IOUtils;
-import org.springframework.util.ResourceUtils;
-
-import java.io.*;
-import java.net.URLDecoder;
-import java.nio.charset.Charset;
-import java.nio.file.*;
-import java.nio.file.attribute.BasicFileAttributeView;
-import java.nio.file.attribute.BasicFileAttributes;
-import java.nio.file.attribute.PosixFileAttributeView;
-import java.nio.file.attribute.PosixFileAttributes;
-import java.security.MessageDigest;
-import java.util.Date;
-
-/**
- * 文件工具类
- *
- * @author xsl
- * @version 3.0
- */
-@Slf4j
-public class FileUtil {
-  /**
-   * 移动本地文件
-   *
-   * @param remoteAbsoluteFile 远程文件名(包括完整路径)
-   * @param localAbsoluteFile  本地文件名(包括完整路径)
-   * @return 成功时,返回true,失败返回false
-   * @throws Exception
-   */
-  public static void move(String localAbsoluteFile, String remoteAbsoluteFile) throws Exception {
-//    File srcFile = new File(localAbsoluteFile);
-//    File destDir = new File(remoteAbsoluteFile);
-//    try {
-//      if (!destDir.exists()) {// 如果目录不存在则创建目录
-//        boolean b = destDir.mkdirs();
-//        if (!b) // 如果创建失败则抛出异常
-//          throw new RuntimeException(destDir + " 目录创建失败");
-//      }
-//      FileUtils.moveFile(srcFile, new File(remoteAbsoluteFile + File.separator + srcFile.getName()));
-//    } catch (IOException e) {
-//      throw new Exception("文件:" + srcFile.getName() + "移动到" + destDir.getPath() + "失败。", e);
-//    }
-    move(localAbsoluteFile, remoteAbsoluteFile, true);
-  }
-
-  public static void move(String localAbsoluteFile, String remoteAbsoluteFile, boolean overwrite) throws Exception {
-    File srcFile = new File(localAbsoluteFile);
-    File destDir = new File(remoteAbsoluteFile);
-    StringBuilder opts = new StringBuilder();
-    try {
-      if (!destDir.exists()) {// 如果目录不存在则创建目录
-        boolean b = destDir.mkdirs();
-        if (!b){ // 如果创建失败则抛出异常
-          throw new RuntimeException(destDir + " 目录创建失败");
-        }
-      }
-      File desFile = new File(remoteAbsoluteFile + File.separator + srcFile.getName());
-      if (overwrite && desFile.exists()) {
-        FileUtils.deleteQuietly(desFile);
-        opts.append(" >> 覆盖 >> ");
-      } else {
-        opts.append(" >> 移动 >> ");
-      }
-      FileUtils.moveFile(srcFile, desFile);
-      log.info("文件:" + srcFile.getName() + opts + destDir.getPath());
-    } catch (IOException e) {
-      throw new Exception("文件:" + srcFile.getName() + opts + destDir.getPath() + "失败。", e);
-    }
-  }
-
-  public static InputStream toInputStream(File file, Charset source, Charset target) {
-    InputStream inputStream = null;
-    try {
-      if (file.exists() && file.canRead()) {
-        String context = FileUtils.readFileToString(file, source);
-        inputStream = null == context ? null : IOUtils.toInputStream(context, target);
-      }
-    } catch (IOException e) {
-      throw new RuntimeException(e);
-    }
-    return inputStream;
-  }
-
-  public static File convert(File file, Charset source, Charset target) {
-    if (file.exists() && file.canRead() && file.canWrite()) {
-      return CharsetUtil.convert(file, source, target);
-    }
-    return null;
-  }
-
-  //文件内容md5加密
-  public static String getMD5(byte[] fileByte) {
-    String md5 = new String();
-    try {
-      MessageDigest md = MessageDigest.getInstance("MD5");
-      md.update(fileByte);
-      byte b[] = md.digest();
-
-      int i;
-
-      StringBuffer buf = new StringBuffer("");
-      for (int offset = 0; offset < b.length; offset++) {
-        i = b[offset];
-        if (i < 0) {
-          i += 256;
-        }
-        if (i < 16) {
-          buf.append("0");
-        }
-        buf.append(Integer.toHexString(i));
-      }
-
-      md5 = buf.toString();
-
-    } catch (Exception e) {
-      e.printStackTrace();
-    }
-    return md5;
-  }
-
-  /**
-   * 获取文件创建时间
-   *
-   * @param fullFileName
-   * @return
-   */
-  public static Long getFileCreateTime(String fullFileName) {
-    Path path = Paths.get(fullFileName);
-    BasicFileAttributeView basicview = Files.getFileAttributeView(path, BasicFileAttributeView.class, LinkOption.NOFOLLOW_LINKS);
-    BasicFileAttributes attr;
-    try {
-      attr = basicview.readAttributes();
-      Date createDate = new Date(attr.creationTime().toMillis());
-      return createDate.getTime();
-    } catch (Exception e) {
-      e.printStackTrace();
-    }
-    return System.currentTimeMillis();
-  }
-
-  /**
-   * 获取项目根路径
-   *
-   * @return
-   */
-  public static String getResourceBasePath() {
-    // 获取跟目录
-    File path = null;
-    try {
-      path = new File(ResourceUtils.getURL("classpath:").getPath());
-
-    } catch (FileNotFoundException e) {
-      // nothing to do
-    }
-    if (path == null || !path.exists()) {
-      path = new File("");
-    }
-
-    String pathStr = path.getAbsolutePath();
-    try {
-      pathStr = URLDecoder.decode(pathStr, "UTF-8");
-    } catch (UnsupportedEncodingException e) {
-      e.printStackTrace();
-    }
-    // 如果是在eclipse中运行,则和target同级目录,如果是jar部署到服务器,则默认和jar包同级
-//    pathStr = pathStr.replace("\\target\\classes", "");
-
-    return pathStr;
-  }
-
-  /**
-   * 获取上报文件目录相对路径
-   *
-   * @return
-   */
-  public static String getFileUploadPath() {
-    return createUploadAllDir("uploadFile");
-  }
-
-  /**
-   * 获取下载文件目录相对路径
-   *
-   * @return
-   */
-  public static String getDownloadFilePath() {
-    return createUploadAllDir("downloadFile");
-  }
-
-  /**
-   * 获取下载文件目录相对路径
-   *
-   * @return
-   */
-  public static String getZxglFilePath() {
-    return createUploadAllDir("zxgl");
-  }
-
-  /**
-   * 获取日志目录相对路径
-   *
-   * @return
-   */
-  public static String getLogsPath() {
-    return createUploadAllDir("logs");
-  }
-
-  /**
-   * 获取系统脚本相对路径
-   *
-   * @return
-   */
-  public static String getBinPath() {
-    return createUploadAllDir("bin");
-  }
-
-  /**
-   * 程序备份相对路径
-   *
-   * @return
-   */
-  public static String getOriginalPath() {
-    return createUploadAllDir("original");
-  }
-
-  /**
-   * 系统资料相对路径
-   *
-   * @return
-   */
-  public static String getSystemFilePath() {
-    return createUploadAllDir("systemFile");
-  }
-
-  /**
-   * 获取被删除文件目录
-   *
-   * @return 路径
-   */
-  public static String getDeleteFilePath() {
-    return createUploadAllDir("deleteFile");
-  }
-
-  /**
-   * 程序运行相对路径
-   *
-   * @return
-   */
-  public static String getProducePath() {
-    return createUploadAllDir("produce");
-  }
-
-  /**
-   * 报表相对路径
-   *
-   * @return
-   */
-  public static String getreportFormPath() {
-    return createUploadAllDir("reportForm");
-  }
-
-  /**
-   * 获取临时文件目录相对路径
-   *
-   * @return
-   */
-  public static String getTempFilePath() {
-    return createUploadAllDir("tempFile");
-  }
-
-  public static String getE46Path() {
-    return createUploadAllDir("HN");
-  }
-
-  private static String createUploadAllDir(String dir) {
-    String path = "";
-    if (System.getProperties().getProperty("file.separator").equals("\\")) {
-      path = new File(getResourceBasePath()).getParentFile().getParentFile().getParentFile().getAbsolutePath() + File.separator + dir;
-      try {
-        path = URLDecoder.decode(path, "UTF-8");
-      } catch (UnsupportedEncodingException e) {
-        e.printStackTrace();
-      }
-      File file = new File(path);
-      if (!file.exists()) {
-        boolean b = file.mkdirs();
-        if (!b) {
-          log.error("目录创建失败" + path);
-        }
-      }
-    } else {
-      path = "/home/syjy/ipfcstV3/" + dir;
-    }
-    return path;
-  }
-
-  /**
-   * 获取环境数据导出文件目录相对路径
-   *
-   * @return
-   */
-  public static String getEnvironmentalDataFile() {
-    return createUploadAllDir("environmentalDataFile");
-  }
-
-
-  /**
-   * 设置用户和用户组
-   *
-   * @param filePath 文件路径
-   * @param owner    用户
-   * @param group    用户组
-   */
-  public static void setFileOwnerAndGroup(String filePath, String owner, String group) {
-    Path path = FileSystems.getDefault().getPath(filePath);
-
-    try {
-      // 创建文件(如果不存在)
-      if (!Files.exists(path)) {
-        Files.createFile(path);
-      }
-
-      // 获取文件属性视图
-      PosixFileAttributeView attributeView = Files.getFileAttributeView(
-        path, PosixFileAttributeView.class);
-
-      // 获取当前的文件属性
-      PosixFileAttributes attributes = attributeView.readAttributes();
-
-      // 设置文件所有者和用户组
-      attributeView.setOwner(FileSystems.getDefault().getUserPrincipalLookupService().lookupPrincipalByName(owner));
-      attributeView.setGroup(FileSystems.getDefault().getUserPrincipalLookupService().lookupPrincipalByGroupName(group));
-
-      log.info("{}文件所有者设置为: {},用户组设置为:{}" + filePath, attributeView.getOwner(), attributes.group());
-
-    } catch (Exception e) {
-      e.printStackTrace();
-    }
-  }
-
-  // 可以在这里添加其他实用方法
-
-
-}

+ 14 - 16
ipfcst-upload/src/test/java/org/example/AppTest.java

@@ -1,5 +1,11 @@
 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 junit.framework.Test;
 import junit.framework.TestCase;
 import junit.framework.TestSuite;
@@ -8,6 +14,8 @@ import java.io.BufferedReader;
 import java.io.File;
 import java.io.FileReader;
 import java.io.IOException;
+import java.util.List;
+import java.util.Map;
 
 /**
  * Unit test for simple App.
@@ -37,22 +45,12 @@ public class AppTest
      * Rigourous Test :-)
      */
     public void testApp() throws Exception{
-        File file1 = new File("d:\\test\\file1.txt");
-        file1.delete();
-//        while (true) {
-//            // 读取文件
-////            FileReader file = new FileReader("d:\\test\\test.txt"); // 创建文件对象
-////            if (file.exists()){
-//                try (BufferedReader reader = new BufferedReader(new FileReader("d:\\test\\test.txt"))) {
-//                    String line;
-//                    while ((line = reader.readLine()) != null) {
-//                        System.out.println(line);
-//                    }
-//                } catch (IOException e) {
-//                    e.printStackTrace();
-//                }
-//                System.out.println("===================读取完成===================");
-////            }
+//        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"));
     }
 }