简体   繁体   English

有没有办法从 ratpack ctx.next() 获取 Promise

[英]Is there a way to get a Promise from ratpack ctx.next()

I have written a ratpack handler that measures the execution time of a handler chain.我编写了一个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);

  }

The problem is that I realised that ctx.next() is an async method and it returns void .问题是我意识到ctx.next()是一个异步方法,它返回void

is there a way to invoke a function in when the last handler of the chain returns?有没有办法在链的最后一个处理程序返回时调用 function?

thank you谢谢你

One way to do it is with the ChannelHandlerContext attributeMap api. 一种方法是使用 ChannelHandlerContext attributeMap api。

What I would do is create some sort of statistics pojo, initialize it within the first handler and update it in your final handler.我要做的是创建某种统计信息 pojo,在第一个处理程序中对其进行初始化,并在您的最终处理程序中对其进行更新。

A benefit of this is that optionally, each handler in the pipeline can track its individual performance and update the Attribute themselves.这样做的一个好处是,管道中的每个处理程序都可以选择跟踪其各自的性能并自行更新属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM