3 回答

TA贡献1898条经验 获得超8个赞
用 CompleteFuture
private String sendCommand(String address, String command) {
CompletableFuture<String> completableFuture = new CompletableFuture<>();
vertx.eventBus().<String>send(address, command, asyncResult -> {
if (asyncResult.succeeded()) {
completableFuture.complete(asyncResult.result().body());
} else {
completableFuture.completeExceptionally(asyncResult.cause());
}
});
try {
return completableFuture.get();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
请确保此代码不会在 Vert.x 事件循环上调用,因为get()在知道答复之前会阻塞。

TA贡献1936条经验 获得超6个赞
的EventBus
是为异步消息传递(发布/订阅消息,点对点和请求响应消息)制成。强制同步动作没有意义。
如果您想要同步响应,如果您在同一个 JVM 中,只需调用另一个 java 类中的方法。

TA贡献2011条经验 获得超2个赞
添加回答
举报