EventFactory (Java SE 26 & JDK 26)
Description: declaration: module: jdk.jfr, package: jdk.jfr, class: EventFactory
Keywords: jdk.jfr.EventFactory class

# 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

- ## Method Summary

  Modifier and Type
  Method
  Description
  `static EventFactory`
  `create(List<AnnotationElement> annotationElements, List<ValueDescriptor> fields)`
  Creates an `EventFactory` object.
  `EventType`
  `getEventType()`
  Returns the event type that is associated with this event factory.
  `Event`
  `newEvent()`
  Instantiates an event, so it can be populated with data and written to the Flight Recorder system.
  `void`
  `register()`
  Registers an unregistered event.
  `void`
  `unregister()`
  Unregisters the event that is associated with this event factory.

  ### Methods declared in class Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`
  Modifier and Type
  Method
  Description
  `protected Object`
  `clone()`
  Creates and returns a copy of this object.
  `boolean`
  `equals(Object obj)`
  Indicates whether some other object is "equal to" this one.
  `protected void`
  `finalize()`
  Deprecated, for removal: This API element is subject to removal in a future version.
  Finalization is deprecated and subject to removal in a future release.
  `final Class<?>`
  `getClass()`
  Returns the runtime class of this `Object`.
  `int`
  `hashCode()`
  Returns a hash code value for this object.
  `final void`
  `notify()`
  Wakes up a single thread that is waiting on this object's monitor.
  `final void`
  `notifyAll()`
  Wakes up all threads that are waiting on this object's monitor.
  `String`
  `toString()`
  Returns a string representation of the object.
  `final void`
  `wait()`
  Causes the current thread to wait until it is awakened, typically by being notified or interrupted.
  `final void`
  `wait(long timeoutMillis)`
  Causes the current thread to wait until it is awakened, typically by being notified or interrupted, or until a certain amount of real time has elapsed.
  `final void`
  `wait(long timeoutMillis, int nanos)`
  Causes the current thread to wait until it is awakened, typically by being notified or interrupted, or until a certain amount of real time has elapsed.

- ## Method Details

  - ### create

    public static EventFactory create(List<AnnotationElement> annotationElements, List<ValueDescriptor> fields)
    Creates an `EventFactory` object.

    The order of the value descriptors specifies the index to use when setting event values.

    Parameters:
      `annotationElements` - list of annotation elements that describes the annotations on the event, not `null`
      `fields` - list of descriptors that describes the fields of the event, not `null`
    Returns:
      event factory, not `null`
    Throws:
      `IllegalArgumentException` - if the input is not valid. For example, input might not be valid if the field type or name is not valid in the Java language or an annotation element references a type that can't be found.
    See Also:
      - `Event.set(int, Object)`

  - ### newEvent

    public Event newEvent()
    Instantiates an event, so it can be populated with data and written to the Flight Recorder system.

    Use the `Event.set(int, Object)` method to set a value.

    Returns:
      an event instance, not `null`

  - ### getEventType

    public EventType getEventType()
    Returns the event type that is associated with this event factory.
    Returns:
      event type that is associated with this event factory, not `null`
    Throws:
      `IllegalStateException` - if the event factory is created with the `Registered(false)` annotation and the event class is not manually registered before the invocation of this method

  - ### register

    public void register()
    Registers an unregistered event.

    By default, the event class associated with this event factory is registered when the event factory is created, unless the event has the `Registered` annotation.

    A registered event class can write data to Flight Recorder and event metadata can be obtained by invoking `FlightRecorder.getEventTypes()`.

    If the event class associated with this event factory is already registered, the call to this method is ignored.

    See Also:
      - `Registered`
      - `FlightRecorder.register(Class)`

  - ### unregister

    public void unregister()
    Unregisters the event that is associated with this event factory.

    A unregistered event class can't write data to Flight Recorder and event metadata can't be obtained by invoking `FlightRecorder.getEventTypes()`.

    If the event class associated with this event factory is not already registered, the call to this method is ignored.

    See Also:
      - `Registered`
      - `FlightRecorder.unregister(Class)`