|
@@ -0,0 +1,210 @@
|
|
|
+package com.emato.file.tunnel.scheduler;
|
|
|
+
|
|
|
+import cn.hutool.core.collection.CollectionUtil;
|
|
|
+import cn.hutool.core.exceptions.UtilException;
|
|
|
+import cn.hutool.core.io.FileUtil;
|
|
|
+import cn.hutool.core.util.XmlUtil;
|
|
|
+import cn.hutool.json.JSONUtil;
|
|
|
+import com.emato.file.tunnel.config.properties.CPortProperties;
|
|
|
+import com.emato.file.tunnel.config.properties.RabbitMQProperties;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.amqp.rabbit.core.RabbitTemplate;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.scheduling.annotation.Scheduled;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+import org.springframework.util.CollectionUtils;
|
|
|
+import org.w3c.dom.Document;
|
|
|
+
|
|
|
+import javax.annotation.PostConstruct;
|
|
|
+import java.io.*;
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 扫描回执目录 定时任务
|
|
|
+ * @author lhm
|
|
|
+ * @version 1.0
|
|
|
+ * 2021-08-24 16:21
|
|
|
+ */
|
|
|
+@Component
|
|
|
+@Slf4j
|
|
|
+public class ScanPullDirectoryTask {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private CPortProperties cPortProperties;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private RabbitTemplate receiptRabbitTemplate;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private RabbitMQProperties rabbitMQProperties;
|
|
|
+
|
|
|
+ @Scheduled(fixedRate = 3000)
|
|
|
+ public void scanDirectory() {
|
|
|
+ String pullDir = cPortProperties.getPullDir();
|
|
|
+ log.info("----- 开始扫描目录: {} -----", pullDir);
|
|
|
+ List<String> fileNames = FileUtil.listFileNames(pullDir);
|
|
|
+ // 根据设置的值处理多少个文件
|
|
|
+ if (CollectionUtil.isEmpty(fileNames)) {
|
|
|
+ log.info("----- 扫描目录完成, 暂无回执文件! -----");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ if (fileNames.size() >= cPortProperties.getScanHandleSize()) {
|
|
|
+ fileNames = fileNames.subList(0, cPortProperties.getScanHandleSize());
|
|
|
+ }
|
|
|
+ // 获取回执目录的所有文件名
|
|
|
+ List<String> pullFilePaths = fileNames.parallelStream().map(fileName -> pullDir + "\\" + fileName).filter(fileName -> fileName.indexOf("xml") > 0).collect(Collectors.toList());
|
|
|
+ pullFilePaths.forEach(pullFilePath -> {
|
|
|
+ File file = FileUtil.file(pullFilePath);
|
|
|
+ HashMap<String, String> map = new HashMap<>();
|
|
|
+ String fileName = file.getName();
|
|
|
+ InputStream inputStream = null;
|
|
|
+ try {
|
|
|
+ if (file.exists()) {
|
|
|
+ inputStream = new FileInputStream(file);
|
|
|
+ Document document = XmlUtil.readXML(inputStream);
|
|
|
+ boolean delete = file.delete();
|
|
|
+ String content = XmlUtil.format(document);
|
|
|
+ // 报文回执信息发送给csp
|
|
|
+ map.put("fileName", fileName);
|
|
|
+ map.put("fileContent", content);
|
|
|
+ receiptRabbitTemplate.convertAndSend(JSONUtil.toJsonStr(map));
|
|
|
+ if (!delete) {
|
|
|
+ log.error("----- 文件: {}, 删除失败! -----", fileName);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (UtilException e) {
|
|
|
+ log.error("----- 解析该文件: {}, 出现错误: {} -----", fileName, e.getMessage());
|
|
|
+ } catch (FileNotFoundException e) {
|
|
|
+ log.error("----- 文件【{}】不存在! -----", pullFilePath);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error(String.format("----- 未知异常: %s -----", e));
|
|
|
+ } finally {
|
|
|
+ if (null != inputStream) {
|
|
|
+ try {
|
|
|
+ inputStream.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ int size = CollectionUtils.isEmpty(pullFilePaths) ? 0 : pullFilePaths.size();
|
|
|
+ log.info("----- 扫描目录完成, 回执文件共 {} 个 -----", size);
|
|
|
+ }
|
|
|
+
|
|
|
+// @Scheduled(fixedRate = 10000)
|
|
|
+ public void sendCEB621 (int size) {
|
|
|
+ String content = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
|
|
|
+ "<configuration>\n" +
|
|
|
+ " <!-- 日志存放路径 -->\n" +
|
|
|
+ "\t<property name=\"log.path\" value=\"D:\\\\reportlogs\" />\n" +
|
|
|
+ " <!-- 日志输出格式 -->\n" +
|
|
|
+ "\t<property name=\"log.pattern\" value=\"%d{HH:mm:ss.SSS} [%thread] %-5level %logger{20} - [%method,%line] - %msg%n\" />\n" +
|
|
|
+ "\n" +
|
|
|
+ "\t<!-- 控制台输出 -->\n" +
|
|
|
+ "\t<appender name=\"console\" class=\"ch.qos.logback.core.ConsoleAppender\">\n" +
|
|
|
+ "\t\t<encoder>\n" +
|
|
|
+ "\t\t\t<pattern>${log.pattern}</pattern>\n" +
|
|
|
+ "\t\t</encoder>\n" +
|
|
|
+ "\t</appender>\n" +
|
|
|
+ "\n" +
|
|
|
+ "\t<!-- 系统日志输出 -->\n" +
|
|
|
+ "\t<appender name=\"file_info\" class=\"ch.qos.logback.core.rolling.RollingFileAppender\">\n" +
|
|
|
+ "\t <file>${log.path}/sys-info.log</file>\n" +
|
|
|
+ " <!-- 循环政策:基于时间创建日志文件 -->\n" +
|
|
|
+ "\t\t<rollingPolicy class=\"ch.qos.logback.core.rolling.TimeBasedRollingPolicy\">\n" +
|
|
|
+ " <!-- 日志文件名格式 -->\n" +
|
|
|
+ "\t\t\t<fileNamePattern>${log.path}/sys-info.%d{yyyy-MM-dd}-%i.log</fileNamePattern>\n" +
|
|
|
+ "\t\t\t<!-- 日志最大的历史 60天 -->\n" +
|
|
|
+ "\t\t\t<maxHistory>60</maxHistory>\n" +
|
|
|
+ " <!-- 分割后单个日志文件大小 -->\n" +
|
|
|
+ " <timeBasedFileNamingAndTriggeringPolicy class=\"ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP\">\n" +
|
|
|
+ " <maxFileSize>10MB</maxFileSize>\n" +
|
|
|
+ " </timeBasedFileNamingAndTriggeringPolicy>\n" +
|
|
|
+ "\t\t</rollingPolicy>\n" +
|
|
|
+ "\t\t<encoder>\n" +
|
|
|
+ "\t\t\t<pattern>${log.pattern}</pattern>\n" +
|
|
|
+ "\t\t</encoder>\n" +
|
|
|
+ "\t\t<filter class=\"ch.qos.logback.classic.filter.LevelFilter\">\n" +
|
|
|
+ " <!-- 过滤的级别 -->\n" +
|
|
|
+ " <level>INFO</level>\n" +
|
|
|
+ " <!-- 匹配时的操作:接收(记录) -->\n" +
|
|
|
+ " <onMatch>ACCEPT</onMatch>\n" +
|
|
|
+ " <!-- 不匹配时的操作:拒绝(不记录) -->\n" +
|
|
|
+ " <onMismatch>DENY</onMismatch>\n" +
|
|
|
+ " </filter>\n" +
|
|
|
+ "\t</appender>\n" +
|
|
|
+ "\n" +
|
|
|
+ "\t<appender name=\"file_error\" class=\"ch.qos.logback.core.rolling.RollingFileAppender\">\n" +
|
|
|
+ "\t <file>${log.path}/sys-error.log</file>\n" +
|
|
|
+ " <!-- 循环政策:基于时间创建日志文件 -->\n" +
|
|
|
+ " <rollingPolicy class=\"ch.qos.logback.core.rolling.TimeBasedRollingPolicy\">\n" +
|
|
|
+ " <!-- 日志文件名格式 -->\n" +
|
|
|
+ " <fileNamePattern>${log.path}/sys-error.%d{yyyy-MM-dd}-%i.log</fileNamePattern>\n" +
|
|
|
+ "\t\t\t<!-- 日志最大的历史 60天 -->\n" +
|
|
|
+ "\t\t\t<maxHistory>60</maxHistory>\n" +
|
|
|
+ " <!-- 分割后单个日志文件大小 -->\n" +
|
|
|
+ " <timeBasedFileNamingAndTriggeringPolicy class=\"ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP\">\n" +
|
|
|
+ " <maxFileSize>10MB</maxFileSize>\n" +
|
|
|
+ " </timeBasedFileNamingAndTriggeringPolicy>\n" +
|
|
|
+ " </rollingPolicy>\n" +
|
|
|
+ " <encoder>\n" +
|
|
|
+ " <pattern>${log.pattern}</pattern>\n" +
|
|
|
+ " </encoder>\n" +
|
|
|
+ " <filter class=\"ch.qos.logback.classic.filter.LevelFilter\">\n" +
|
|
|
+ " <!-- 过滤的级别 -->\n" +
|
|
|
+ " <level>ERROR</level>\n" +
|
|
|
+ "\t\t\t<!-- 匹配时的操作:接收(记录) -->\n" +
|
|
|
+ " <onMatch>ACCEPT</onMatch>\n" +
|
|
|
+ "\t\t\t<!-- 不匹配时的操作:拒绝(不记录) -->\n" +
|
|
|
+ " <onMismatch>DENY</onMismatch>\n" +
|
|
|
+ " </filter>\n" +
|
|
|
+ " </appender>\n" +
|
|
|
+ "\n" +
|
|
|
+ "\t<!-- Spring日志级别控制 -->\n" +
|
|
|
+ "\t<logger name=\"org.springframework\" level=\"warn\" />\n" +
|
|
|
+ "\n" +
|
|
|
+ "\t<root level=\"info\">\n" +
|
|
|
+ "\t\t<appender-ref ref=\"console\" />\n" +
|
|
|
+ "\t</root>\n" +
|
|
|
+ "\n" +
|
|
|
+ "\t<!--系统操作日志-->\n" +
|
|
|
+ " <root level=\"info\">\n" +
|
|
|
+ " <appender-ref ref=\"file_info\" />\n" +
|
|
|
+ " <appender-ref ref=\"file_error\" />\n" +
|
|
|
+ " </root>\n" +
|
|
|
+ "</configuration>\n";
|
|
|
+ content = content.replaceAll("\n", "");
|
|
|
+ content = content.replaceAll("\t", "");
|
|
|
+ content = new String(content.getBytes(), StandardCharsets.UTF_8);
|
|
|
+ for (int i = 0; i < size; i++) {
|
|
|
+// reportRabbitTemplate.convertAndSend(rabbitMQProperties.getK_normal_csp_to_tunnel_report(), content);
|
|
|
+ }
|
|
|
+ log.info("----- 发送完成 -----");
|
|
|
+ }
|
|
|
+
|
|
|
+ @PostConstruct
|
|
|
+ public void init () {
|
|
|
+
|
|
|
+ String pullDir = cPortProperties.getPullDir();
|
|
|
+ String pushDir = cPortProperties.getPushDir();
|
|
|
+
|
|
|
+ File pullDirName = new File(pullDir);
|
|
|
+ File pushDirName = new File(pushDir);
|
|
|
+
|
|
|
+ if (!pullDirName.exists()) {
|
|
|
+ pullDirName.mkdirs();
|
|
|
+ log.info("----- 检测到目录未创建, 创建目录: {} -----", pullDirName);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!pushDirName.exists()) {
|
|
|
+ pushDirName.mkdirs();
|
|
|
+ log.info("----- 检测到目录未创建, 创建目录: {} -----", pushDirName);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+}
|