|
@@ -1,7 +1,6 @@
|
|
|
package com.wj.service.impl;
|
|
package com.wj.service.impl;
|
|
|
|
|
|
|
|
import com.wj.DTO.RecordingInfo;
|
|
import com.wj.DTO.RecordingInfo;
|
|
|
-import com.wj.config.PropertiesConfig;
|
|
|
|
|
import com.wj.exception.CustomXmlParseException;
|
|
import com.wj.exception.CustomXmlParseException;
|
|
|
import lombok.Getter;
|
|
import lombok.Getter;
|
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
@@ -11,43 +10,34 @@ import org.w3c.dom.NodeList;
|
|
|
|
|
|
|
|
import javax.xml.parsers.DocumentBuilder;
|
|
import javax.xml.parsers.DocumentBuilder;
|
|
|
import javax.xml.parsers.DocumentBuilderFactory;
|
|
import javax.xml.parsers.DocumentBuilderFactory;
|
|
|
-import java.io.BufferedReader;
|
|
|
|
|
-import java.io.BufferedWriter;
|
|
|
|
|
import java.io.File;
|
|
import java.io.File;
|
|
|
-import java.io.IOException;
|
|
|
|
|
-import java.nio.file.Files;
|
|
|
|
|
-import java.nio.file.Path;
|
|
|
|
|
-import java.nio.file.Paths;
|
|
|
|
|
import java.time.OffsetDateTime;
|
|
import java.time.OffsetDateTime;
|
|
|
import java.time.format.DateTimeFormatter;
|
|
import java.time.format.DateTimeFormatter;
|
|
|
-import java.util.Iterator;
|
|
|
|
|
-import java.util.LinkedHashMap;
|
|
|
|
|
-import java.util.Map;
|
|
|
|
|
|
|
|
|
|
@Getter
|
|
@Getter
|
|
|
@Service
|
|
@Service
|
|
|
public class XmlProcessor {
|
|
public class XmlProcessor {
|
|
|
- //private static final Map<Path, RecordingInfo> xmlCache = new ConcurrentHashMap<>();
|
|
|
|
|
- private static final Path cacheFilePath = Paths.get("xml_cache.txt");
|
|
|
|
|
- // 定义缓存大小上限,例如1000条记录
|
|
|
|
|
- private static final int MAX_CACHE_SIZE = PropertiesConfig.getXmlCache();
|
|
|
|
|
- // 使用LinkedHashMap来维护一个有序的缓存,按照访问顺序排序
|
|
|
|
|
- private static final Map<Path, RecordingInfo> xmlCache = new LinkedHashMap<Path, RecordingInfo>() {
|
|
|
|
|
- @Override
|
|
|
|
|
- protected boolean removeEldestEntry(Map.Entry<Path, RecordingInfo> eldest) {
|
|
|
|
|
- return size() > MAX_CACHE_SIZE; // 当缓存大小超过上限时,自动移除最旧的条目
|
|
|
|
|
- }
|
|
|
|
|
- };
|
|
|
|
|
- public XmlProcessor() throws CustomXmlParseException {
|
|
|
|
|
- loadCache();
|
|
|
|
|
- }
|
|
|
|
|
|
|
+// //private static final Map<Path, RecordingInfo> xmlCache = new ConcurrentHashMap<>();
|
|
|
|
|
+// private static final Path cacheFilePath = Paths.get("xml_cache.txt");
|
|
|
|
|
+// // 定义缓存大小上限,例如1000条记录
|
|
|
|
|
+// private static final int MAX_CACHE_SIZE = PropertiesConfig.getXmlCache();
|
|
|
|
|
+// // 使用LinkedHashMap来维护一个有序的缓存,按照访问顺序排序
|
|
|
|
|
+// private static final Map<Path, RecordingInfo> xmlCache = new LinkedHashMap<Path, RecordingInfo>() {
|
|
|
|
|
+// @Override
|
|
|
|
|
+// protected boolean removeEldestEntry(Map.Entry<Path, RecordingInfo> eldest) {
|
|
|
|
|
+// return size() > MAX_CACHE_SIZE; // 当缓存大小超过上限时,自动移除最旧的条目
|
|
|
|
|
+// }
|
|
|
|
|
+// };
|
|
|
|
|
+// public XmlProcessor() throws CustomXmlParseException {
|
|
|
|
|
+// loadCache();
|
|
|
|
|
+// }
|
|
|
|
|
|
|
|
public static RecordingInfo parseXmlFile(File xmlFile) throws CustomXmlParseException {
|
|
public static RecordingInfo parseXmlFile(File xmlFile) throws CustomXmlParseException {
|
|
|
try {
|
|
try {
|
|
|
- Path xmlPath = xmlFile.toPath();
|
|
|
|
|
- if (xmlCache.containsKey(xmlPath)) {
|
|
|
|
|
- return xmlCache.get(xmlPath);
|
|
|
|
|
- }
|
|
|
|
|
|
|
+// Path xmlPath = xmlFile.toPath();
|
|
|
|
|
+// if (xmlCache.containsKey(xmlPath)) {
|
|
|
|
|
+// return xmlCache.get(xmlPath);
|
|
|
|
|
+// }
|
|
|
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
|
|
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
|
|
|
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
|
|
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
|
|
|
Document doc = dBuilder.parse(xmlFile);
|
|
Document doc = dBuilder.parse(xmlFile);
|
|
@@ -59,14 +49,21 @@ public class XmlProcessor {
|
|
|
String fileName = getElementValue(doc, "inum");
|
|
String fileName = getElementValue(doc, "inum");
|
|
|
|
|
|
|
|
OffsetDateTime parsedEndTime = null;
|
|
OffsetDateTime parsedEndTime = null;
|
|
|
|
|
+
|
|
|
|
|
+// if (endTime != null) {
|
|
|
|
|
+// parsedEndTime = OffsetDateTime.parse(endTime, DateTimeFormatter.ISO_OFFSET_DATE_TIME);
|
|
|
|
|
+// }
|
|
|
|
|
+// if (parsedEndTime != null) {
|
|
|
|
|
+// RecordingInfo info = new RecordingInfo(switchCallId, parsedEndTime.toString(), fileName);
|
|
|
|
|
+// xmlCache.put(xmlFile.toPath(), info); // 缓存解析结果
|
|
|
|
|
+// saveCache();
|
|
|
|
|
+// return info;
|
|
|
|
|
+// }
|
|
|
if (endTime != null) {
|
|
if (endTime != null) {
|
|
|
parsedEndTime = OffsetDateTime.parse(endTime, DateTimeFormatter.ISO_OFFSET_DATE_TIME);
|
|
parsedEndTime = OffsetDateTime.parse(endTime, DateTimeFormatter.ISO_OFFSET_DATE_TIME);
|
|
|
}
|
|
}
|
|
|
if (parsedEndTime != null) {
|
|
if (parsedEndTime != null) {
|
|
|
- RecordingInfo info = new RecordingInfo(switchCallId, parsedEndTime.toString(), fileName);
|
|
|
|
|
- xmlCache.put(xmlFile.toPath(), info); // 缓存解析结果
|
|
|
|
|
- saveCache();
|
|
|
|
|
- return info;
|
|
|
|
|
|
|
+ return new RecordingInfo(switchCallId, parsedEndTime.toString(), fileName);
|
|
|
}
|
|
}
|
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
|
throw new CustomXmlParseException("解析XML文件出错: " + xmlFile.getName(), e);
|
|
throw new CustomXmlParseException("解析XML文件出错: " + xmlFile.getName(), e);
|
|
@@ -101,41 +98,41 @@ public class XmlProcessor {
|
|
|
return null;
|
|
return null;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- private void loadCache() throws CustomXmlParseException {
|
|
|
|
|
- if (Files.exists(cacheFilePath)) {
|
|
|
|
|
- try (BufferedReader reader = Files.newBufferedReader(cacheFilePath)) {
|
|
|
|
|
- String line;
|
|
|
|
|
- while ((line = reader.readLine()) != null) {
|
|
|
|
|
- String[] parts = line.split(",");
|
|
|
|
|
- Path xmlPath = Paths.get(parts[0]);
|
|
|
|
|
- RecordingInfo info = new RecordingInfo(parts[1], parts[2], parts[3]);
|
|
|
|
|
- xmlCache.put(xmlPath, info);
|
|
|
|
|
- }
|
|
|
|
|
- } catch (IOException e) {
|
|
|
|
|
- throw new CustomXmlParseException(new RuntimeException(e));
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- private static void saveCache() throws CustomXmlParseException {
|
|
|
|
|
- // 在保存缓存之前,确保缓存不超过上限
|
|
|
|
|
- if (xmlCache.size() > MAX_CACHE_SIZE) {
|
|
|
|
|
- Iterator<Map.Entry<Path, RecordingInfo>> iterator = xmlCache.entrySet().iterator();
|
|
|
|
|
- while (xmlCache.size() > MAX_CACHE_SIZE && iterator.hasNext()) {
|
|
|
|
|
- iterator.next();
|
|
|
|
|
- iterator.remove(); // 移除最旧的条目
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- try (BufferedWriter writer = Files.newBufferedWriter(cacheFilePath)) {
|
|
|
|
|
- for (Map.Entry<Path, RecordingInfo> entry : xmlCache.entrySet()) {
|
|
|
|
|
- writer.write(entry.getKey().toString() + "," +
|
|
|
|
|
- entry.getValue().getSwitchCallId() + "," +
|
|
|
|
|
- entry.getValue().getEndTime() + "," +
|
|
|
|
|
- entry.getValue().getFileName());
|
|
|
|
|
- writer.newLine();
|
|
|
|
|
- }
|
|
|
|
|
- } catch (IOException e) {
|
|
|
|
|
- throw new CustomXmlParseException(e);
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
|
|
+// private void loadCache() throws CustomXmlParseException {
|
|
|
|
|
+// if (Files.exists(cacheFilePath)) {
|
|
|
|
|
+// try (BufferedReader reader = Files.newBufferedReader(cacheFilePath)) {
|
|
|
|
|
+// String line;
|
|
|
|
|
+// while ((line = reader.readLine()) != null) {
|
|
|
|
|
+// String[] parts = line.split(",");
|
|
|
|
|
+// Path xmlPath = Paths.get(parts[0]);
|
|
|
|
|
+// RecordingInfo info = new RecordingInfo(parts[1], parts[2], parts[3]);
|
|
|
|
|
+// xmlCache.put(xmlPath, info);
|
|
|
|
|
+// }
|
|
|
|
|
+// } catch (IOException e) {
|
|
|
|
|
+// throw new CustomXmlParseException(new RuntimeException(e));
|
|
|
|
|
+// }
|
|
|
|
|
+// }
|
|
|
|
|
+// }
|
|
|
|
|
+//
|
|
|
|
|
+// private static void saveCache() throws CustomXmlParseException {
|
|
|
|
|
+// // 在保存缓存之前,确保缓存不超过上限
|
|
|
|
|
+// if (xmlCache.size() > MAX_CACHE_SIZE) {
|
|
|
|
|
+// Iterator<Map.Entry<Path, RecordingInfo>> iterator = xmlCache.entrySet().iterator();
|
|
|
|
|
+// while (xmlCache.size() > MAX_CACHE_SIZE && iterator.hasNext()) {
|
|
|
|
|
+// iterator.next();
|
|
|
|
|
+// iterator.remove(); // 移除最旧的条目
|
|
|
|
|
+// }
|
|
|
|
|
+// }
|
|
|
|
|
+// try (BufferedWriter writer = Files.newBufferedWriter(cacheFilePath)) {
|
|
|
|
|
+// for (Map.Entry<Path, RecordingInfo> entry : xmlCache.entrySet()) {
|
|
|
|
|
+// writer.write(entry.getKey().toString() + "," +
|
|
|
|
|
+// entry.getValue().getSwitchCallId() + "," +
|
|
|
|
|
+// entry.getValue().getEndTime() + "," +
|
|
|
|
|
+// entry.getValue().getFileName());
|
|
|
|
|
+// writer.newLine();
|
|
|
|
|
+// }
|
|
|
|
|
+// } catch (IOException e) {
|
|
|
|
|
+// throw new CustomXmlParseException(e);
|
|
|
|
|
+// }
|
|
|
|
|
+// }
|
|
|
}
|
|
}
|