- java.lang.Object
-  
      - jdk.jfr.EventFactory
 
-  
       
 public final class EventFactory extends Object 用于在运行时定义事件的类。强烈建议在编译时定义事件,如果字段布局已知,那么Java虚拟机(JVM)可以优化代码,如果Flight Recorder处于非活动状态或者此事件的启用设置为设置为 false。要在编译时定义事件,请参阅 Event。以下示例显示如何实现动态 Event类。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();- 从以下版本开始:
- 9
 
-  
        
       -  
             方法摘要所有方法 静态方法 实例方法 具体的方法 变量和类型 方法 描述 static EventFactorycreate(List<AnnotationElement> annotationElements, List<ValueDescriptor> fields)创建一个EventFactory对象。EventTypegetEventType()返回与此事件工厂关联的事件类型。EventnewEvent()实例化一个事件,因此可以填充数据并写入Flight Recorder系统。voidregister()注册未注册的活动。voidunregister()取消注册与此事件工厂关联的事件。
 
-  
             
-  
        
       -  
             方法详细信息-  createpublic static EventFactory create(List<AnnotationElement> annotationElements, List<ValueDescriptor> fields) 创建一个EventFactory对象。值描述符的顺序指定设置事件值时要使用的索引。 - 参数
-  
              annotationElements- 描述事件注释的注释元素列表,而不是null
-  
              字段- 描述事件字段的描述符列表,而不是null
- 结果
- 
               事件工厂,而不是 
              null
- 异常
-  
              IllegalArgumentException- 如果输入无效。 例如,如果字段类型或名称在Java语言中无效或者注释元素引用了无法找到的类型,则输入可能无效。
-  
              SecurityException- 如果存在安全管理器且调用者没有FlightRecorderPermission("registerEvent")
- 另请参见:
-  
              Event.set(int, Object)
 
 -  newEventpublic Event newEvent() 实例化一个事件,因此可以填充数据并写入Flight Recorder系统。使用 Event.set(int, Object)方法设置值。- 结果
- 
               事件实例,而不是 
              null
 
 -  getEventTypepublic EventType getEventType() 返回与此事件工厂关联的事件类型。- 结果
- 
               与此事件工厂关联的事件类型,而不是 
              null
- 异常
-  
              IllegalStateException- 如果使用Registered(false)批注创建事件工厂,并且在调用此方法之前未手动注册事件类
 
 -  registerpublic void register() 注册未注册的活动。默认情况下,创建事件工厂时会注册与此事件工厂关联的事件类,除非事件具有 Registered注释。已注册的事件类可以将数据写入Flight Recorder,并且可以通过调用 FlightRecorder.getEventTypes()获取事件元数据。如果已注册与此事件工厂关联的事件类,则忽略对此方法的调用。 - 异常
-  
              SecurityException- 如果存在安全管理器且调用者没有FlightRecorderPermission("registerEvent")
- 另请参见:
-  
              Registered,FlightRecorder.register(Class)
 
 -  unregisterpublic void unregister() 取消注册与此事件工厂关联的事件。未注册的事件类无法将数据写入Flight Recorder,并且无法通过调用 FlightRecorder.getEventTypes()获取事件元数据。如果尚未注册与此事件工厂关联的事件类,则忽略对此方法的调用。 - 异常
-  
              SecurityException- 如果存在安全管理器且调用者没有FlightRecorderPermission("registerEvent")
- 另请参见:
-  
              Registered,FlightRecorder.unregister(Class)
 
 
-  
 
-