我编写了一个 ratpack 处理程序来测量处理程序链的执行时间。
public class EndpointMetricsEmitter implements Handler {
public static final String HANDLE_DURATION_METRIC = "HANDLE_DURATION_MILLIS";
private final MetricContext baseContext;
private MonitoringRegistry registry;
public EndpointMetricsEmitter(MonitoringRegistry monitoringRegistry) {
this.registry = monitoringRegistry;
this.baseContext = MetricContext.newContext(REST);
}
@Override
public void handle(Context ctx) throws Exception {
Instant start = Instant.now();
ctx.next();
Instant end = Instant.now();
long dur = Duration.between(start, end).toMillis();
MetricContext endpointCtx = this.baseContext.withService(ctx.getPathBinding().getDescription());
this.registry.timer(endpointCtx, HANDLE_DURATION_METRIC, dur);
}
问题是我意识到 ctx.next()
是一个异步方法,它返回 void
。
有没有办法在链的最后一个处理程序返回时调用函数?
谢谢
最佳答案
One way to do it is with the ChannelHandlerContext attributeMap api.
我要做的是创建某种统计数据 pojo,在第一个处理程序中对其进行初始化并在最终处理程序中对其进行更新。
这样做的一个好处是,可选地,管道中的每个处理程序都可以跟踪其各自的性能并自行更新属性。
关于java - 有没有办法从 ratpack ctx.next() 获得 Promise,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61523640/