hamed
hamed

Reputation: 8033

spring rabbitmq - consume multiple messages at the same time

I'm using RabbitMQ in my spring boot application in this way:

Sender:

rabbitTemplate.convertAndSend("exchange", "routingKey", "Message Text");

Listener:

@RabbitListener(queues = "queueName")
public void receive(String message) {
    System.out.println("start");  
    //send an http request that takes for example 4 seconds 
    System.out.println("end");  
}

With above codes, when application executes sender part, receive method invoked. My problem is while receive method is processing a message, if sender part put another message into queue, the method does not proccess new message and so second start word wont be printed until end word of previous message. In the other words, I want to know, how a message listener can proccess multiple messages at a time I don't know what is the problem.

Upvotes: 2

Views: 5325

Answers (2)

user666
user666

Reputation: 2013

If you are using spring boot, just add this configuration to the application properties:

# Minimum number of listener invoker threads
spring.rabbitmq.listener.simple.concurrency=5

And your listener will start accepting messages in parallel (multiple threads). There are other configurations that you can check too. Like the max number of listener invoker threads (check spring boot doc for more info).

Upvotes: 1

Shailendra
Shailendra

Reputation: 9102

From the problem you are stating, it looks like your listener is configured for single thread. Refer to the container listener configuration docs here and here especially the concurrency settings. The concurrency settings control how many threads process messages on the queue at same time.

Upvotes: 3

Related Questions