How to collect async results before return response

300 views
Skip to first unread message

ji...@cloudcar.com

unread,
Oct 2, 2015, 2:34:51 PM10/2/15
to vert.x
Hi, there, 

I am new for vert.x, and try to understand what to do in following case, 

I want to build a vertx. When a http request coming, the vertx will send a set of messages, each of which should be handled by its own async handler to call file system, DB, or other proxy service. At the end, the vertx, or its request handler, can be called when all the async processes are finished. 

I think I can do it with message reply if there is only one async processing involved, but could not figure out how to do with multiple async processes. It is highly appreciated in somebody can guide me or show me some sample codes. 

Many thanks in advance for the nice help. 

Jian  

jordan.h...@gmail.com

unread,
Oct 2, 2015, 4:20:31 PM10/2/15
to ve...@googlegroups.com
This is a pretty common pattern in asynchronous programming. Java 8's CompletableFuture has an allOf(...) method that allows you to execute a callback after all futures are completed. You can also look at RxJava support, which handles this type of thing quite succinctly. Or you can roll your own. Basically, all you need to do is use something like an AtomicInteger to count successful replies. Increment the counter in each reply callback, and once it has reached the expected number of replies, send the response. If you need an actual response to be built from each separate result, store the replies in something like a Set and use the set size() to determine when all asynchronous callbacks have completed.

I would write some code, but I'm not very good at coding on my phone :-)
--
You received this message because you are subscribed to the Google Groups "vert.x" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vertx+un...@googlegroups.com.
Visit this group at http://groups.google.com/group/vertx.
To view this discussion on the web, visit https://groups.google.com/d/msgid/vertx/47cb7b57-f623-4f6e-9941-c59bc2c0d70c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

ji...@cloudcar.com

unread,
Oct 2, 2015, 5:01:42 PM10/2/15
to vert.x
Thanks a lot for the nice help, Jordan. 

I know Java Concurrency Package. I guess what I really want to know is if vert.x provides standard API to deal the case like mine. To my best understanding, it is discouraged to use Java native concurrency package inside Vert.x. 

Jian

Blake

unread,
Oct 2, 2015, 9:08:42 PM10/2/15
to vert.x
Obviously you could write your own class to deal with this. Apart from that other's have written their own as well (here's one: https://github.com/aesteve/vertx-feeds/blob/master/src/main/java/io/vertx/examples/feeds/utils/async/MultipleFutures.java). I personally use RxJava for everything, because I find it much cleaner. (See Vertx Rx for it: http://vertx.io/docs/vertx-rx/java/ and RxJava: https://github.com/ReactiveX/RxJava -- specifically merge and combineLatest in the wiki on combining)

ji...@cloudcar.com

unread,
Oct 5, 2015, 2:29:24 AM10/5/15
to vert.x
Thanks a lot for the nice help. I will try it and share my experience here. 

Jian
Reply all
Reply to author
Forward
0 new messages