Module jdk.jfr
Package jdk.jfr

Annotation Interface ContentType


@Target(ANNOTATION_TYPE) @Retention(RUNTIME) public @interface ContentType
元注解,指定一个注解表示内容类型,例如时间跨度或频率。

以下示例展示了如何创建和使用温度内容类型。

首先使用ContentType注解声明一个温度注解:

@MetadataDefinition
@ContentType
@Name("com.example.Temperature")
@Label("Temperature")
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Temperature {
    public final static String KELVIN = "KELVIN";
    public final static String CELSIUS = "CELSIUS";
    public final static String FAHRENEHIT = "FAHRENHEIT";

    String value() default CELSIUS;
}
然后声明一个事件,注解一个字段并提交数据:
@Name("com.example.CPU")
@Label("CPU")
@Category({ "Hardware", "CPU" })
@Period("1 s")
@StackTrace(false)
static public class CPUEvent extends Event {
    @Label("ID")
    String id;

    @Temperature(Temperature.KELVIN)
    @Label("Temperature")
    float temperature;
}

public static void main(String... args) throws InterruptedException {
    FlightRecorder.addPeriodicEvent(CPUEvent.class, () -> {
        for (var cpu : listCPUs()) {
            CPUEvent event = new CPUEvent();
            event.id = cpu.id();
            event.temperature = cpu.temperature(); // in Kelvin
            event.commit();
        }
    });
    Thread.sleep(10_000);
}
最后,在显示事件数据时检查注解:
void printTemperaturesInCelsius(Path file) throws IOException {
    for (RecordedEvent event : RecordingFile.readAllEvents(file)) {
        for (ValueDescriptor field : event.getEventType().getFields()) {
            for (AnnotationElement ae : field.getAnnotationElements()) {
                ContentType type = ae.getAnnotation(ContentType.class);
                if (type != null) {
                    if (ae.getTypeName().equals("com.example.Temperature")) {
                        double value = event.getDouble(field.getName());
                        String unit = (String) ae.getValue("value");
                        double celsius = switch (unit) {
                            case "CELSIUS" -> value;
                            case "KELVIN" -> value - 273.15;
                            case "FAHRENHEIT" -> (value - 32) / 1.8;
                            default -> throw new IllegalStateException("未知的温度单位 '" + unit + "'");
                        };
                        System.out.println(celsius + " 摄氏度");
                    } else {
                        System.err.println("无法格式化内容类型 " + ae.getTypeName() + " 用于字段 " + field.getName());
                    }
                }
            }
        }
    }
}
自 JDK 版本:
9