public class Tailer extends Object implements Runnable, AutoCloseable
To build an instance, use Tailer.Builder.
First you need to create a TailerListener implementation; (TailerListenerAdapter is provided for
convenience so that you don't have to implement every method).
For example:
public class MyTailerListener extends TailerListenerAdapter {
public void handle(String line) {
System.out.println(line);
}
}
You can create and use a Tailer in one of three ways:
Tailer.BuilderExecutorThreadAn example of each is shown below.
TailerListener listener = new MyTailerListener(); Tailer tailer = Tailer.builder() .setFile(file) .setTailerListener(listener) .setDelayDuration(delay) .get();
TailerListener listener = new MyTailerListener();
Tailer tailer = new Tailer(file, listener, delay);
// stupid executor impl. for demo purposes
Executor executor = new Executor() {
public void execute(Runnable command) {
command.run();
}
};
executor.execute(tailer);
TailerListener listener = new MyTailerListener(); Tailer tailer = new Tailer(file, listener, delay); Thread thread = new Thread(tailer); thread.setDaemon(true); // optional thread.start();
Remember to stop the tailer when you have done with it:
tailer.stop();
You can interrupt the thread a tailer is running on by calling Thread.interrupt().
thread.interrupt();
If you interrupt a tailer, the tailer listener is called with the InterruptedException.
The file is read using the default Charset; this can be overridden if necessary.
Tailer.Builder,
TailerListener,
TailerListenerAdapter| Modifier and Type | Class and Description |
|---|---|
static class |
Tailer.Builder
Builds a new
Tailer. |
static interface |
Tailer.RandomAccessResourceBridge
Bridges access to a resource for random access, normally a file.
|
static interface |
Tailer.Tailable
A tailable resource like a file.
|
| Modifier and Type | Method and Description |
|---|---|
static Tailer.Builder |
builder()
Constructs a new
Tailer.Builder. |
void |
close()
Requests the tailer to complete its current loop and return.
|
java.time.Duration |
getDelayDuration()
Gets the delay Duration.
|
File |
getFile()
Gets the file.
|
protected boolean |
getRun()
Gets whether to keep on running.
|
Tailer.Tailable |
getTailable()
Gets the Tailable.
|
void |
run()
Follows changes in the file, calling
TailerListener.handle(String) with each new line. |
public static Tailer.Builder builder()
Tailer.Builder.Tailer.Builder.public void close()
close in interface AutoCloseablepublic java.time.Duration getDelayDuration()
public File getFile()
IllegalStateException - if constructed using a user provided Tailer.Tailable implementationprotected boolean getRun()
public Tailer.Tailable getTailable()
public void run()
TailerListener.handle(String) with each new line.