1

I would like to know what happens if MessageListenerContainer, pointing to a topic, recieves 2 (or more) messages at the same time.

For example, 2 users of the app triggers a publish at the same time, hence 2 jmstemplate calls sending 2 different messages to the same topic.

How will the container handle this?

  1. Will the container create 2 threads calling the MessageListenerAdapter (which should implemented as thread-safe) at same time?
  2. Or will the container have just 1 thread calling the MessageListenerAdapter serially (sequentially)?
Carlos Jaime C. De Leon
  • 2,476
  • 2
  • 37
  • 53

1 Answers1

1

The concurrency setting controls how many listeners consume messages concurrently. You should not use more than 1 consumer for topics, otherwise, the message may be consumed twice on the same node.

<jms:listener-container
  container-type="default"
  connection-factory="connectionFactory"
  acknowledge="auto"
  concurrency="1"     
  cache="consumer">
  <jms:listener destination="TEST.FOO" 
                ref="simpleMessageListener"
                method="onMessage" />
</jms:listener-container>

See this too... https://stackoverflow.com/a/5808803/791406

Spring Docs... http://static.springsource.org/spring/docs/3.0.5.RELEASE/api/org/springframework/jms/listener/DefaultMessageListenerContainer.html

Community
  • 1
  • 1
raffian
  • 31,267
  • 26
  • 103
  • 174
  • thanks for you reply. but if i set the concurrency to 1, does that mean that 2 messages simultaneously received by the container/adapter will be processed serially, not in parallel? – Carlos Jaime C. De Leon Jun 08 '13 at 07:08
  • Yes, for a topic with 1 listener per node, 2 separate messages will be processed serially. If you set `concurrency > 1` for the same topic, the same message will be processed in parallel for each listener; that's sort of pointless for topics. What's your use case; what's in the message? If it's a news broadcast or something similar, use topics, but if you require exactly-once processing for any unique message, say, for a customer purchase order, use a queue and set `concurrency > 1` to process many messages in parallel. – raffian Jun 08 '13 at 10:42