It's not uncommon use case to get a RecordedEvent from a Recording. Before JEP 349: JFR Event Streaming, users had to do:
try (Recording r = new Recording()) {
r.start();
Thread.sleep(10_000);
Path p = Path.of("temp.jfr");
r.dump(p);
List<RecordedEvent> events = RecordingFile.readAllEvents(p);
Files.delete(p);
}
With streaming, the temporary file can be avoided, but you need to use startAsync() and synchronization if you want it to terminate due to some external factor, like a clock, JVM shutdown, or a test that ends executing.
var events = Collections.synchronizedList(new ArrayList<RecordedEvent>());
try (RecordingStream r = new RecordingStream()) {
r.onEvent(events::add);
r.startAsync();
Thread.sleep(10_000);
r.stop();
}
Can we improve this situation, perhaps by adding a method to Recording or RecordingStream/RemoteRecordingStream?
Should the new API method(s) only work on stopped recordings, or should it handle ongoing recordings as well? What about releasing the resources?
try (Recording r = new Recording()) {
r.start();
Thread.sleep(10_000);
Path p = Path.of("temp.jfr");
r.dump(p);
List<RecordedEvent> events = RecordingFile.readAllEvents(p);
Files.delete(p);
}
With streaming, the temporary file can be avoided, but you need to use startAsync() and synchronization if you want it to terminate due to some external factor, like a clock, JVM shutdown, or a test that ends executing.
var events = Collections.synchronizedList(new ArrayList<RecordedEvent>());
try (RecordingStream r = new RecordingStream()) {
r.onEvent(events::add);
r.startAsync();
Thread.sleep(10_000);
r.stop();
}
Can we improve this situation, perhaps by adding a method to Recording or RecordingStream/RemoteRecordingStream?
Should the new API method(s) only work on stopped recordings, or should it handle ongoing recordings as well? What about releasing the resources?