vert.xvertx-eventbus

Vertx: request/response with response stream


I want to implement a verticle that can respond to a request with a long running stream via the event bus. Looking at the event bus, both the request/response and pub/sub patterns are supported - I want to combine them into a kind of request/subscribe pattern.

With request/response, you would do something like this:

eventbus.request("address", myRequest).onSuccess(msg -> processResponse(msg)); //single response

What I want is something like this:

eventbus.request("address", myRequest).handle(msg -> processNewResponse(msg)); // many responses over time

I'm unsure what the best way to achieve this is. One possible way, which I have done in the past and is certainly workable but a bit cumbersome, is to have a two-phase approach, where you setup the temporary stream address with a request/response and then subscribe to that address like so:

eventbus.request("address", myRequest)
    .onSuccess(msg -> eventbus.consumer(msg.body().getTempStreamAddress(), msg2 -> processNewResponse(msg2)))

But this means dealing with creating temporary addresses and managing the subscription on the verticle, which I'm happy to do but only if that turns out to be the best option, but I'm wondering if I'm missing a simple trick here?


Solution

  • I'm wondering if I'm missing a simple trick here?

    No you are not

    There is no way for a single sided stream on the eventbus

    The way you went about it is the right approach