Search code examples
springspring-bootwebsocketactivemq-classicstomp

Is it guaranteed that subscription for response is established before request is processed in temp-topic based request/response scheme?


I would like to implement an request/response pattern in the following parts:

  • Server: Springboot with ActiveMQ
  • Client: JavaScript with stompjs over websocket

I want a behaviour like an http request.

Send a Request, get a corresponding Response.

I try to do this with temporary channels I send 3 Messages

The steps are:

  1. SUBSCRIBE to a temprorary channel

    SUBSCRIBE
    id:mysub
    destination:/temp-queue/example
    
  2. SEND the request and include a reply-to header with the subscribed channel of Step 1

    destination:/queue/PO.REQUEST
    reply-to:/temp-queue/example
    
  3. Get the Response Message from the Server

    MESSAGE
    subscription:foo
    reply-to:/queue/temp.default.23.example
    destination:/queue/PO.REQUEST
    reply-to:/temp-queue/example
    

But now (As Client send messages asynchronous) im not sure if Step 1 is complete on server, and so server is ready to send Response to the queue when the Request of Step 2 arrives at the server.

Is it possible that server finishes Step 2 before finishing Step 1, and so sends the response to nowhere? Or does ActiveMQ ensures that the received messages 1 and 2 from the client are processed and finished in the correct order?

Can any race condition between message 1 and 2 happen?

Thank you very much!


Solution

  • Any STOMP frame that your client sends can be sent with a receipt request that makes the processing of that frame synchronous. So if you want to ensure that a subscribe is complete before doing the send, then attach a receipt-id to the subscribe frame and wait for the spec mandated RECEIPT frame before going on to do the send, this ensures that your subscription is setup prior to any other processing.