import jdk.jfr.Configuration;
import jdk.jfr.Recording;

import java.io.IOException;
import java.nio.file.Path;
import java.text.ParseException;
import jdk.jfr.*;
import jdk.jfr.consumer.*;

public class Main {

    @Name("Test")
    @StackTrace(false)
    static class TestEvent extends Event {
       long id;
    }

    public static void emit(long id) {
       TestEvent e = new TestEvent();
       e.id = id;
       e.commit();
    }

    public static void main(String[] args) throws InterruptedException, IOException, ParseException {

        long pid = ProcessHandle.current().pid();
        System.out.println("PID.. " + pid);
        Recording r1 = new Recording(Configuration.getConfiguration("default"));

        r1.setDestination(Path.of("rec.jfr"));
        r1.start();
        emit(1);
        System.gc();

        Recording r2 = new Recording(Configuration.getConfiguration("default"));

        r2.setDestination(Path.of("rec.jfr"));
        r2.start();
        emit(2);
        Thread.sleep(2000);
        r2.stop();
        emit(3);
        r2.close();
        Thread.sleep(1000);
        emit(4);
        r1.stop();
        r1.close();
        for (RecordedEvent e : RecordingFile.readAllEvents(Path.of("rec.jfr"))) {
          if (e.getEventType().getName().equals("Test")) {
             System.out.println(e);
          }
        }
    }
}
