SPUERSAIYAN opened a new pull request, #5229:
URL: https://github.com/apache/eventmesh/pull/5229

   [Enhancement] ConfigMonitorService uses JDK WatchService to monitor config 
changes
   Fixes #3331
   ## Motivation
   原有的 `ConfigMonitorService` 采用**定时轮询**机制(每 30 秒)检测配置文件变化,这种方式存在以下问题:
   1. **延迟**:配置变更后最多需要等待 30 秒才能被检测到
   2. **资源浪费**:即使没有配置变更,定时器仍会持续执行
   
   ## Modifications
   
   ### 核心改动
   将配置监控机制从 **轮询模式** 改为 **事件驱动模式**:
   | 对比项 | 原始实现 | 修改后 |
   |--------|----------|--------|
   | 监控方式 | `ScheduledExecutorService` 定时轮询 (30s) | JDK `WatchService` 事件驱动 |
   | 数据结构 | `ArrayList` | `ConcurrentHashMap` + `CopyOnWriteArrayList` |
   | 响应延迟 | 最多 30 秒 | 实时 |
   | 资源消耗 | 持续轮询 | 仅在变更时触发 |
   ### 新增文件
   - **`WatchFileManager.java`**:文件监控管理器,使用 JDK `WatchService` 实现目录级监控
   - **`FileChangeListener.java`**:文件变更监听器接口
   - **`FileChangeContext.java`**:文件变更上下文
   
   ### 代码变更
   **`ConfigMonitorService.java`**:
   - 移除 `ScheduledExecutorService` 定时轮询
   - 移除 `TIME_INTERVAL` 常量
   - 新增 `ConcurrentHashMap` 存储文件路径与监听器的映射
   - 通过 `WatchFileManager.registerFileChangeListener()` 注册文件变更监听
   - 实现 `FileChangeListener` 接口处理变更回调
   
   ### 解决方案
   1. 事件驱动替代轮询
   java
   运行
   // 使用 JDK WatchService 监听文件系统事件
   WatchFileManager.registerFileChangeListener(directoryPath, 
CONFIG_FILE_CHANGE_LISTENER);
   原理:利用操作系统底层的文件监听机制
   
   2. 匹配文件路径
   实现 FileChangeListener,只处理目标文件
           // 只处理被监控的文件
           List<ConfigInfo> configInfoList = 
CONFIG_INFO_MAP.get(changedFilePath);
           if (configInfoList != null) {
               for (ConfigInfo configInfo : configInfoList) {
                   load(configInfo);
               }
           }
   
   3. 资源管理优化
   进程退出时自动释放所有监控资源
   // JVM 关闭时自动清理
   static {
       Runtime.getRuntime().addShutdownHook(new Thread(() -> {
           log.info("[ConfigMonitorService] shutdown, clearing {} entries", 
CONFIG_INFO_MAP.size());
           CONFIG_INFO_MAP.clear();
       }));
   }
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to