Class StandardFileWatchService

  • All Implemented Interfaces:
    FileWatchService, java.lang.AutoCloseable

    public class StandardFileWatchService
    extends java.lang.Object
    implements FileWatchService, java.lang.AutoCloseable
    /** Standard implementation of the FileWatchService interface using JDK 7's WatchService API. This class monitors files or directories for creation, modification, and deletion events.

    Key Features

    • Supports watching individual files or entire directories.
    • Allows filtering based on event types: CREATED, MODIFIED, DELETED.
    • Uses a thread-safe design to handle concurrent listeners and events.
    • Provides auto-closeable behavior via the close() method.

    Example Usage

    
     // Example 1: Watching a single file for modification events
     FileWatchService watchService = new StandardFileWatchService();
     File fileToWatch = new File("/path/to/file.txt");
     FileChangedListener listener = event -> System.out.println("File changed: " + event.getFile());
    
     watchService.watch(fileToWatch, listener, FileChangedEvent.Kind.MODIFIED);
    
     // Example 2: Watching a directory for all types of events with multiple listeners
     File dirToWatch = new File("/path/to/directory");
     List<FileChangedListener> listeners = Arrays.asList(
         event -> System.out.println("Listener 1 triggered: " + event),
         event -> System.out.println("Listener 2 triggered: " + event)
     );
    
     watchService.watch(dirToWatch, listeners); // All kinds by default
     

    Make sure to call close() when you're done using the service to release resources.

    Since:
    1.0.0
    Author:
    Mercy
    See Also:
    WatchService
    • Field Detail

      • DEFAULT_THREAD_NAME_PREFIX

        public static final java.lang.String DEFAULT_THREAD_NAME_PREFIX
        The default thread name prefix : "microsphere-file-watch-service"
        See Also:
        Constant Field Values
      • THREAD_NAME_PREFIX_PROPERTY_NAME

        public static final java.lang.String THREAD_NAME_PREFIX_PROPERTY_NAME
        The thread name prefix property name : "microsphere.file-watch-service.thread-name-prefix
        See Also:
        Constant Field Values
      • THREAD_NAME_PREFIX

        @ConfigurationProperty(name="microsphere.file-watch-service.thread-name-prefix",
                               defaultValue="microsphere-file-watch-service",
                               description="The thread name prefix for FileWatchService",
                               source="system-properties")
        public static final java.lang.String THREAD_NAME_PREFIX
        The thread name prefix , default value : "microsphere-file-watch-service-"
    • Constructor Detail

      • StandardFileWatchService

        public StandardFileWatchService()
      • StandardFileWatchService

        public StandardFileWatchService​(java.util.concurrent.Executor eventHandlerExecutor)
      • StandardFileWatchService

        public StandardFileWatchService​(java.util.concurrent.Executor eventHandlerExecutor,
                                        java.util.concurrent.ExecutorService eventLoopExecutor)
    • Method Detail

      • start

        public void start()
                   throws java.lang.Exception
        Throws:
        java.lang.Exception
      • stop

        public void stop()
                  throws java.lang.Exception
        Throws:
        java.lang.Exception
      • close

        public void close()
                   throws java.lang.Exception
        Specified by:
        close in interface java.lang.AutoCloseable
        Throws:
        java.lang.Exception