IOException { CompletableFuture.runAsync(() -> { while (true) { try { var event = new SampleCustomEvent2(); event.begin(); event.myproperty = "Hello, " + System.currentTimeMillis(); event.commit(); Thread.sleep(1000); } catch (InterruptedException ex) { Logger.getLogger(ExampleStreamJFR.class.getName()).log(Level.SEVERE, null, ex); } } }); try (var rs = new RecordingStream()) { rs.enable("jdk.CPULoad").withPeriod(Duration.ofSeconds(1)); rs.onEvent("jdk.CPULoad", System.out::println); rs.onEvent(SampleCustomEvent2.class.getName(), (event) -> { System.err.println("my event:" + event.getString("myproperty")); }); rs.start(); } }