Class RemoteRecordingStream
- All Implemented Interfaces:
-
AutoCloseable
,EventStream
public final class RemoteRecordingStream extends Object implements EventStream
EventStream
that can serialize events over the network using an MBeanServerConnection
. The following example shows how to record garbage collection pauses and CPU usage on a remote host and print the events to standard out.
String host = "com.example"; int port = 4711; String url = "service:jmx:rmi:///jndi/rmi://" + host + ":" + port + "/jmxrmi"; JMXServiceURL u = new JMXServiceURL(url); JMXConnector c = JMXConnectorFactory.connect(u); MBeanServerConnection conn = c.getMBeanServerConnection(); try (var rs = new RemoteRecordingStream(conn)) { rs.enable("jdk.GCPhasePause").withoutThreshold(); rs.enable("jdk.CPULoad").withPeriod(Duration.ofSeconds(1)); rs.onEvent("jdk.CPULoad", System.out::println); rs.onEvent("jdk.GCPhasePause", System.out::println); rs.start(); }
- Since:
- 16
Constructor Summary
Constructor | Description |
---|---|
RemoteRecordingStream |
Creates an event stream that operates against a MBeanServerConnection that has a registered FlightRecorderMXBean . |
RemoteRecordingStream |
Creates an event stream that operates against a MBeanServerConnection that has a registered FlightRecorderMXBean . |
Method Summary
Modifier and Type | Method | Description |
---|---|---|
void |
awaitTermination() |
Blocks until all actions are completed, or the stream is closed, or the current thread is interrupted, whichever happens first. |
void |
awaitTermination |
Blocks until all actions are completed, or the stream is closed, or the timeout occurs, or the current thread is interrupted, whichever happens first. |
void |
close() |
Releases all resources associated with this stream. |
EventSettings |
disable |
Disables event with the specified name. |
void |
dump |
Writes recording data to a file. |
EventSettings |
enable |
Enables the event with the specified name. |
void |
onClose |
Registers an action to perform when the stream is closed. |
void |
onError |
Registers an action to perform if an exception occurs. |
void |
onEvent |
Registers an action to perform on all events matching a name. |
void |
onEvent |
Registers an action to perform on all events in the stream. |
void |
onFlush |
Registers an action to perform after the stream has been flushed. |
boolean |
remove |
Unregisters an action. |
void |
setEndTime |
Specifies the end time of the stream. |
void |
setMaxAge |
Determines how far back data is kept for the stream. |
void |
setMaxSize |
Determines how much data is kept for the stream. |
void |
setOrdered |
Specifies that events arrives in chronological order, sorted by the time they were committed to the stream. |
void |
setReuse |
Specifies that the event object in an EventStream.onEvent(Consumer) action can be reused. |
void |
setSettings |
Replaces all settings for this recording stream. |
void |
setStartTime |
Specifies the start time of the stream. |
void |
start() |
Starts processing of actions. |
void |
startAsync() |
Starts asynchronous processing of actions. |
Methods declared in class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods declared in interface jdk.jfr.consumer.EventStream
onMetadata
Constructor Details
RemoteRecordingStream
public RemoteRecordingStream(MBeanServerConnection connection) throws IOException
MBeanServerConnection
that has a registered FlightRecorderMXBean
. To configure event settings, use setSettings(Map)
.
- Parameters:
-
connection
- theMBeanServerConnection
where theFlightRecorderMXBean
is registered, notnull
- Throws:
-
IOException
- if a stream can't be opened, an I/O error occurs when trying to access the repository or theFlightRecorderMXBean
-
SecurityException
- if a security manager exists and itscheckRead
method denies read access to the directory, or files in the directory.
RemoteRecordingStream
public RemoteRecordingStream(MBeanServerConnection connection, Path directory) throws IOException
MBeanServerConnection
that has a registered FlightRecorderMXBean
. To configure event settings, use setSettings(Map)
.
- Parameters:
-
connection
- theMBeanServerConnection
where theFlightRecorderMXBean
is registered, notnull
-
directory
- the directory to store event data that is downloaded, notnull
- Throws:
-
IOException
- if a stream can't be opened, an I/O error occurs when trying to access the repository or theFlightRecorderMXBean
-
SecurityException
- if a security manager exists and itscheckRead
method denies read access to the directory, or files in the directory.
Method Details
setSettings
public void setSettings(Map<String,String> settings)
The following example connects to a remote host and stream events using settings from the "default" configuration.
{ String host = "com.example"; int port = 4711; String url = "service:jmx:rmi:///jndi/rmi://" + host + ":" + port + "/jmxrmi"; JMXServiceURL u = new JMXServiceURL(url); JMXConnector c = JMXConnectorFactory.connect(u); MBeanServerConnection conn = c.getMBeanServerConnection(); try (final var rs = new RemoteRecordingStream(conn)) { rs.onMetadata(e -> { for (Configuration c : e.getConfigurations()) { if (c.getName().equals("default")) { rs.setSettings(c.getSettings()); } } }); rs.onEvent(System.out::println); rs.start(); }
- Parameters:
-
settings
- the settings to set, notnull
- See Also:
disable
public EventSettings disable(String 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.
- Parameters:
-
name
- the settings for the event, notnull
- Returns:
- an event setting for further configuration, not
null
enable
public EventSettings enable(String 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.
- Parameters:
-
name
- the settings for the event, notnull
- Returns:
- an event setting for further configuration, not
null
- See Also:
setMaxAge
public void setMaxAge(Duration maxAge)
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 not consumed.
- Parameters:
-
maxAge
- the length of time that data is kept, ornull
if infinite - Throws:
-
IllegalArgumentException
- ifmaxAge
is negative -
IllegalStateException
- if the recording is in theCLOSED
state
setMaxSize
public void setMaxSize(long maxSize)
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 if events are not consumed.
The size is measured in bytes.
- Parameters:
-
maxSize
- the amount of data to retain,0
if infinite - Throws:
-
IllegalArgumentException
- ifmaxSize
is negative -
IllegalStateException
- if the recording is inCLOSED
state
onEvent
public void onEvent(Consumer<RecordedEvent> action)
EventStream
- Specified by:
-
onEvent
in interfaceEventStream
- Parameters:
-
action
- an action to perform on eachRecordedEvent
, notnull
onEvent
public void onEvent(String eventName, Consumer<RecordedEvent> action)
EventStream
- Specified by:
-
onEvent
in interfaceEventStream
- Parameters:
-
eventName
- the name of the event, notnull
-
action
- an action to perform on eachRecordedEvent
matching the event name, notnull
onFlush
public void onFlush(Runnable action)
EventStream
- Specified by:
-
onFlush
in interfaceEventStream
- Parameters:
-
action
- an action to perform after the stream has been flushed, notnull
onError
public void onError(Consumer<Throwable> action)
EventStream
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:
-
onError
in interfaceEventStream
- Parameters:
-
action
- an action to perform if an exception occurs, notnull
onClose
public void onClose(Runnable action)
EventStream
If the stream is already closed, the action will be performed immediately in the current thread.
- Specified by:
-
onClose
in interfaceEventStream
- Parameters:
-
action
- an action to perform after the stream is closed, notnull
- See Also:
close
public void close()
EventStream
If a stream is started, asynchronously or synchronously, it is stopped immediately or after the next flush. This method does NOT guarantee that all registered actions are completed before return.
Closing a previously closed stream has no effect.
- Specified by:
-
close
in interfaceAutoCloseable
- Specified by:
-
close
in interfaceEventStream
remove
public boolean remove(Object action)
EventStream
If the action has been registered multiple times, all instances are unregistered.
- Specified by:
-
remove
in interfaceEventStream
- Parameters:
-
action
- the action to unregister, notnull
- Returns:
-
true
if the action was unregistered,false
otherwise - See Also:
setReuse
public void setReuse(boolean reuse)
EventStream
EventStream.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:
-
setReuse
in interfaceEventStream
- Parameters:
-
reuse
-true
if an event object can be reused,false
otherwise
setOrdered
public void setOrdered(boolean ordered)
EventStream
- Specified by:
-
setOrdered
in interfaceEventStream
- Parameters:
-
ordered
- if event objects arrive in chronological order toEventStream.onEvent(Consumer)
setStartTime
public void setStartTime(Instant startTime)
EventStream
The start time must be set before starting the stream
- Specified by:
-
setStartTime
in interfaceEventStream
- Parameters:
-
startTime
- the start time, notnull
- See Also:
setEndTime
public void setEndTime(Instant endTime)
EventStream
The end time must be set before starting the stream.
At end time, the stream is closed.
- Specified by:
-
setEndTime
in interfaceEventStream
- Parameters:
-
endTime
- the end time, notnull
- See Also:
start
public void start()
EventStream
Actions are performed in the current thread.
To stop the stream, use the EventStream.close()
method.
- Specified by:
-
start
in interfaceEventStream
startAsync
public void startAsync()
EventStream
Actions are performed in a single separate thread.
To stop the stream, use the EventStream.close()
method.
- Specified by:
-
startAsync
in interfaceEventStream
dump
public void dump(Path destination) throws IOException
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, notnull
- Throws:
-
IOException
- if the recording data can't be copied to the specified location, or if the stream is closed, or not started. -
SecurityException
- if a security manager exists and the caller doesn't haveFilePermission
to write to the destination path - Since:
- 17
- See Also:
awaitTermination
public void awaitTermination(Duration timeout) throws InterruptedException
EventStream
- Specified by:
-
awaitTermination
in interfaceEventStream
- Parameters:
-
timeout
- the maximum time to wait, notnull
- Throws:
-
InterruptedException
- if interrupted while waiting - See Also:
awaitTermination
public void awaitTermination() throws InterruptedException
EventStream
- Specified by:
-
awaitTermination
in interfaceEventStream
- Throws:
-
InterruptedException
- if interrupted while waiting - See Also:
© 1993, 2021, Oracle and/or its affiliates. All rights reserved.
Documentation extracted from Debian's OpenJDK Development Kit package.
Licensed under the GNU General Public License, version 2, with the Classpath Exception.
Various third party code in OpenJDK is licensed under different licenses (see Debian package).
Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
https://docs.oracle.com/en/java/javase/17/docs/api/jdk.management.jfr/jdk/management/jfr/RemoteRecordingStream.html