I have a List
of Future
and need to handle results on each Future
when it completes. Once all Futures are completed I need to compose a response.
My simplified code to create the list of futures and their handler:
final List<Future<MyResponse1>> futureMyResponse1Lst = new ArrayList<>();
for (final MyObject1 obj1: listOfObject1) {
final Future<MyResponse1> resp1Future = myRestClient.getMyResponse1(obj1);
resp1Future .setHandler(new Handler<AsyncResult<MyResponse1>>() {
@Override
public void handle(final AsyncResult<MyResponse1> event) {
final MyResponse1 resp1= event.result();
handleResponse1(obj1, resp1);
}
});
futureMyResponse1Lst.add(resp1Future );
}
At this stage all handler are called on each Future result.
But if I add a CompositeFuture
only the Handler
of the CompositeFuture
is called:
final List<Future> futuresAll = new ArrayList<>();
futuresAll.addAll(futureMyResponse1Lst );
CompositeFuture.all(futuresAll).setHandler(new Handler<AsyncResult<CompositeFuture>>() {
@Override
public void handle(final AsyncResult<CompositeFuture> event) {
// called when all future are completed
logger.debug("handle end of CompositeFuture and create response");
}
});
All questions and answers I found propose to use CompositeFuture but in that case the Future single Handler is not called.
How can I have a handler called on each Future
result and additionally one when all Future are completed? I am quite new to Async programming and Vert.x, am I on the right track using CompositeFuture
? Should I use RxJava
instead? How?
You can create an additional Future that will be completed in the original Futures handler and use that Future for CompositeFuture.
final List<Future<MyResponse1>> futureMyResponse1Lst = new ArrayList<>();
for (final MyObject1 obj1: listOfObject1) {
final Future<MyResponse1> resp1Future = myRestClient.getMyResponse1(obj1);
Future<MyResponse1> resp1FutureComposite = Future.future();
resp1Future.setHandler(event-> {
final MyResponse1 resp1= event.result();
handleResponse1(obj1, resp1);
resp1FutureComposite.compelete(resp1);
});
futureMyResponse1Lst.add(resp1FutureComposite);
}
If you care only about the successful call and don't want to handle the errors per future (and then handle it in the CompositeFuture handler) then you can use compose method instead:
final List<Future<MyResponse1>> futureMyResponse1Lst = new ArrayList<>();
for (final MyObject1 obj1: listOfObject1) {
final Future<MyResponse1> resp1Future = myRestClient.getMyResponse1(obj1);
Future<MyResponse1> resp1FutureComposite = resp1Future.compose(resp1 -> {
handleResponse1(obj1, resp1);
return Future.succeededFuture(resp1);
});
futureMyResponse1Lst.add(resp1FutureComposite);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.