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 createFile(Path file);
void updateFile(Path file); void updateFile(Path file);
void notChanges();
void close(); void close();
} }

View File

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

View File

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