Archived
0

fix log reader, if log file not close

This commit is contained in:
iMoHax
2016-06-01 13:38:26 +03:00
parent ae783f4e5b
commit a8d5deb5a9
3 changed files with 34 additions and 9 deletions

View File

@@ -6,5 +6,6 @@ public interface LogHandler {
void createFile(Path file);
void updateFile(Path file);
void notChanges();
void close();
}

View File

@@ -10,7 +10,7 @@ public class LogReader implements LogHandler {
private final static Logger LOG = LoggerFactory.getLogger(LogReader.class);
private final String pattern;
private BufferedReader reader;
private RandomAccessFile reader;
private File file;
public LogReader(String pattern) {
@@ -20,13 +20,11 @@ public class LogReader implements LogHandler {
private void changeFile(File file){
if (this.file != null && this.file.equals(file)) return;
LOG.trace("Watch new file {}", file);
FileReader fileReader;
try {
fileReader = new FileReader(file);
if (reader != null){
closeReader();
}
reader = new BufferedReader(fileReader);
reader = new RandomAccessFile(file, "r");
this.file = file;
} catch (FileNotFoundException e) {
LOG.error("Not found log file", e);
@@ -82,6 +80,13 @@ public class LogReader implements LogHandler {
}
@Override
public void notChanges() {
if (file != null){
readFile();
}
}
private void closeReader(){
if (reader == null) return;
try {

View File

@@ -3,6 +3,7 @@ package ru.trader.edlog;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.IOException;
import java.nio.file.*;
import java.util.concurrent.TimeUnit;
@@ -13,7 +14,7 @@ public class LogWatcher {
private final Path dir;
private final LogHandler handler;
private WatchService watcher;
private boolean notCancel;
private boolean run;
private Thread thread;
public LogWatcher(String dir, LogHandler handler) {
@@ -29,6 +30,7 @@ public class LogWatcher {
watch();
}
};
thread.setDaemon(true);
}
public void start() throws IOException {
@@ -38,15 +40,32 @@ public class LogWatcher {
}
watcher = FileSystems.getDefault().newWatchService();
dir.register(watcher, StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.ENTRY_MODIFY);
notCancel = true;
Path last = getLastModify();
if (last != null) handler.createFile(last);
run = true;
thread.start();
}
private Path getLastModify(){
File last = null;
File[] files = dir.toFile().listFiles();
if (files == null) return null;
for (File file : files) {
if (last == null || last.lastModified() < file.lastModified()){
last = file;
}
}
return last != null ? last.toPath() : null;
}
private void watch(){
try {
while (notCancel) {
while (run) {
WatchKey key = watcher.poll(5, TimeUnit.SECONDS);
if (key == null) continue;
if (key == null){
handler.notChanges();
continue;
}
for (WatchEvent<?> event: key.pollEvents()) {
WatchEvent.Kind<?> kind = event.kind();
if (kind == StandardWatchEventKinds.OVERFLOW) {
@@ -93,6 +112,6 @@ public class LogWatcher {
public void stop(){
LOG.debug("Stop log watch service");
notCancel = false;
run = false;
}
}