+

Search Tips   |   Advanced Search

Monitoring local files for changes

The Liberty profile has highly dynamic behavior, responding to changes in configuration, applications and other resources. Much of this dynamic behavior is based on monitoring of the local file system for changes. The service that performs this monitoring is available to all Liberty features through the FileMonitor SPI. The file monitor service is provided by the Liberty kernel, so we do not have to specify a feature in server.xml to make it available.

The FileMonitor SPI provides different properties to specify what resources are monitored and with what frequency. We have to implement the FileMonitor interface and register the implementation class into the service registry.

The Java API documentation for each Liberty profile SPI is detailed in the Programming Interfaces (APIs) section of the information center, and is also available as a separate .zip file in one of the javadoc subdirectories of the ${wlp.install.dir}/dev directory.


Example

...
import com.ibm.wsspi.kernel.filemonitor.FileMonitor;
...

public class MyFileMonitor implements FileMonitor {
  ...

  private final BundleContext bundleContex;
    ...

  public MyFileMonitor(BundleContext bundleContext) {
    this.bundleContext = BundleContext;
    ...
  }
 
  public ServiceRegistration<FileMonitor> monitorFiles(Collection<String> paths, long monitorInterval) {
    ...
    final Hashtable<String, Object> fileMonitorProps = new Hashtable<String, Object>();
    fileMonitorProps.put(FileMonitor.MONITOR_FILES, paths);
    fileMonitorProps.put(FileMonitor.MONITOR_INTERVAL, monitorInterval);
    ...
    return bundleContext.registerService(FileMonitor.class, this, fileMonitorProps);
  }
  ...
}


Parent topic: Liberty SPI utilities