Skip to content

Commit

Permalink
8345339: JFR: Missing javadoc for RecordingStream::onMetadata
Browse files Browse the repository at this point in the history
Reviewed-by: nbenalla, liach
  • Loading branch information
egahlin committed Dec 5, 2024
1 parent 456c71d commit 97b8a09
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
21 changes: 21 additions & 0 deletions src/jdk.jfr/share/classes/jdk/jfr/consumer/RecordingStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,27 @@ public void awaitTermination() throws InterruptedException {
directoryStream.awaitTermination();
}

/**
* Registers 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.
* <p>
* 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 {@link #onEvent(Consumer)} method,
* is that a stream implementation can avoid reading events that are of no
* interest.
*
* {@snippet class = "Snippets" region = "RecordingStreamMetadata"}
*
* @param action to perform, not {@code null}
*
* @throws IllegalStateException if an action is added after the stream has
* started
* @since 16
*/
@Override
public void onMetadata(Consumer<MetadataEvent> action) {
directoryStream.onMetadata(action);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2022, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -109,6 +109,27 @@ public static void main(String... args) throws IOException {
// @end
}

class RecordingStreamMetadata {
// @start region="RecordingStreamMetadata"
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);
}
}
// @end
}

void RecordingFileOverview() throws Exception {
// @start region="RecordingFileOverview"
try (RecordingFile recordingFile = new RecordingFile(Paths.get("recording.jfr"))) {
Expand Down

0 comments on commit 97b8a09

Please sign in to comment.