Module jdk.jfr
Package jdk.jfr

Class EventFactory

java.lang.Object
jdk.jfr.EventFactory

public final class EventFactory extends Object
Class for defining an event at runtime.

It's highly recommended that the event is defined at compile time, if the field layout is known, so the Java Virtual Machine (JVM) can optimize the code, possibly remove all instrumentation if Flight Recorder is inactive or if the enabled setting for this event is set to false.

To define an event at compile time, see Event.

The following example shows how to implement a dynamic Event class.

List<ValueDescriptor> fields = new ArrayList<>();
List<AnnotationElement> messageAnnotations = Collections.singletonList(new AnnotationElement(Label.class, "Message"));
fields.add(new ValueDescriptor(String.class, "message", messageAnnotations));
List<AnnotationElement> numberAnnotations = Collections.singletonList(new AnnotationElement(Label.class, "Number"));
fields.add(new ValueDescriptor(int.class, "number", numberAnnotations));

String[] category = { "Example", "Getting Started" };
List<AnnotationElement> eventAnnotations = new ArrayList<>();
eventAnnotations.add(new AnnotationElement(Name.class, "com.example.HelloWorld"));
eventAnnotations.add(new AnnotationElement(Label.class, "Hello World"));
eventAnnotations.add(new AnnotationElement(Description.class, "Helps programmer getting started"));
eventAnnotations.add(new AnnotationElement(Category.class, category));

EventFactory f = EventFactory.create(eventAnnotations, fields);

Event event = f.newEvent();
event.set(0, "hello, world!");
event.set(1, 4711);
event.commit();

Since:
9