SoFunction
Updated on 2025-03-06

SpringBoot implements four ways to listen to messages in RabbitMQ

Spring Boot implements RabbitMQ to listen to messages in the following ways:

  • @RabbitListener Annotation method: By marking the method@RabbitListenerAnnotation is used to listen to the specified queue. When the message arrives, the annotated method will be called.

  • MessageListener interface method: Implementing the provided by Spring AMQPMessageListenerInterface, write a custom message listener, and then useSimpleMessageListenerContainerSet up a message listener.

  • MessageListenerAdapter Adapter Adapter Method:passMessageListenerAdapterClass, which can convert ordinary Java methods into message listeners, thereby simplifying message processing logic.

  • @RabbitHandler Annotation method: Combined@RabbitListenerand@RabbitHandlerAnnotation: Multiple types of messages can be used to process processing methods, and corresponding processing methods can be called according to different messages content.

These methods can easily implement RabbitMQ's message listening function in Spring Boot project.

1. @RabbitListener annotation method

The following is used@RabbitListenerSample code for implementing RabbitMQ message monitoring using annotation method:

import ;
import ;

@Component
public class RabbitMQListener {

    @RabbitListener(queues = "myQueue")
    public void handleMessage(String message) {
        ("Received message: " + message);
        // Process the received messages here    }
}

In this example, we create a name calledRabbitMQListenerclass and use@RabbitListenerAnnotation markhandleMessageMethod, specify the listening queue named "myQueue". When the message reaches the queue,handleMessageThe method will be called to process the received message.

2. MessageListener interface method

The following is usedMessageListenerSample code for implementing RabbitMQ message monitoring using interface method:

import ;
import ;
import ;

public class MyMessageListener implements MessageListener {

    @Override
    public void onMessage(Message message) {
        ("Received message: " + new String(()));
        // Process the received messages here    }

    public static void main(String[] args) {
        SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
        (connectionFactory); // Set up the connection factory        ("myQueue"); // Set the listening queue name        (new MyMessageListener()); // Set up message listener        (); // Start the monitor container    }
}

In this example, we created an implementationMessageListenerInterfaceMyMessageListenerClass to process received messages. Then, we useSimpleMessageListenerContainerSet up the connection factory, queue name and message listener, and start the listening container. When the message arrives,onMessageThe method will be called to process the received message.

3. MessageListenerAdapter Adapter Adapter

The following is usedMessageListenerAdapterSample code for implementing RabbitMQ message monitoring using adapter method:

import ;
import ;
import ;
import ;
import ;

public class MyMessageListener {

    public void handleMessage(String message) {
        ("Received message: " + message);
        // Process the received messages here    }

    public static void main(String[] args) {
        SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
        (connectionFactory); // Set up the connection factory        ("myQueue"); // Set the listening queue name
        MessageListenerAdapter adapter = new MessageListenerAdapter(new MyMessageListener());
        ("handleMessage"); // Set the default processing method        MessageConverter converter = new SimpleMessageConverter();
        (converter); // Set up the message converter
        (adapter); // Set up the message listening adapter        (); // Start the monitor container    }
}

In this example, we create aMyMessageListenerclass, and defines a method to process messageshandleMessage. Then, we useMessageListenerAdapterClass willMyMessageListenerThe methods in the class are converted into message listeners and set the default processing method. Finally, set the message listening adapter toSimpleMessageListenerContainer, and start the listening container to process the received message.

4. @RabbitHandler annotation method

use@RabbitHandlerCombination of annotation methods@RabbitListenerAnnotation The example code for implementing multiple different types of messages is as follows:

import ;
import ;
import ;

@Component
@RabbitListener(queues = "myQueue")
public class MyMessageHandler {

    @RabbitHandler
    public void handleStringMessage(String message) {
        ("Received string message: " + message);
        // Process string type messages    }

    @RabbitHandler
    public void handleIntMessage(Integer message) {
        ("Received integer message: " + message);
        // Process integer type messages    }
}

In this example, we create a name calledMyMessageHandlerclass and use@RabbitListenerAnnotation marks the entire class and specifies the listening queue named "myQueue". Then, we define two methods for processing messages in the class, using@RabbitHandlerAnnotation marks, processing messages of string type and integer type respectively. When different types of messages arrive at the queue, the corresponding processing method is called according to the message type to process the message.

This is the end of this article about the four ways SpringBoot implements RabbitMQ monitoring messages. For more related SpringBoot RabbitMQ monitoring content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!