我编写了一个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
.
有没有办法在链的最后一个处理程序返回时调用函数?
谢谢你