- java.lang.Object
-
- jdk.jfr.Event
-
public abstract class Event extends Object
Base class for events, to be subclassed in order to define events and their fields.Example,
import jdk.jfr.Event; import jdk.jfr.Description; import jdk.jfr.Label; public class Example { @Label("Hello World") @Description("Helps programmer getting started") static class HelloWorld extends Event { @Label("Message") String message; } public static void main(String... args) { HelloWorld hwe = new HelloWorld(); hwe.message = "hello, world!"; hwe.commit(); } }Once an event has been allocated and its field members populated, it can be committed to the Flight Recorder system using the
commit()method.Events are enabled by default, but can be disabled by annotating the event class with
@Enabled(false).Supported field types are the Java primitives:
boolean,char,byte,short,int,long,floatanddouble. Additionally the following reference types are supported:String,ThreadandClass. Arrays, enums and other reference types will be silently ignored and not included. Fields that are of the supported types can be excluded by using the transient modifier. Static fields, even of the supported types, are not included.In order for tools to visualize data in a meaningful way, use annotations, for example:
Label,DescriptionandTimespan. Annotations applied to event classes or its fields will be included if they are present (indirectly, directly or associated), have theMetadataDefinitionannotation and they do not contain enums, arrays or classes.If performing data gathering operations, in order to populate an event, is considered expensive, the
shouldCommit()method can be used to pre-check if this event instance would actually be written to the system whencommit()is invoked. IfshouldCommit()returns false , then those operations can be avoided.- Since:
- 9
-
-
Constructor Summary
Constructors Modifier Constructor Description protectedEvent()Sole constructor.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidbegin()Starts timing of this event.voidcommit()Writes field values, timestamp and event duration to the Flight Recorder system.voidend()Ends timing of this event.booleanisEnabled()Returnstrueif at least one recording is running, and the enabled setting for this event is set totrue, otherwisefalseis returned.voidset(int index, Object value)Sets a field value, only applicable if the event is dynamically defined usingEventFactory.booleanshouldCommit()Returnstrueif event is enabled and if the duration, time elapsed between an invocation to begin and end, is within the threshold for the event, otherwisefalseis returned.
-
-
-
Method Detail
-
begin
public final void begin()
Starts timing of this event.
-
end
public final void end()
Ends timing of this event. Method must be invoked after the begin method
-
commit
public final void commit()
Writes field values, timestamp and event duration to the Flight Recorder system.If the event was started with an invocation of
begin, but not ended with an explicit invocation toend, the event will end when thecommitmethod is invoked.
-
isEnabled
public final boolean isEnabled()
Returnstrueif at least one recording is running, and the enabled setting for this event is set totrue, otherwisefalseis returned.- Returns:
trueif event is enabled,falseotherwise
-
shouldCommit
public final boolean shouldCommit()
Returnstrueif event is enabled and if the duration, time elapsed between an invocation to begin and end, is within the threshold for the event, otherwisefalseis returned. The threshold is determined by taking the minimum threshold for all running recordings.- Returns:
trueif the event can be committed,falseotherwise
-
set
public final void set(int index, Object value)Sets a field value, only applicable if the event is dynamically defined usingEventFactory.The supplied index corresponds to the index of the field in
ValueDescriptorpassed when the event factory was created.- Parameters:
index- index of the field passed toEventFactory#create(String, java.util.List, java.util.List)value- value to set, may benull- Throws:
UnsupportedOperationException- if it's not a dynamically generated eventIndexOutOfBoundsException- ifindexis less than0or greater or equal than the number of fields for the event- See Also:
EventType.getFields(),EventFactory
-
-