繁体   English   中英

如何在 Vertx 事件循环线程上运行 CompletableFuture 处理程序?

[英]How do I run a CompletableFuture handler on the Vertx event loop thread?

我有一个库 xyz,它给了我一个 CompletableFuture,我想在我的 Vertx (v3.5) 事件循环中处理它。 目前我正在使用CompletableFuture.handle(BiFunction) (见下文)但我想使用CompletableFuture.handleAsync(BiFunction, Executor) ,但我无法弄清楚如何在此方法调用中将 vertx 事件循环线程提供给第二个参数.

我尝试在Vertx.runOnContext()中执行整个代码,但 handleAsync 中的调用仍然在我想避免的 Java 的 ForkJoin 池中执行。

CompletableFuture<Void> f = xyz.someMethod();
f.handle((v, th) -> { //Want to run this in handleAsync()
   if (th == null) {
     future.complete(null);
   } else {
     future.completeExceptionally(th);
   }
   return null;
});

您可以通过简单地使用vertx.nettyEventLoopGroup()作为第二个参数来实现这一点,因此您的代码将如下所示:

CompletableFuture<Void> f = xyz.someMethod();
f.handleAsync((v, th) -> { 

  if (th == null) {
    future.complete(null);
  } else {
    future.completeExceptionally(th);
  }

  return null;
}, vertx.nettyEventLoopGroup());

注意:上面的代码可能会运行回调代码,而不是在 Vertx future 运行的同一线程上。

要保留 Vertx 的线程模型,您需要使用以下代码:

CompletableFuture<String> f = xyz.someMethod();
Context context = vertx.getOrCreateContext();
f.handleAsync((v, th) -> {
    context.runOnContext(event -> {
        if (th == null) {
            future.complete(null);
        } else {
            future.completeExceptionally(th);
        }
    });
    return null;
});

这可以通过以下方式完成。 这是一种解决方法,而不是最佳解决方案。 最初我将 runOnContext 调用放在 handle 方法之外,这就是它不起作用的原因。

CompletableFuture<Void> f = xyz.someMethod(); f.handle((v, th) -> { vertx.runOnContext(e->{ if (th == null) { future.complete(null); } else { future.completeExceptionally(th); } }); return null; });

这将进行一次额外的上下文切换(从 ForkJoin 池到 vertx 事件循环),这是这种方法的一个缺点。

暂无
暂无

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

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