Question

we're going to use rabbitmq in our project, but facing a problem that, we want to debug on our dev machines, so the response message have to be send to machine which originally send the request message out. How we're going to achive that, is there an existing solution in spring-rabbitmq framework?

We have considered several solutions. such as declare a set of queues for each machine, the queue name prefix by machine name. Is that feasible?

Was it helpful?

Solution

Define set of queues (debug queue A-Z) and bind them to headers exchange with attributes x-match=any, from=[A-Z], to=[A-Z] respectively to . Then bind headers exchange to you main working exchange (one or more) to receive all messages you interested in, so when your consumer publish response it will be duplicated to your debug exchange and then routed to appropriate queue.

 [sender X]   [     worker      ]                       [consumer on queue X]
     |            ^          |                 
 [request]        |      [response from=X, to=X]      [duped request from=X|                 
      \           |          |                        [duplicated response from=X, to=X]
       \  [request from=X]   |                                 ^
        v         |          V                                 |
      [working topic exchange]           -------> [debug headers exchange]
       /           |         \                   /           |         \
   {bindings by routing key mask}           {bindings by any headers from=[A-Z], to=[A-Z]}
     /             |           \               /             |           \
[working queue 1] ...  [working queue N]   [debug queue A]  ...  [debug queue Z]

To bind request and response messages you can use applicationId and correlationId message attributes.

Note, that both request and response messages will be duplicated to debug queues. You may also specify separate queue for request and response messages by binding queues to match only specific headers, something like x-match=all, from=[A-Z] or x-match=all, to=[A-Z] and publish response and request messages with only that headers (only from or only to), but it is up to you.

The pros:

  • easy to implement
  • requires minimal code changes
  • easy to turn on/off
  • may be safely run in production environment

Cons:

  • use more resource power from RabbitMQ side

Alternatively, you can utilize RPC pattern somehow if you debugging process requires realtime response receiving. But this will block publisher until response processed, which may differ from real-world app usage and break business logic.

Pros:

  • step-by-step debugging process

Cons:

  • hard to implement
  • may require a lot of code changes
  • break business logic
  • hard to enable/disable
  • not production environment safe

p.s.: sorry for ascii graph

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top