1.优化地区csv加载机制 2.修复数据库监控无法访问问题 3.增加了登录状态保存 4.针对session存储机制做适配 5.文件扫描机制优化,并行扫描速度更快
This commit is contained in:
+26
-3
@@ -22,6 +22,8 @@ import java.nio.channels.FileChannel;
|
||||
import java.nio.file.*;
|
||||
import java.nio.file.attribute.BasicFileAttributes;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
import static java.nio.file.StandardWatchEventKinds.*;
|
||||
|
||||
@@ -50,6 +52,9 @@ public class FileWatchServiceManager {
|
||||
private String rootReceiveDir;
|
||||
private volatile boolean running = false;
|
||||
|
||||
/** 文件处理线程池,用于并发 MD5 计算 + 入库 */
|
||||
private final ExecutorService fileProcessExecutor = Executors.newFixedThreadPool(4);
|
||||
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
try {
|
||||
@@ -72,6 +77,7 @@ public class FileWatchServiceManager {
|
||||
log.warn("WatchService 关闭异常", e);
|
||||
}
|
||||
}
|
||||
fileProcessExecutor.shutdown();
|
||||
}
|
||||
|
||||
/** 确保文件扫描目录结构存在(receive/display/archive 及各部门子目录) */
|
||||
@@ -188,7 +194,8 @@ public class FileWatchServiceManager {
|
||||
/** 扫描部门目录下的所有文件 */
|
||||
private void scanDirectory(Path deptDir, Long deptId) {
|
||||
try (var fileStream = Files.list(deptDir)) {
|
||||
fileStream.filter(Files::isRegularFile).forEach(file -> processFile(file, deptId));
|
||||
fileStream.filter(Files::isRegularFile).forEach(file ->
|
||||
fileProcessExecutor.submit(() -> processFile(file, deptId)));
|
||||
} catch (IOException e) {
|
||||
log.error("扫描目录失败: {}", deptDir, e);
|
||||
}
|
||||
@@ -212,7 +219,7 @@ public class FileWatchServiceManager {
|
||||
|
||||
Long deptId = deptDirMap.get(watchDir.toString());
|
||||
// deptDirMap 中没有的是根目录,deptId 为 null
|
||||
processFile(fullPath, deptId);
|
||||
fileProcessExecutor.submit(() -> processFile(fullPath, deptId));
|
||||
}
|
||||
|
||||
if (!key.reset()) {
|
||||
@@ -227,8 +234,24 @@ public class FileWatchServiceManager {
|
||||
}
|
||||
}
|
||||
|
||||
/** 等待文件写入完成并可读 */
|
||||
/**
|
||||
* 等待文件写入完成并可读。
|
||||
* 先快速路径尝试获取共享锁(已有文件通常立即可用),
|
||||
* 若被占用再进入大小稳定 + 等待循环。
|
||||
*/
|
||||
private boolean waitForFileReady(Path filePath) {
|
||||
if (!Files.exists(filePath)) return false;
|
||||
|
||||
// 快速路径:已写完的文件可立即获得共享锁,直接返回
|
||||
try (RandomAccessFile raf = new RandomAccessFile(filePath.toFile(), "rw");
|
||||
FileChannel ch = raf.getChannel()) {
|
||||
java.nio.channels.FileLock lock = ch.lock(0, Long.MAX_VALUE, true);
|
||||
lock.release();
|
||||
return true;
|
||||
} catch (Exception ignored) {
|
||||
// 文件正在被写入,进入等待逻辑
|
||||
}
|
||||
|
||||
long lastSize = -1;
|
||||
int stableCount = 0;
|
||||
int maxRetries = 20;
|
||||
|
||||
+1
-1
@@ -39,7 +39,7 @@ import java.util.Map;
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("station/weatherstation")
|
||||
@Tag(name = "站点")
|
||||
@Tag(name = "气象站点")
|
||||
public class WeatherStationController {
|
||||
|
||||
@Resource
|
||||
|
||||
Reference in New Issue
Block a user