public class FileWatcher extends Object implements Jooby.Module
Watches for file system changes or event. It uses a watch service to monitor a directory for changes so that it can update its display of the list of files when files are created or deleted.
Watch mydir
and listen for create, modify or delete event on all files under it:
{
use(new FileWatcher()
.register(Paths.get("mydir"), (kind, path) -> {
log.info("found {} on {}", kind, path);
});
);
}
You can specify a FileEventHandler
instance or class while registering a path:
Instance:
{
use(new FileWatcher()
.register(Paths.get("mydir"), (kind, path) -> {
log.info("found {} on {}", kind, path);
});
);
}
Class reference:
{
use(new FileWatcher()
.register(Paths.get("mydir"), MyFileEventHandler.class)
);
}
Worth to mention that MyFileEventHandler
will be provided by Guice.
You can specify a couple of options at registration time:
{
use(new FileWatcher(
.register(Paths.get("mydir"), MyFileEventHandler.class, options -> {
options.kind(StandardWatchEventKinds.ENTRY_MODIFY)
.recursive(false)
.includes("*.java");
});
));
}
1. Here we listen for StandardWatchEventKinds.ENTRY_MODIFY
(we don't care about create or delete).
2. We turn off recursive watching, only direct files are detected.
3. We want .java
files and we ignore any other files.
In addition to do it programmatically, you can do it via configuration properties:
{
use(new FileWatcher()
.register("mydirproperty", MyEventHandler.class)
);
}
The mydirproperty
property must be present in your .conf
file.
But of course, you can entirely register paths from .conf
file:
filewatcher {
register {
path: "mydir"
handler: "org.example.MyFileEventHandler"
kind: "ENTRY_MODIFY"
includes: "*.java"
recursive: false
}
}
Multiple paths are supported using array notation:
filewatcher {
register: [{
path: "mydir1"
handler: "org.example.MyFileEventHandler"
}, {
path: "mydir2"
handler: "org.example.MyFileEventHandler"
}]
}
Now use the module:
{
use(new FileWatcher());
}
The FileWatcher
module read the filewatcher.register
property and setup everything.
Constructor and Description |
---|
FileWatcher() |
Modifier and Type | Method and Description |
---|---|
void |
configure(Env env, com.typesafe.config.Config conf, com.google.inject.Binder binder)
Configure and produces bindings for the underlying application.
|
FileWatcher |
register(Path path, Class<? extends FileEventHandler> handler)
Register the given directory tree and execute the given handler whenever a file/folder has been created, modified or delete.
|
FileWatcher |
register(Path path, Class<? extends FileEventHandler> handler, Consumer<FileEventOptions> configurer)
Register the given directory tree and execute the given handler whenever a file/folder has been created, modified or delete.
|
FileWatcher |
register(Path path, FileEventHandler handler)
Register the given directory tree and execute the given handler whenever a file/folder has been created, modified or delete.
|
FileWatcher |
register(Path path, FileEventHandler handler, Consumer<FileEventOptions> configurer)
Register the given directory tree and execute the given handler whenever a file/folder has been created, modified or delete.
|
FileWatcher |
register(String property, Class<? extends FileEventHandler> handler)
Register the given directory tree and execute the given handler whenever a file/folder has been created, modified or delete.
|
FileWatcher |
register(String property, Class<? extends FileEventHandler> handler, Consumer<FileEventOptions> configurer)
Register the given directory tree and execute the given handler whenever a file/folder has been created, modified or delete.
|
FileWatcher |
register(String property, FileEventHandler handler)
Register the given directory tree and execute the given handler whenever a file/folder has been created, modified or delete.
|
FileWatcher |
register(String property, FileEventHandler handler, Consumer<FileEventOptions> configurer)
Register the given directory tree and execute the given handler whenever a file/folder has been created, modified or delete.
|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
config
public FileWatcher register(Path path, Class<? extends FileEventHandler> handler)
path
- Directory to register.
handler
- Handler to execute.
public FileWatcher register(Path path, Class<? extends FileEventHandler> handler, Consumer<FileEventOptions> configurer)
path
- Directory to register.
handler
- Handler to execute.
configurer
- Configurer callback.
FileEventOptions
public FileWatcher register(Path path, FileEventHandler handler)
path
- Directory to register.
handler
- Handler to execute.
public FileWatcher register(Path path, FileEventHandler handler, Consumer<FileEventOptions> configurer)
path
- Directory to register.
handler
- Handler to execute.
configurer
- Configurer callback.
FileEventOptions
public FileWatcher register(String property, FileEventHandler handler)
property
- Directory to register.
handler
- Handler to execute.
public FileWatcher register(String property, FileEventHandler handler, Consumer<FileEventOptions> configurer)
property
- Directory to register.
handler
- Handler to execute.
configurer
- Configurer callback.
FileEventOptions
public FileWatcher register(String property, Class<? extends FileEventHandler> handler)
property
- Directory to register.
handler
- Handler to execute.
public FileWatcher register(String property, Class<? extends FileEventHandler> handler, Consumer<FileEventOptions> configurer)
property
- Directory to register.
handler
- Handler to execute.
configurer
- Configurer callback.
FileEventOptions
public void configure(Env env, com.typesafe.config.Config conf, com.google.inject.Binder binder) throws Throwable
Jooby.Module
application env
and/or the current application properties available from Config
.
configure
in interface Jooby.Module
env
- The current application's env. Not null.
conf
- The current config object. Not null.
binder
- A guice binder. Not null.
Throwable
- If something goes wrong.
Copyright © 2019. All rights reserved.