Class RecordingStream
- All Implemented Interfaces:
- AutoCloseable, EventStream
The following example shows how to record events using the default configuration and print the Garbage Collection, CPU Load and JVM Information event to standard out.
Configuration c = Configuration.getConfiguration("default");
try (var rs = new RecordingStream(c)) {
    rs.onEvent("jdk.GarbageCollection", System.out::println);
    rs.onEvent("jdk.CPULoad", System.out::println);
    rs.onEvent("jdk.JVMInformation", System.out::println);
    rs.start();
}
- Since:
- 14
- 
Constructor SummaryConstructorsConstructorDescriptionCreates an event stream for the current JVM (Java Virtual Machine).RecordingStream(Configuration configuration) Creates a recording stream using settings from a configuration.
- 
Method SummaryModifier and TypeMethodDescriptionvoidBlocks until all actions are completed, or the stream is closed, or the current thread is interrupted, whichever happens first.voidawaitTermination(Duration timeout) Blocks until all actions are completed, or the stream is closed, or the timeout occurs, or the current thread is interrupted, whichever happens first.voidclose()Closes this resource, relinquishing any underlying resources.Disables event.Disables event with the specified name.voidWrites recording data to a file.Enables event.Enables the event with the specified name.voidRegisters an action to perform when the stream is closed.voidRegisters an action to perform if an exception occurs.voidonEvent(String eventName, Consumer<RecordedEvent> action) Registers an action to perform on all events matching a name.voidonEvent(Consumer<RecordedEvent> action) Registers an action to perform on all events in the stream.voidRegisters an action to perform after the stream has been flushed.voidonMetadata(Consumer<MetadataEvent> action) Registers an action to perform when new metadata arrives in the stream.booleanUnregisters an action.voidsetEndTime(Instant endTime) Specifies the end time of the stream.voidDetermines how far back data is kept for the stream.voidsetMaxSize(long maxSize) Determines how much data is kept for the stream.voidsetOrdered(boolean ordered) Specifies that events arrives in chronological order, sorted by the time they were committed to the stream.voidsetReuse(boolean reuse) Specifies that the event object in anEventStream.onEvent(Consumer)action can be reused.voidsetSettings(Map<String, String> settings) Replaces all settings for this recording stream.voidsetStartTime(Instant startTime) Specifies the start time of the stream.voidstart()Starts processing of actions.voidStarts asynchronous processing of actions.booleanstop()Stops the recording stream.
- 
Constructor Details- 
RecordingStreampublic RecordingStream()Creates an event stream for the current JVM (Java Virtual Machine).- Throws:
- IllegalStateException- if Flight Recorder can't be created (for example, if the Java Virtual Machine (JVM) lacks Flight Recorder support, or if the file repository can't be created or accessed)
 
- 
RecordingStreamCreates a recording stream using settings from a configuration.The following example shows how to create a recording stream that uses a predefined configuration. var c = Configuration.getConfiguration("default"); try (var rs = new RecordingStream(c)) { rs.onEvent(System.out::println); rs.start(); }- Parameters:
- configuration- configuration that contains the settings to use, not- null
- Throws:
- IllegalStateException- if Flight Recorder can't be created (for example, if the Java Virtual Machine (JVM) lacks Flight Recorder support, or if the file repository can't be created or accessed)
- See Also:
 
 
- 
- 
Method Details- 
enableEnables the event with the specified name.If multiple events have the same name (for example, the same class is loaded in different class loaders), then all events that match the name are enabled. To enable a specific class, use the enable(Class)method or aStringrepresentation of the event type ID.- Parameters:
- name- the settings for the event, not- null
- Returns:
- an event setting for further configuration, not null
- See Also:
 
- 
setSettingsReplaces all settings for this recording stream.The following example records 20 seconds using the "default" configuration and then changes settings to the "profile" configuration. Configuration defaultConfiguration = Configuration.getConfiguration("default"); Configuration profileConfiguration = Configuration.getConfiguration("profile"); try (var rs = new RecordingStream(defaultConfiguration)) { rs.onEvent(System.out::println); rs.startAsync(); Thread.sleep(20_000); rs.setSettings(profileConfiguration.getSettings()); Thread.sleep(20_000); }- Parameters:
- settings- the settings to set, not- null
- See Also:
 
- 
enableEnables event.- Parameters:
- eventClass- the event to enable, not- null
- Returns:
- an event setting for further configuration, not null
- Throws:
- IllegalArgumentException- if- eventClassis an abstract class or not a subclass of- Event
 
- 
disableDisables event with the specified name.If multiple events with same name (for example, the same class is loaded in different class loaders), then all events that match the name are disabled. To disable a specific class, use the disable(Class)method or aStringrepresentation of the event type ID.- Parameters:
- name- the settings for the event, not- null
- Returns:
- an event setting for further configuration, not null
 
- 
disableDisables event.- Parameters:
- eventClass- the event to enable, not- null
- Returns:
- an event setting for further configuration, not null
- Throws:
- IllegalArgumentException- if- eventClassis an abstract class or not a subclass of- Event
 
- 
setMaxAgeDetermines how far back data is kept for the stream.To control the amount of recording data stored on disk, the maximum length of time to retain the data can be specified. Data stored on disk that is older than the specified length of time is removed by the Java Virtual Machine (JVM). If neither maximum limit or the maximum age is set, the size of the recording may grow indefinitely if events are on - Parameters:
- maxAge- the length of time that data is kept, or- nullif infinite
- Throws:
- IllegalArgumentException- if- maxAgeis negative
- IllegalStateException- if the recording is in the- CLOSEDstate
 
- 
setMaxSizepublic void setMaxSize(long maxSize) Determines how much data is kept for the stream.To control the amount of recording data that is stored on disk, the maximum amount of data to retain can be specified. When the maximum limit is exceeded, the Java Virtual Machine (JVM) removes the oldest chunk to make room for a more recent chunk. If neither maximum limit or the maximum age is set, the size of the recording may grow indefinitely. The size is measured in bytes. - Parameters:
- maxSize- the amount of data to retain,- 0if infinite
- Throws:
- IllegalArgumentException- if- maxSizeis negative
- IllegalStateException- if the recording is in- CLOSEDstate
 
- 
setReusepublic void setReuse(boolean reuse) Description copied from interface:EventStreamSpecifies that the event object in anEventStream.onEvent(Consumer)action can be reused.If reuse is set to true, an action should not keep a reference to the event object after the action has completed.- Specified by:
- setReusein interface- EventStream
- Parameters:
- reuse-- trueif an event object can be reused,- falseotherwise
 
- 
setOrderedpublic void setOrdered(boolean ordered) Description copied from interface:EventStreamSpecifies that events arrives in chronological order, sorted by the time they were committed to the stream.- Specified by:
- setOrderedin interface- EventStream
- Parameters:
- ordered- if event objects arrive in chronological order to- EventStream.onEvent(Consumer)
 
- 
setStartTimeDescription copied from interface:EventStreamSpecifies the start time of the stream.The start time must be set before starting the stream - Specified by:
- setStartTimein interface- EventStream
- Parameters:
- startTime- the start time, not- null
- See Also:
 
- 
setEndTimeDescription copied from interface:EventStreamSpecifies the end time of the stream.The end time must be set before starting the stream. At end time, the stream is closed. - Specified by:
- setEndTimein interface- EventStream
- Parameters:
- endTime- the end time, not- null
- See Also:
 
- 
onEventDescription copied from interface:EventStreamRegisters an action to perform on all events matching a name.- Specified by:
- onEventin interface- EventStream
- Parameters:
- eventName- the name of the event, not- null
- action- an action to perform on each- RecordedEventmatching the event name, not- null
 
- 
onEventDescription copied from interface:EventStreamRegisters an action to perform on all events in the stream.To perform an action on a subset of event types, consider using EventStream.onEvent(String, Consumer)andEventStream.onMetadata(Consumer)as it is likely more performant than any selection or filtering mechanism implemented in a generic action.- Specified by:
- onEventin interface- EventStream
- Parameters:
- action- an action to perform on each- RecordedEvent, not- null
- See Also:
 
- 
onFlushDescription copied from interface:EventStreamRegisters an action to perform after the stream has been flushed.- Specified by:
- onFlushin interface- EventStream
- Parameters:
- action- an action to perform after the stream has been flushed, not- null
 
- 
onCloseDescription copied from interface:EventStreamRegisters an action to perform when the stream is closed.If the stream is already closed, the action will be performed immediately in the current thread. - Specified by:
- onClosein interface- EventStream
- Parameters:
- action- an action to perform after the stream is closed, not- null
- See Also:
 
- 
onErrorDescription copied from interface:EventStreamRegisters an action to perform if an exception occurs.If an action is not registered, an exception stack trace is printed to standard error. Registering an action overrides the default behavior. If multiple actions have been registered, they are performed in the order of registration. If this method itself throws an exception, resulting behavior is undefined. - Specified by:
- onErrorin interface- EventStream
- Parameters:
- action- an action to perform if an exception occurs, not- null
 
- 
closepublic void close()Description copied from interface:AutoCloseableCloses this resource, relinquishing any underlying resources. This method is invoked automatically on objects managed by thetry-with-resources statement.- Specified by:
- closein interface- AutoCloseable
- Specified by:
- closein interface- EventStream
 
- 
removeDescription copied from interface:EventStreamUnregisters an action.If the action has been registered multiple times, all instances are unregistered. - Specified by:
- removein interface- EventStream
- Parameters:
- action- the action to unregister, not- null
- Returns:
- trueif the action was unregistered,- falseotherwise
- See Also:
 
- 
startpublic void start()Description copied from interface:EventStreamStarts processing of actions.Actions are performed in the current thread. To stop the stream, use the EventStream.close()method.- Specified by:
- startin interface- EventStream
 
- 
startAsyncpublic void startAsync()Starts asynchronous processing of actions.Actions are performed in a single separate thread. To stop the stream, use the close()method.The following example prints the CPU usage for ten seconds. When the current thread leaves the try-with-resources block the stream is stopped/closed. try (var stream = new RecordingStream()) { stream.enable("jdk.CPULoad").withPeriod(Duration.ofSeconds(1)); stream.onEvent("jdk.CPULoad", event -> { System.out.println(event); }); stream.startAsync(); Thread.sleep(10_000); }- Specified by:
- startAsyncin interface- EventStream
- Throws:
- IllegalStateException- if the stream is already started or closed
 
- 
stoppublic boolean stop()Stops the recording stream.Stops a started stream and waits until all events in the recording have been consumed. Invoking this method in an action, for example in the onEvent(Consumer)method, could block the stream indefinitely. To stop the stream abruptly, use theclose()method.The following code snippet illustrates how this method can be used in conjunction with the startAsync()method to monitor what happens during a test method:AtomicBoolean socketUse = new AtomicBoolean(); try (var r = new RecordingStream()) { r.setMaxSize(Long.MAX_VALUE); r.enable("jdk.SocketWrite").withoutThreshold(); r.enable("jdk.SocketRead").withoutThreshold(); r.onEvent(event -> socketUse.set(true)); r.startAsync(); testFoo(); r.stop(); if (socketUse.get()) { r.dump(Path.of("socket-events.jfr")); throw new AssertionError("testFoo() should not use network"); } }- Returns:
- trueif recording is stopped,- falseotherwise
- Throws:
- IllegalStateException- if the recording is not started or is already stopped
- Since:
- 20
 
- 
dumpWrites recording data to a file.The recording stream must be started, but not closed. It's highly recommended that a max age or max size is set before starting the stream. Otherwise, the dump may not contain any events. - Parameters:
- destination- the location where recording data is written, not- null
- Throws:
- IOException- if the recording data can't be copied to the specified location, or if the stream is closed, or not started.
- Since:
- 17
- See Also:
 
- 
awaitTerminationDescription copied from interface:EventStreamBlocks until all actions are completed, or the stream is closed, or the timeout occurs, or the current thread is interrupted, whichever happens first.- Specified by:
- awaitTerminationin interface- EventStream
- Parameters:
- timeout- the maximum time to wait, not- null
- Throws:
- InterruptedException- if interrupted while waiting
- See Also:
 
- 
awaitTerminationDescription copied from interface:EventStreamBlocks until all actions are completed, or the stream is closed, or the current thread is interrupted, whichever happens first.- Specified by:
- awaitTerminationin interface- EventStream
- Throws:
- InterruptedException- if interrupted while waiting
- See Also:
 
- 
onMetadataRegisters an action to perform when new metadata arrives in the stream. The event type of an event always arrives sometime before the actual event. The action must be registered before the stream is started.The following example shows how to listen to new event types, register an action if the event type name matches a regular expression and increase a counter if a matching event is found. A benefit of using an action per event type, instead of the generic onEvent(Consumer)method, is that a stream implementation can avoid reading events that are of no interest.static long count = 0; public static void main(String... args) throws Exception { String regExp = args[0]; var pr = Pattern.compile(regExp).asMatchPredicate(); Configuration c = Configuration.getConfiguration("default"); try (var s = new RecordingStream(c)) { s.setOrdered(false); s.onMetadata(metadata -> metadata.getAddedEventTypes() .stream().map(EventType::getName).filter(pr) .forEach(eventName -> s.onEvent(eventName, event -> count++))); s.startAsync(); System.out.println("Running recording for 5 s. Please wait."); s.awaitTermination(Duration.ofSeconds(5)); System.out.println(count + " events matches " + regExp); } }- Specified by:
- onMetadatain interface- EventStream
- Parameters:
- action- to perform, not- null
- Throws:
- IllegalStateException- if an action is added after the stream has started
- Since:
- 16
 
 
-