事件注解,指定周期性事件的默认设置值。
以下示例展示了如何使用Period
注解来在不同间隔下触发事件。
@Period("1 s")
@Name("Counter")
class CountEvent extends Event {
int count;
}
@Period("3 s")
@Name("Fizz")
class FizzEvent extends Event {
}
@Period("5 s")
@Name("Buzz")
class BuzzEvent extends Event {
}
var counter = new AtomicInteger();
FlightRecorder.addPeriodicEvent(CountEvent.class, () -> {
CountEvent event = new CountEvent();
event.count = counter.incrementAndGet();
event.commit();
});
FlightRecorder.addPeriodicEvent(FizzEvent.class, () -> {
new FizzEvent().commit();
});
FlightRecorder.addPeriodicEvent(BuzzEvent.class, () -> {
new BuzzEvent().commit();
});
var sb = new StringBuilder();
var last = new AtomicInteger();
var current = new AtomicInteger();
try (var r = new RecordingStream()) {
r.onEvent("Counter", e -> current.set(e.getValue("count")));
r.onEvent("Fizz", e -> sb.append("Fizz"));
r.onEvent("Buzz", e -> sb.append("Buzz"));
r.onFlush(() -> {
if (current.get() != last.get()) {
System.out.println(sb.isEmpty() ? current : sb);
last.set(current.get());
sb.setLength(0);
}
});
r.start();
}
- 自 JDK 9 起
-
Optional Element Summary
-
Field Summary
-
Field Details
-
NAME
用于配置周期性事件的设置名称为"period"
。- 参见:
-
-
Element Details
-
value
String value返回周期性设置的默认设置值。正整数
Long
值的字符串表示,后跟一个空格和以下单位之一:"ns"
(纳秒)"us"
(微秒)"ms"
(毫秒)"s"
(秒)"m"
(分钟)"h"
(小时)"d"
(天)示例值:
"0 ns"
,"10 ms"
和"1 s"
。周期也可以是
"everyChunk"
,表示它至少在每个记录文件中发生一次。发出的事件数量取决于数据记录时文件旋转发生的次数。- 返回:
-
默认设置值,非
null
- 默认值:
-
"everyChunk"
-