0

I am pretty new to Spring AMQP module. I was successful to create simple project which produces and consumes messages. What I don't understand is following:

If there is only one listener and more than one concurrent consumers are configured in SimpleMessageListenerContainer, how it will improve the performance? As per my understanding, as long as I have single listener which processes the message, no matter how many consumers(threads) picks up the messages from the queue does not matter.

Here's my code for your reference:

@Bean
public SimpleMessageListenerContainer messageListenerContainer() {
SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(connectionFactory());
container.setQueues(someQueue());
container.setMessageListener(messageListenerAdapter());
container.setConcurrentConsumers(3);
return container;
}

I am sure, I am missing something in my understanding. Can someone throw light please. Thanks in advance.

Gary Russell
  • 166,535
  • 14
  • 146
  • 179
Atul Kumbhar
  • 1,073
  • 16
  • 26

1 Answers1

3

The container will create 3 threads; each of which is registered as a consumer. It is equivalent to creating 3 separate containers with 1 consumer each.

Each consumer will call your single listener - it must be thread-safe - no shared data/fields or access to any such data must be synchronized.

It's generally best, however, to use a state-less bean for your listener so you don't have to worry about concurrency.

If you can't make your listener thread-safe, you must create 3 separate containers and provide each one with its own instance of your listener.

Gary Russell
  • 166,535
  • 14
  • 146
  • 179
  • I am facing the same issue, my new consumer created automatically because of **setMaxConcurrentConsumers int springboot**, and my SecurityContext get override by one of the thread. Could you please give some reference doc which you have just mentioned ? – Alpesh Jikadra Aug 02 '22 at 16:57
  • 1
    It is not clear what you mean. This answer is over 6 years old; ask a new question showing your code and configuration, with much more detail. spring-amqp does nothing with security contexts internally. – Gary Russell Aug 02 '22 at 17:40