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?
How to tail a file using a NIO selector, in other words, as lines are added to the file a channel is selected so you can read the lines?
Asked Answered
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).
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.
© 2022 - 2024 — McMap. All rights reserved.