[英]Make two requests in worker verticle and merge response from two requests
I have vertx server application where I am getting single client requests and from the server, I need to make two blocking calls. 我有vertx服务器应用程序,在该应用程序中我正在接收单个客户端请求,并且需要从服务器进行两次阻塞调用。 For instance, one call to back-end system A and another call to back-end system B. I am looking to make two concurrent calls to both the systems.
例如,一个调用到后端系统A,另一个调用到后端系统B。我正在寻找对两个系统的两个同时调用。 I need to wait for the responses from both the calls and then merge two data from both the calls and then send the response back to client.
我需要等待两个调用的响应,然后合并两个调用的两个数据,然后将响应发送回客户端。 I am unable to figure out how to do this in worker verticle.
我无法弄清楚如何在工人垂直线中执行此操作。
Could anyone recommend what would be the best approach in vertx? 谁能推荐在vertx中最好的方法是什么?
This sounds like a good use case for Promises. 这听起来像一个Promises的好用例。 Give the module vertx-promises a try.
尝试使用vertx-promises模块。
create a CompositeFuture from your launched Futures and handle it normally. 从您启动的期货创建一个CompositeFuture并正常处理。
public Future<JsonArray> getEntitiesByIndFields(String keyspace, String entidad, String field1, String field2) {
Promise<JsonArray> p = Promise.promise();
// launch in parallel
Future<JsonArray> f1 = getEntitiesByIndField1(keyspace, entidad, field1);
Future<JsonArray> f2 = getEntitiesByIndField2(keyspace, entidad, field2);
CompositeFuture.all(f1, f2).setHandler(done ->
{
if (done.failed()) {
p.fail(done.cause());
return;
}
List<JsonArray> ja = done.result().list();
JsonArray finalarray = ja.get(0);
ja.get(1).forEach(jo ->
{ // add one by one, don't duplicate ids
long id = ((JsonObject) jo).getLong("id");
if (!containsKey(finalarray, id)) {
finalarray.add(jo);
}
});
;
p.complete(finalarray); // send union of founds
});
return p.future();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.