java - 有没有办法从 ratpack ctx.next() 获得 Promise

标签 java asynchronous netty ratpack

我编写了一个 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/

相关文章:

java - 为 GWT/Spring/Hibernate/PostgreSQL 生成服务/dao 层

java - 如何取消定时器的多个实例?

java - 在 URL 中包含变量,返回错误页面

java - 使用流 API 在 Java 中按 n 个字段分组

swift - PromiseKit 如何返回 "when(resolved)"作为 promise ?

JavaScript 异步事件

javascript - 使用 jQuery.Deferred 自定义 JS 确认模态以及基于按钮的返回值问题

tomcat - netty , tomcat 线程模型

java - 使用ExecutionHandler多线程处理receivedMessage

mysql - 提高只写表的性能?