The @RabbitListener annotation lets your Spring Boot app listen to messages from RabbitMQ queues automatically. It helps your app react when new messages arrive.
@RabbitListener for consuming in Spring Boot
import org.springframework.amqp.rabbit.annotation.RabbitListener; import org.springframework.stereotype.Component; @Component public class MessageListener { @RabbitListener(queues = "queueName") public void receiveMessage(String message) { // process the message } }
The @RabbitListener annotation marks a method to receive messages from the specified queue.
The method parameter usually matches the message payload type, like String or a custom object.
ordersQueue and prints the order message.@RabbitListener(queues = "ordersQueue") public void receiveOrder(String order) { System.out.println("Order received: " + order); }
@RabbitListener(queues = "emptyQueue") public void receiveEmptyQueue(String message) { System.out.println("Message from emptyQueue: " + message); }
@RabbitListener(queues = "singleMessageQueue") public void receiveSingleMessage(String message) { System.out.println("Single message: " + message); }
@RabbitListener(queues = "endQueue") public void receiveEndQueue(String message) { System.out.println("Last message in queue: " + message); }
This Spring Boot app listens to the RabbitMQ queue named testQueue. When a message arrives, it prints it to the console.
package com.example.rabbitlistenerdemo; import org.springframework.amqp.rabbit.annotation.EnableRabbit; import org.springframework.amqp.rabbit.annotation.RabbitListener; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.stereotype.Component; @SpringBootApplication @EnableRabbit public class RabbitListenerDemoApplication { public static void main(String[] args) { SpringApplication.run(RabbitListenerDemoApplication.class, args); } } @Component class SimpleMessageListener { @RabbitListener(queues = "testQueue") public void receiveMessage(String message) { System.out.println("Received message: " + message); } }
Time complexity: Receiving a message is O(1) because it processes one message at a time.
Space complexity: Minimal, only stores the current message being processed.
Common mistake: Forgetting to enable Rabbit support with @EnableRabbit or missing queue configuration.
Use @RabbitListener when you want automatic, event-driven message consumption instead of manual polling.
@RabbitListener makes your app listen to RabbitMQ queues easily.
It runs the annotated method whenever a new message arrives.
Remember to enable Rabbit support and configure queues properly.
Practice
@RabbitListener annotation in a Spring Boot application?Solution
Step 1: Understand the role of @RabbitListener
The annotation is used to mark methods that should be triggered when a message arrives on a RabbitMQ queue.Step 2: Differentiate from other RabbitMQ tasks
Sending messages or configuring connections are done elsewhere, not by @RabbitListener.Final Answer:
To mark a method that listens for messages from a RabbitMQ queue -> Option DQuick Check:
@RabbitListener listens to queues = D [OK]
- Confusing listener with sender
- Thinking it configures connection
- Assuming it creates queues automatically
@RabbitListener to listen to a queue named "orders"?Solution
Step 1: Check the correct attribute name
The attribute to specify queues isqueues, notqueueorlistenTo.Step 2: Understand the attribute type
Thequeuesattribute expects an array of strings, so curly braces are needed even for one queue.Final Answer:
@RabbitListener(queues = {"orders"}) -> Option AQuick Check:
queues attribute needs array syntax = B [OK]
- Forgetting quotes around queue name
- Using singular 'queue' instead of 'queues'
- Using wrong attribute names
@RabbitListener(queues = {"greetings"})
public void receiveMessage(String msg) {
System.out.println("Received: " + msg);
}Solution
Step 1: Understand @RabbitListener behavior
The method is annotated correctly to listen to the "greetings" queue and takes a String parameter for the message.Step 2: Analyze method output
When a message arrives, it prints "Received: " plus the message content.Final Answer:
The message "Hello" will be printed as "Received: Hello" -> Option AQuick Check:
Listener prints message content = C [OK]
- Thinking method needs a return type
- Assuming annotation is missing
- Confusing listener with sender
@RabbitListener:@RabbitListener(queue = "tasks")
public void processTask(String task) {
System.out.println(task);
}Solution
Step 1: Check the @RabbitListener attribute syntax
The correct attribute name isqueues(plural), notqueue.Step 2: Validate method signature and annotation
The method can have void return type and String parameter is valid for message content.Final Answer:
The attribute should be 'queues' (plural), not 'queue' -> Option CQuick Check:
attribute name must be queues = A [OK]
- Using singular 'queue' instead of 'queues'
- Expecting return value from listener method
- Confusing @RabbitListener with @RabbitHandler
@RabbitListener is correct?Solution
Step 1: Understand multiple queue listening
You can listen to multiple queues by annotating separate methods for each queue to handle messages differently.Step 2: Evaluate other options
Using one method for multiple queues (B) processes messages from both the same way. The syntax in C is invalid, and D ignores @RabbitListener purpose.Final Answer:
Use two methods each annotated with @RabbitListener(queues = {"queueA"}) and @RabbitListener(queues = {"queueB"}) respectively -> Option BQuick Check:
Separate methods for separate queues = A [OK]
- Using one method for multiple queues without differentiation
- Combining queue names in one string without array
- Ignoring proper annotation usage
Ready to prove it?
Pass the test and this pattern moves unknown
