From a5f944c2bc0107e5df936937f1c33e5e03eb8fed Mon Sep 17 00:00:00 2001
From: yangys <y_ys79@sina.com>
Date: 星期六, 06 九月 2025 19:15:48 +0800
Subject: [PATCH] 解锁增加解锁原因和展示
---
blade-service/blade-mdm/src/main/java/org/springblade/mdm/machinefile/filewatch/FileWatcherService.java | 29 ++++++++++++++++++++++++++++-
1 files changed, 28 insertions(+), 1 deletions(-)
diff --git a/blade-service/blade-mdm/src/main/java/org/springblade/mdm/machinefile/filewatch/FileWatcherService.java b/blade-service/blade-mdm/src/main/java/org/springblade/mdm/machinefile/filewatch/FileWatcherService.java
index ebd3d24..bc65651 100644
--- a/blade-service/blade-mdm/src/main/java/org/springblade/mdm/machinefile/filewatch/FileWatcherService.java
+++ b/blade-service/blade-mdm/src/main/java/org/springblade/mdm/machinefile/filewatch/FileWatcherService.java
@@ -1,5 +1,6 @@
package org.springblade.mdm.machinefile.filewatch;
+import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import java.io.IOException;
@@ -10,12 +11,22 @@
import java.util.concurrent.Executors;
import static java.nio.file.StandardWatchEventKinds.*;
+import com.google.common.cache.Cache;
+import com.google.common.cache.CacheBuilder;
+@Slf4j
@Service
public class FileWatcherService {
private final ExecutorService executor = Executors.newCachedThreadPool();
private final Map<Path, WatchService> watchServices = new HashMap<>();
private final Map<Path, FileChangeListener> listeners = new HashMap<>();
+ private final Map<Path, FileState> fileStatesOld = new HashMap<>();
+ Cache<Path, FileState> fileStates = CacheBuilder.newBuilder()
+ .maximumSize(10) // 鏈�澶у閲�3
+ .build();
+ enum FileState {
+ CREATED, MODIFIED, STABLE
+ }
public interface FileChangeListener {
void onFileCreated(Path filePath);
@@ -42,6 +53,7 @@
try {
while (true) {
WatchKey key = watchService.take();
+
for (WatchEvent<?> event : key.pollEvents()) {
WatchEvent.Kind<?> kind = event.kind();
@@ -50,13 +62,28 @@
Path fileName = ev.context();
Path fullPath = directory.resolve(fileName);
+ FileState currentState = fileStates.getIfPresent(fullPath);
+ currentState = (currentState == null) ? FileState.STABLE : currentState;
+
+ //FileState currentState = fileStates.getOrDefault(fullPath, FileState.STABLE);
+
FileChangeListener currentListener = listeners.get(directory);
if (currentListener == null) break;
if (kind == ENTRY_CREATE) {
+ if (currentState != FileState.CREATED) {
+ log.info("鏂版枃浠跺垱寤�: {}", fullPath);
+ fileStates.put(fullPath, FileState.CREATED);
+ }
currentListener.onFileCreated(fullPath);
} else if (kind == ENTRY_MODIFY) {
- currentListener.onFileModified(fullPath);
+ if (currentState == FileState.CREATED) {
+ // 蹇界暐鍒涘缓鍚庣殑绗竴娆′慨鏀�
+ log.info("鏂囦欢鍒涘缓鏃剁殑淇敼浜嬩欢锛屽拷鐣�: {}", fullPath);
+ fileStates.put(fullPath, FileState.STABLE);
+ } else {
+ currentListener.onFileModified(fullPath);
+ }
} else if (kind == ENTRY_DELETE) {
currentListener.onFileDeleted(fullPath);
}
--
Gitblit v1.9.3