问题
Because you cannot redirect GC logs I am left with the option to redirect it to a file with -Xloggc and then get the contents of this file inside my selector through a file channel of some kind. Basically as lines are being added to my file, the selector is being triggered to read them. That way I can get the GC logs programmatically. Is it possible to do that using NIO?
回答1:
Given GC logs are buffered, I wouldn't worry about a bit of latency. You can just poll the file length periodically and read the data added each time. You can do this in IO, NIO or NIO2.
回答2:
No. FileChannel does not extend SelectableChannel so you can't select with it, and even in C where you can, select() doesn't deliver readable events when the file is extended (it delivers them every time you select, as the file is always readable).
来源:https://stackoverflow.com/questions/9856128/how-to-tail-a-file-using-a-nio-selector-in-other-words-as-lines-are-added-to-t