1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
|
AgentTailerListener listener = new AgentTailerListener();
Tailer tailer;
String tailSource;
Thread thread;
WatchService watcher = FileSystems.getDefault().newWatchService();
WatchKey watchKey;
watchKey = Paths.get("/tmp").register(watcher, ENTRY_CREATE);
//création de la liste dans laquelle je vais mettre les thread que je vais activer puis détruire au fur et à mesure du programme
List<Thread> threadList = new ArrayList<Thread>();
while (true) {
watchKey = watcher.take();
for (WatchEvent<?> event : watchKey.pollEvents()) {
if (event.kind() == StandardWatchEventKinds.ENTRY_CREATE) {
tailSource = event.context().toString();
System.out.println(tailSource);
File file = new File("/tmp/" + tailSource);
String end = "log";
if (file.getName().endsWith(end)) {
tailer= TailerFactory.createTailer(file, listener);
//je narrete pas le thread au premier tour car il ny en a pas
if(threadList.isEmpty()){}
else{
Thread.sleep(1000);
threadList.get(0).stop();
threadList.clear();}
System.out.println(threadList.size());
threadList.add(thread = new Thread(tailer));
threadList.get(0).start();
}
}
}
watchKey.reset();
} |
Partager