Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RabbitMQ: prefetched messages processing

I am using Spring AMQP to work with RabbitMQ. Here is my configuration:

<rabbit:connection-factory id="connectionFactory"
    host="${queue.host}" port="${queue.port}" />
<rabbit:template id="amqpTemplate" connection-factory="connectionFactory" />
<rabbit:admin connection-factory="connectionFactory" />
<rabbit:queue name="${queue.names}" durable="true"
    exclusive="false" />
<rabbit:listener-container
    connection-factory="connectionFactory" acknowledge="auto" error-handler="airbrakeHandler" prefetch="1000" >
    <rabbit:listener ref="consumer" queue-names="${queue.names}" />
</rabbit:listener-container>

As you can see the prefetchCount is 1000.

I was wondering whether the the prefetched messages are processed in parallel in the consumer; that is, multiple threads calling the onMessage(Message message) method. Or are the messages rather processed sequentially; that is, one thread that iterates over the prefetched messages and calls on each message the onMessage(Message message) method in a sequntial manner.

I should note that the order of the processing is not important for me. Just the fact that they are processed one at a time.

Thanks in advance.

like image 255
Yonatan Wilkof Avatar asked Nov 01 '22 15:11

Yonatan Wilkof


1 Answers

The SimpleMessageListenerContainer configuration includes a concurrency parameter to set the maximum number of consumer threads:

concurrency: The number of concurrent consumers to start for each listener.

For single-threaded operation, you'd set this to "1" - for example:

<rabbit:listener-container ... prefetch="1000" concurrency="1">
    <rabbit:listener ref="consumer" queue-names="${queue.names}" />
</rabbit:listener-container>

More information in the Spring Docs and the javadoc for SimpleMessageListenerContainer.

like image 78
sherb Avatar answered Nov 09 '22 14:11

sherb