Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Concurrent Synchronous Request-Reply with JMS/ActiveMQ - Patterns/Libraries?

Tags:

I have a web-app where when the user submits a request, we send a JMS message to a remote service and then wait for the reply. (There are also async requests, and we have various niceties set up for message replay, etc, so we'd prefer to stick with JMS instead of, say, HTTP)

In How should I implement request response with JMS?, ActiveMQ seems to discourage the idea of either temporary queues per request or temporary consumers with selectors on the JMSCorrelationID, due to the overhead involved in spinning them up.

However, if I use pooled consumers for the replies, how do I dispatch from the reply consumer back to the original requesting thread?

I could certainly write my own thread-safe callback-registration/dispatch, but I hate writing code I suspect has has already been written by someone who knows better than I do.

That ActiveMQ page recommends Lingo, which hasn't been updated since 2006, and Camel Spring Remoting, which has been hellbanned by my team for its many gotcha bugs.

Is there a better solution, in the form of a library implementing this pattern, or in the form of a different pattern for simulating synchronous request-reply over JMS?


Related SO question:

  • Is it a good practice to use JMS Temporary Queue for synchronous use?, which suggests that spinning up a consumer with a selector on the JMSCorrelationID is actually low-overhead, which contradicts what the ActiveMQ documentation says. Who's right?
like image 376
joshwa Avatar asked Aug 01 '12 08:08

joshwa


People also ask

How should I implement request response with JMS?

The best way to implement request-response over JMS is to create a temporary queue and consumer per client on startup, set JMSReplyTo property on each message to the temporary queue and then use a correlationID on each message to correlate request messages to response messages.

Is ActiveMQ synchronous?

The default setting for ActiveMQ is that all persistent messages outside of a transaction are sent to a broker are synchronous.

What is difference between JMS and ActiveMQ?

What Is the Difference Between JMS and ActiveMQ? ActiveMQ is a JMS provider. A JMS provider forms the software framework for facilitating the use of JMS concepts inside an application. A single node of ActiveMQ which allows clients to connect to it and use these messaging concepts is called an “ActiveMQ Broker.”

What is synchronous and asynchronous in JMS?

Synchronous—A message consumer must call a receive method to explicitly fetch a message from a destination. Asynchronous —A message listener is registered with a message consumer. When messages arrive for the message consumer, they are delivered by calling the listener's onMessage() method.


1 Answers

In a past project we had a similar situation, where a sync WS request was handled with a pair of Async req/res JMS Messages. We were using the Jboss JMS impl at that time and temporary destinations where a big overhead.

We ended up writing a thread-safe dispatcher, leaving the WS waiting until the JMS response came in. We used the CorrelationID to map the response back to the request.

That solution was all home grown, but I've come across a nice blocking map impl that solves the problem of matching a response to a request.

BlockingMap

If your solution is clustered, you need to take care that response messages are dispatched to the right node in the cluster. I don't know ActiveMQ, but I remember JBoss messaging to have some glitches under the hood for their clusterable destinations.

like image 81
maasg Avatar answered Sep 22 '22 14:09

maasg