Class WatchServer

java.lang.Object
java.lang.Thread
org.aoju.bus.core.io.watcher.WatchServer
All Implemented Interfaces:
Closeable, Serializable, AutoCloseable, Runnable
Direct Known Subclasses:
WatchMonitor

public class WatchServer extends Thread implements Closeable, Serializable
文件监听服务,此服务可以同时监听多个路径
Since:
Java 17+
Author:
Kimi Liu
See Also:
  • Field Details

    • events

      protected WatchEvent.Kind<?>[] events
      监听事件列表
    • isClosed

      protected boolean isClosed
      监听是否已经关闭
  • Constructor Details

    • WatchServer

      public WatchServer()
  • Method Details

    • init

      public void init() throws InstrumentException
      初始化 初始化包括:
       1、解析传入的路径,判断其为目录还是文件
       2、创建WatchService 对象
       
      Throws:
      InstrumentException - 监听异常,IO异常时抛出此异常
    • setModifiers

      public void setModifiers(WatchEvent.Modifier[] modifiers)
      设置监听选项,例如监听频率等,可设置项包括:
       1、com.sun.nio.file.StandardWatchEventKinds
       2、com.sun.nio.file.SensitivityWatchEventModifier
       
      Parameters:
      modifiers - 监听选项,例如监听频率等
    • registerPath

      public void registerPath(Path path, int maxDepth)
      将指定路径加入到监听中
      Parameters:
      path - 路径
      maxDepth - 递归下层目录的最大深度
    • watch

      public void watch(Watcher watcher, Filter<WatchEvent<?>> watchFilter)
      执行事件获取并处理
      Parameters:
      watcher - Watcher
      watchFilter - 监听过滤接口,通过实现此接口过滤掉不需要监听的情况,null表示不过滤
    • close

      public void close()
      关闭监听
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable