优化执行器线程, 使用单线程去读写文件,以防止日志数据混乱
- 使用单线程 - 写入更多文件信息,方便查找问题
This commit is contained in:
parent
c55b7ad024
commit
220f93fae5
@ -1,42 +1,50 @@
|
|||||||
package com.imdroid.sideslope.bd;
|
package com.imdroid.sideslope.bd;
|
||||||
|
|
||||||
import com.imdroid.common.util.ThreadManager;
|
import com.imdroid.common.util.ThreadManager;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
import org.springframework.core.env.Environment;
|
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
import javax.annotation.PostConstruct;
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.nio.file.*;
|
import java.nio.file.*;
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
import java.time.format.DateTimeFormatter;
|
import java.time.format.DateTimeFormatter;
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.concurrent.*;
|
import java.util.concurrent.ExecutorService;
|
||||||
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
@Component
|
@Component
|
||||||
public class WarningLogExecutor {
|
public class WarningLogExecutor {
|
||||||
|
|
||||||
// 警告日志生成
|
|
||||||
@Value("${log.directory}")
|
@Value("${log.directory}")
|
||||||
private String logDirectory;
|
private String logDirectory;
|
||||||
private final ExecutorService executor;
|
private final ExecutorService executor;
|
||||||
|
// 记录已初始化的文件
|
||||||
|
private final ConcurrentHashMap<String, Boolean> initializedFiles = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
public WarningLogExecutor() {
|
public WarningLogExecutor() {
|
||||||
this.executor = ThreadManager.getFixedThreadPool();
|
this.executor = ThreadManager.getSingleThreadPool("warning_log_thread");
|
||||||
}
|
}
|
||||||
|
|
||||||
public void generateWarningLogs(String keyword) {
|
public void generateWarningLogs(String keyword, String warningTypeName) {
|
||||||
try {
|
try {
|
||||||
|
String date = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmm"));
|
||||||
|
String warningFileName = "Warning-" + keyword + "-" + date + ".log";
|
||||||
|
Path warningFile = Paths.get(logDirectory, warningFileName);
|
||||||
|
|
||||||
|
// 如果是新文件,先写入初始化信息
|
||||||
|
if (initializedFiles.putIfAbsent(warningFileName, true) == null) {
|
||||||
|
initializeWarningFile(warningFile, keyword, warningTypeName);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 找到日志文件
|
||||||
List<Path> logFiles = Files.list(Paths.get(logDirectory))
|
List<Path> logFiles = Files.list(Paths.get(logDirectory))
|
||||||
.filter(Files::isRegularFile)
|
.filter(Files::isRegularFile)
|
||||||
.filter(path -> path.getFileName().toString().matches("sideslopertcm(\\.\\d{4}-\\d{2}-\\d{2}\\.\\d+)?\\.log"))
|
.filter(path -> path.getFileName().toString().matches("sideslopertcm(\\.\\d{4}-\\d{2}-\\d{2}\\.\\d+)?\\.log"))
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
|
|
||||||
for (Path logFile : logFiles) {
|
for (Path logFile : logFiles) {
|
||||||
executor.submit(() -> processFile(logFile, keyword));
|
executor.submit(() -> processFile(logFile, keyword, warningTypeName, warningFile));
|
||||||
}
|
}
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
@ -45,17 +53,35 @@ public class WarningLogExecutor {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void processFile(Path logFile, String keyword) {
|
private void initializeWarningFile(Path warningFile, String keyword, String warningTypeName) {
|
||||||
String date = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMdd"));
|
try (BufferedWriter writer = Files.newBufferedWriter(warningFile,
|
||||||
String warningFileName = "Warning-" + keyword + "-" + date + ".log";
|
StandardOpenOption.CREATE, StandardOpenOption.APPEND)) {
|
||||||
Path warningFile = Paths.get(logDirectory, warningFileName);
|
writer.write("=== Warning Log File ===");
|
||||||
|
writer.newLine();
|
||||||
|
writer.write("Created Time: " + LocalDateTime.now());
|
||||||
|
writer.newLine();
|
||||||
|
writer.write("Warning Type Name: " + warningTypeName);
|
||||||
|
writer.newLine();
|
||||||
|
writer.write("Keyword: " + keyword);
|
||||||
|
writer.newLine();
|
||||||
|
writer.write("==============================");
|
||||||
|
writer.newLine();
|
||||||
|
writer.newLine();
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void processFile(Path logFile, String keyword, String warningTypeName, Path warningFile) {
|
||||||
try (BufferedReader reader = Files.newBufferedReader(logFile);
|
try (BufferedReader reader = Files.newBufferedReader(logFile);
|
||||||
BufferedWriter writer = Files.newBufferedWriter(warningFile, StandardOpenOption.CREATE, StandardOpenOption.APPEND)) {
|
BufferedWriter writer = Files.newBufferedWriter(warningFile,
|
||||||
|
StandardOpenOption.CREATE, StandardOpenOption.APPEND)) {
|
||||||
|
|
||||||
|
writer.write("Processing source file: " + logFile.getFileName());
|
||||||
|
writer.newLine();
|
||||||
|
|
||||||
String line;
|
String line;
|
||||||
while ((line = reader.readLine()) != null) {
|
while ((line = reader.readLine()) != null) {
|
||||||
// 行如果包含了特定的关键词,比如站点的编号,则记录这一行
|
|
||||||
if (line.contains(keyword)) {
|
if (line.contains(keyword)) {
|
||||||
writer.write(line);
|
writer.write(line);
|
||||||
writer.newLine();
|
writer.newLine();
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user