When a recording is parsed, known types are materialized in a Java representation, such as j.j.c.RecordedThread. For some reason this doesn't work, leading to the object defaulting to j.j.c.RecordedObject, which then can't be cast, resulting in j.l.ClassCastException.
java.lang.ClassCastException: class jdk.jfr.consumer.RecordedObject cannot be cast to class jdk.jfr.consumer.RecordedThread (jdk.jfr.consumer.RecordedObject and jdk.jfr.consumer.RecordedThread are in module jdk.jfr of loader 'bootstrap')
at jdk.jfr/jdk.jfr.consumer.RecordedEvent.getThread(RecordedEvent.java:70)
at JFRStreamingIssue$JFRStreamConsumer.onEvent(JFRStreamingIssue.java:64)
at JFRStreamingIssue$JFRStreamConsumer.access$0(JFRStreamingIssue.java:61)
at jdk.jfr/jdk.jfr.internal.consumer.Dispatcher$EventDispatcher.offer(Dispatcher.java:52)
at jdk.jfr/jdk.jfr.internal.consumer.Dispatcher.dispatch(Dispatcher.java:165)
at jdk.jfr/jdk.jfr.internal.consumer.EventDirectoryStream.processOrdered(EventDirectoryStream.java:211)
at jdk.jfr/jdk.jfr.internal.consumer.EventDirectoryStream.processRecursionSafe(EventDirectoryStream.java:139)
at jdk.jfr/jdk.jfr.internal.consumer.EventDirectoryStream.process(EventDirectoryStream.java:97)
at jdk.jfr/jdk.jfr.internal.consumer.AbstractEventStream.execute(AbstractEventStream.java:243)
at jdk.jfr/jdk.jfr.internal.consumer.AbstractEventStream$1.run(AbstractEventStream.java:265)
at jdk.jfr/jdk.jfr.internal.consumer.AbstractEventStream$1.run(AbstractEventStream.java:262)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
at jdk.jfr/jdk.jfr.internal.consumer.AbstractEventStream.run(AbstractEventStream.java:262)
at jdk.jfr/jdk.jfr.internal.consumer.AbstractEventStream.start(AbstractEventStream.java:222)
at jdk.jfr/jdk.jfr.consumer.RecordingStream.start(RecordingStream.java:329)
at JFRStreamingIssue.subscribeJFR(JFRStreamingIssue.java:46)
at java.base/java.lang.Thread.run(Thread.java:832)
Reproducer:
import jdk.jfr.consumer.RecordedEvent;
import jdk.jfr.consumer.RecordingStream;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class JFRStreamingIssue {
public static void main(String[] args) {
Thread jfrStreamThread = new Thread(JFRStreamingIssue::subscribeJFR);
jfrStreamThread.start();
ExecutorService executor = new ThreadPoolExecutor(0, Integer.MAX_VALUE,
5L, TimeUnit.SECONDS,
new SynchronousQueue<Runnable>());
for (int i = 0; i < 10000; i++) {
executor.submit(() -> {
try {
Thread.sleep(100);
} catch (Exception ex) {
}
});
}
System.out.println("done");
}
private static void subscribeJFR() {
System.out.println("subscribeJFR");
try {
try (Writer writer = new BufferedWriter(new FileWriter("events.txt"))) {
JFRStreamConsumer consumer = new JFRStreamConsumer(writer);
try (RecordingStream st = new RecordingStream()) {
st.enable("jdk.ThreadStart");
st.enable("jdk.ThreadEnd");
st.enable("jdk.ThreadSleep");
st.enable("jdk.ThreadPark");
st.onEvent("jdk.ThreadStart", consumer::onEvent);
st.onEvent("jdk.ThreadEnd", consumer::onEvent);
st.onEvent("jdk.ThreadSleep", consumer::onEvent);
st.onEvent("jdk.ThreadPark", consumer::onEvent);
st.start();
}
}
} catch (IOException ex) {
}
}
private static class JFRStreamConsumer {
private final Writer writer;
public JFRStreamConsumer(Writer writer) {
this.writer = writer;
}
private void onEvent(RecordedEvent event) {
String serializedEvent = null;
try {
serializedEvent = String.valueOf(event.getThread());
} catch (Exception ex) {
ex.printStackTrace();
return;
}
try {
writer.write(serializedEvent);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
java.lang.ClassCastException: class jdk.jfr.consumer.RecordedObject cannot be cast to class jdk.jfr.consumer.RecordedThread (jdk.jfr.consumer.RecordedObject and jdk.jfr.consumer.RecordedThread are in module jdk.jfr of loader 'bootstrap')
at jdk.jfr/jdk.jfr.consumer.RecordedEvent.getThread(RecordedEvent.java:70)
at JFRStreamingIssue$JFRStreamConsumer.onEvent(JFRStreamingIssue.java:64)
at JFRStreamingIssue$JFRStreamConsumer.access$0(JFRStreamingIssue.java:61)
at jdk.jfr/jdk.jfr.internal.consumer.Dispatcher$EventDispatcher.offer(Dispatcher.java:52)
at jdk.jfr/jdk.jfr.internal.consumer.Dispatcher.dispatch(Dispatcher.java:165)
at jdk.jfr/jdk.jfr.internal.consumer.EventDirectoryStream.processOrdered(EventDirectoryStream.java:211)
at jdk.jfr/jdk.jfr.internal.consumer.EventDirectoryStream.processRecursionSafe(EventDirectoryStream.java:139)
at jdk.jfr/jdk.jfr.internal.consumer.EventDirectoryStream.process(EventDirectoryStream.java:97)
at jdk.jfr/jdk.jfr.internal.consumer.AbstractEventStream.execute(AbstractEventStream.java:243)
at jdk.jfr/jdk.jfr.internal.consumer.AbstractEventStream$1.run(AbstractEventStream.java:265)
at jdk.jfr/jdk.jfr.internal.consumer.AbstractEventStream$1.run(AbstractEventStream.java:262)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
at jdk.jfr/jdk.jfr.internal.consumer.AbstractEventStream.run(AbstractEventStream.java:262)
at jdk.jfr/jdk.jfr.internal.consumer.AbstractEventStream.start(AbstractEventStream.java:222)
at jdk.jfr/jdk.jfr.consumer.RecordingStream.start(RecordingStream.java:329)
at JFRStreamingIssue.subscribeJFR(JFRStreamingIssue.java:46)
at java.base/java.lang.Thread.run(Thread.java:832)
Reproducer:
import jdk.jfr.consumer.RecordedEvent;
import jdk.jfr.consumer.RecordingStream;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class JFRStreamingIssue {
public static void main(String[] args) {
Thread jfrStreamThread = new Thread(JFRStreamingIssue::subscribeJFR);
jfrStreamThread.start();
ExecutorService executor = new ThreadPoolExecutor(0, Integer.MAX_VALUE,
5L, TimeUnit.SECONDS,
new SynchronousQueue<Runnable>());
for (int i = 0; i < 10000; i++) {
executor.submit(() -> {
try {
Thread.sleep(100);
} catch (Exception ex) {
}
});
}
System.out.println("done");
}
private static void subscribeJFR() {
System.out.println("subscribeJFR");
try {
try (Writer writer = new BufferedWriter(new FileWriter("events.txt"))) {
JFRStreamConsumer consumer = new JFRStreamConsumer(writer);
try (RecordingStream st = new RecordingStream()) {
st.enable("jdk.ThreadStart");
st.enable("jdk.ThreadEnd");
st.enable("jdk.ThreadSleep");
st.enable("jdk.ThreadPark");
st.onEvent("jdk.ThreadStart", consumer::onEvent);
st.onEvent("jdk.ThreadEnd", consumer::onEvent);
st.onEvent("jdk.ThreadSleep", consumer::onEvent);
st.onEvent("jdk.ThreadPark", consumer::onEvent);
st.start();
}
}
} catch (IOException ex) {
}
}
private static class JFRStreamConsumer {
private final Writer writer;
public JFRStreamConsumer(Writer writer) {
this.writer = writer;
}
private void onEvent(RecordedEvent event) {
String serializedEvent = null;
try {
serializedEvent = String.valueOf(event.getThread());
} catch (Exception ex) {
ex.printStackTrace();
return;
}
try {
writer.write(serializedEvent);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}