@RabbitListener turns a method into a RabbitMQ message consumer. Spring AMQP creates a listener container for it, connects to the broker, receives messages from the queues you name and calls the method with each converted message.
It is the standard annotation-driven way to consume messages in Spring Boot, replacing manual MessageListenerContainer and MessageListenerAdapter configuration.
Add the starter and point it at your broker:
Spring Boot enables listener processing automatically. In a plain Spring application you must add @EnableRabbit to a configuration class and declare a RabbitListenerContainerFactory.
The method runs on a container thread each time a message arrives. The queue must exist on the broker, or you must declare it. If it does not, the container fails to start consuming. Declare queues, exchanges and bindings as beans, and Spring AMQP's RabbitAdmin creates them on startup:
You can also declare everything on the annotation with bindings = @QueueBinding(...):
With Jackson on the classpath, declare a JSON message converter bean and Spring Boot applies it to the listener containers:
@Payload marks the body when you also take other parameters, and @Header reads message headers. You can also accept the raw org.springframework.amqp.core.Message.
One annotation can listen to several queues and run several consumers:
concurrency = "3" starts a fixed number of consumers. "3-10" starts three and scales up to ten under load. Messages from one queue can then be processed out of order, so use a single consumer when ordering matters. Boot also supports defaults such as spring.rabbitmq.listener.simple.concurrency and spring.rabbitmq.listener.simple.prefetch.
With the default AUTO acknowledge mode, Spring acknowledges a message when the method returns normally. If the method throws, the message is rejected and, by default, requeued. A message that always fails then loops forever. Pick one of these strategies:
AmqpRejectAndDontRequeueException to reject without requeue, and configure a dead-letter exchange on the queue so failed messages go somewhere you can inspect.The second false in basicNack means the message is not requeued. Delivery is at least once, so make processing idempotent.
If a listener method returns a value and the incoming message has a replyTo or you add @SendTo, Spring publishes the return value as a reply. This supports request-reply patterns, which pair with RabbitTemplate.convertSendAndReceive.
Only if a Queue bean (with RabbitAdmin, which Boot configures) or a @QueueBinding declares it. Otherwise the queue must already exist.
An exception was thrown and the message was requeued. Configure retry with a dead-letter queue, or throw AmqpRejectAndDontRequeueException.
Yes. Put it on the class and mark methods with @RabbitHandler; Spring chooses the method by payload type.
Set an id on the annotation and use RabbitListenerEndpointRegistry to get the container and call stop() or start().
@RabbitListener registers a method as a RabbitMQ consumer. Declare queues and bindings as beans or on the annotation, use a JSON converter for typed payloads, tune concurrency, and handle failures with retry, dead-letter queues or manual acknowledgements. Remember that the default behaviour requeues failed messages.