16 Manually Register and Unregister an Event
By default, an event is automatically registered when the event class is initialized. Alternatively, you can manually register an event with the @Registered annotation. One reason to do this is to take control of the security context in which the event is initialized.
The difference between the @Enabled annotation and the @Registered annotation is that when an event is unregistered, its metadata, such as the field layout, is not available for inspection. A call to FlightRecorder::register can ensure that an event class is visible for configuration, for example, to a Java Management Extensions (JMX) client.
The example RegistrationSample.java
demonstrates this:
import jdk.jfr.Event;
import jdk.jfr.FlightRecorder;
import jdk.jfr.Label;
import jdk.jfr.Name;
import jdk.jfr.Registered;
public class RegistrationSample {
@Name("com.oracle.Message")
@Label("Message")
@Registered(false)
static class Message extends Event {
String message;
}
public static void main(String... args) {
Message event1 = new Message();
event1.message = "Not registered, so you won't see this";
event1.commit();
FlightRecorder.register(Message.class);
Message event2 = new Message();
event2.message = "Now registered, so you will see this!";
event2.commit();
FlightRecorder.unregister(Message.class);
Message event3 = new Message();
event3.message = "Not registered again, so you won't see this";
event3.commit();
}
}