|
@@ -8,6 +8,7 @@ import cn.hutool.json.JSONUtil;
|
|
import com.emato.file.tunnel.config.properties.CPortProperties;
|
|
import com.emato.file.tunnel.config.properties.CPortProperties;
|
|
import com.emato.file.tunnel.config.properties.RabbitMQProperties;
|
|
import com.emato.file.tunnel.config.properties.RabbitMQProperties;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
+import org.springframework.amqp.rabbit.connection.CorrelationData;
|
|
import org.springframework.amqp.rabbit.core.RabbitTemplate;
|
|
import org.springframework.amqp.rabbit.core.RabbitTemplate;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.scheduling.annotation.Scheduled;
|
|
import org.springframework.scheduling.annotation.Scheduled;
|
|
@@ -43,57 +44,66 @@ public class ScanPullDirectoryTask {
|
|
|
|
|
|
@Scheduled(fixedRate = 3000)
|
|
@Scheduled(fixedRate = 3000)
|
|
public void scanDirectory() {
|
|
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);
|
|
|
|
|
|
+ try {
|
|
|
|
+ String pullDir = cPortProperties.getPullDir();
|
|
|
|
+ List<String> fileNames = FileUtil.listFileNames(pullDir);
|
|
|
|
+ // 根据设置的值处理多少个文件
|
|
|
|
+ if (CollectionUtil.isEmpty(fileNames)) {
|
|
|
|
+ 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 && fileName.contains("CEB622Message")).collect(Collectors.toList());
|
|
|
|
+ if (CollectionUtils.isEmpty(pullFilePaths)) {
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+ log.info("----- 扫描报文目录, 开始扫描目录: {} -----", pullDir);
|
|
|
|
+ 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);
|
|
|
|
+ Object message = JSONUtil.toJsonStr(map);
|
|
|
|
+ CorrelationData correlationData = new CorrelationData();
|
|
|
|
+ correlationData.setId(fileName);
|
|
|
|
+ receiptRabbitTemplate.convertAndSend(rabbitMQProperties.getK_normal_tunnel_to_csp_receipt(), message, correlationData);
|
|
|
|
+ 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();
|
|
|
|
|
|
+ } 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);
|
|
|
|
|
|
+ int size = CollectionUtils.isEmpty(pullFilePaths) ? 0 : pullFilePaths.size();
|
|
|
|
+ log.info("----- 扫描报文目录, 扫描目录完成, 回执文件共 {} 个 -----", size);
|
|
|
|
+ } catch (Throwable e) {
|
|
|
|
+ log.error("------ 扫描报文目录, 扫描目录出现未知错误! -----", e);
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
// @Scheduled(fixedRate = 10000)
|
|
// @Scheduled(fixedRate = 10000)
|