java - Using Tailer and WatchService -
i use tailer , watchservice @ same time using code:
agenttailerlistener listener = new agenttailerlistener(); tailer tailer; watchservice watcher = filesystems.getdefault().newwatchservice(); while (true) { watchkey watchkey; watchkey = paths.get("/tmp").register(watcher, entry_create); watchkey = watcher.take(); (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); (new thread(tailer)).start(); } } } watchkey.reset(); transport.close(); }
but problem is: want check 1 file tailer (like stoping thread, can’t stop thread specific file), , when i write in file echo command, not letters wrote appear.
when write same text echo several times in row letters written.
i saw topic, how tail -f latest log file given pattern, don’t know if can use problem (i don’t know difference between tail , tailer).
finally, think works better:
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); list<thread> threadlist = new arraylist<thread>(); while (true) { watchkey = watcher.take(); (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); 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(); }
but creates many threads, think have use fix thread pool.
Comments
Post a Comment