How can we receive multiple replies for a single request?

riddhisurve
riddhisurve Member Posts: 2

I'm implementing the pubsub+ request-reply pattern where I need to receive multiple records as a reply for a particular request but, I'm receiving just one record as a reply per request after which the session exits.
Can someone suggest what can be done for this.
Reference - https://tutorials.solace.dev/jcsmp/request-reply/

Answers

  • TomF
    TomF Member, Employee Posts: 406 Solace Employee

    Hello @riddhisurve, the way the BasicRequestor sample works is to send a blocking request. It waits for the reply, or times out, then exits. This isn't normally the way you'd do things: you send the request, wait for the reply, then send more requests or wait for something else to happen.

    Given you are waiting for multiple replies to a single request, you'd be better off moving to a non-blocking request-reply model. Here, we tag the outgoing request with a correlation identifier, so that when the response comes back you can work out which request it relates to. When you receive multiple replies you can then simply correlate them via the correlation ID.

    To summarise:

    • Add a correlationID to the request with setCorrelationId.
    • Use a non-blocking request send: set request(..., timeout,...) timeout to 0.
    • In the repliers, reply to the message remembering to set the correlationID
    • In the requestor, you'll be getting multiple replies asynchronously, so set up XMLMessageConsumer or its XMLMessageListener for replies.
    • Use getCorrelationID on the replies to determine which request the reply corresponds to.
    • Don't allow your application to exit until you're happy all the responses have been received.
  • riddhisurve
    riddhisurve Member Posts: 2

    Hey @TomF I do have XMLMessageConsumer running.
    It seems the "reply = requestor.request(request, timeoutMs, topic)" statement receives just one reply after sending the request and exits. Thus the rest of the sendReply calls from the replier seems to get lost as there is no variable receiving it.
    For me to receive all the records I have to hit the above statement again and again which will be the wrong way of doing it.

  • TomF
    TomF Member, Employee Posts: 406 Solace Employee

    @riddhisurve yes you're right, I forgot a key step! requestor.request will return after one response. What you need to do is specify a "Reply-To" topic using .setReplyTo() in your outgoing request. You then need to subscribe to this Reply To topic so your XMLMessageConsumer gets the extra responses.

    To make the code more logical, you might want to consider removing the requestor completely and publish the request as a message using XMLMessageProducer, then dealing with the responses via the Reply To subscription.

  • marc
    marc Member, Administrator, Moderator, Employee Posts: 914 admin

    +1 for this; I think leaving the "requestor" could be confusing.

    To make the code more logical, you might want to consider removing the requestor completely and publish the request as a message using XMLMessageProducer, then dealing with the responses via the Reply To subscription.