下面列出了org.apache.commons.io.input.TailerListener#org.apache.commons.io.input.TailerListenerAdapter 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void readAndTail(File logsFile, int fromNumberOfLines) throws IOException {
new Tailer(logsFile, StandardCharsets.UTF_8, new TailerListenerAdapter() {
@SneakyThrows
@Override
public void init(Tailer tailer) {
super.init(tailer);
if(fromNumberOfLines != -1) {
out.println(LauncherUtils.readLastLines(logsFile, fromNumberOfLines));
}
}
@Override
public void handle(String line) {
out.println(line);
}
@Override
public void handle(Exception ex) {
ex.printStackTrace();
System.exit(1);
}
}, 100, fromNumberOfLines != -1, false, 4096).run();
}
private void startTailingLog() {
TailerListener listener =
new TailerListenerAdapter() {
@Override
public void handle(String line) {
System.out.println(testName + ": " + line);
}
};
// Tail the log
File file = new File(getBasedir() + File.separator + getLogFileName());
try {
if (file.exists()) {
file.delete();
}
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
Tailer tailer = new Tailer(file, listener, TAIL_DELAY_MILLIS);
Thread thread = new Thread(tailer);
thread.setDaemon(true);
thread.start();
}
private TailerListenerAdapter createListener(final ObservableEmitter<String> emitter) {
return new TailerListenerAdapter() {
@Override
public void fileRotated() {
// ignore, just keep tailing
}
@Override
public void handle(String line) {
emitter.onNext(line);
}
@Override
public void fileNotFound() {
emitter.onError(new FileNotFoundException(file.toString()));
}
@Override
public void handle(Exception ex) {
emitter.onError(ex);
}
};
}
/**
* Create a named pipe subscriber
* @param handler TailerListenerAdaptor. The parent, the thing that consumes the message.
* @param fileName String. The name of the pipe.
* @throws Exception if the file does not exist or what is named is not a file.
*/
public PipeTailer(TailerListenerAdapter handler, String fileName) throws Exception {
this.handler = handler;
this.fileName = fileName;
File f_pipe = new File(fileName);
if (!f_pipe.exists())
throw new Exception("Named pipe: " + fileName + " does not exist");
if (!f_pipe.isFile()==false)
throw new Exception(fileName + " is not a pipee");
me = new Thread(this);
me.start();
}