Search code examples
jmsspring-jms

Disable parallel processing of multiple JmsListeners


In my Spring Boot JMS application I want to ensure that only one JMSListener is working at a time.

My Application is listening to two queues - Queue-A and Queue-B.

While a message from Queue-A is processed the application shall not start processing of a message of Queue-B.

@JmsListener(destination = "Queue-A", containerFactory = "factory")
public void recieveFromQueueA(final Message jmsMessage)  {
     // do the neccessary to process msg from Queue-A
} 


@JmsListener(destination = "Queue-B", containerFactory = "factory")
public void receiveFromQueueB(final Message jmsMessage) {
    // do the necessary to process msg from Queue-B
}

I tried several things like factory.setConcurrency("1-1") and setting a maxThreadPoolsize but it seems all the settings only apply to one JmsListener, but not to them all collectively.

I have a Spring Boot app, but it is not a web app (no REST / Web Endpoints, no Tomcat or what so ever) and I am using spring-boot-starter-activemq.


Solution

  • It is hard to understand why you don't want to have messages from Queue-A and Queue-B to be processed in parallel. If it was clear what you try to achieve, maybe alternative suggestions would apply. But let's now assume there are good reasons. As there is such a strict dependency between Queue-A and Queue-B processing, I question the design decision to separate both messages into two queues. You could let the ActiveMQ definitions be changed in a way that both kind of messages arrive on the same queue as input to your application (a task outside of the scope of your Spring Boot application though). You would than set up only one JMSListener with concurrency 1-1. Part of the overall application design should be a way to easily distinct between both kind of messages in your code (for example by letting all senders set a message property, if not already available). This way you can easily forward the message to either type-A or type-B processing. You would never have parallel processing of both types of input.