What is RabbitMQ? What is its main function?

What is RabbitMQ? What is its main function?

RabbitMQ is an open source message broker software that implements the Advanced Message Queuing Protocol (AMQP) for reliable asynchronous communication between distributed systems. It can pass messages between different applications, services and systems and ensure the reliability and sequence of messages.

The main features of RabbitMQ include:

  1. Message queue: RabbitMQ delivers messages from one application to another through message queue. Messages are sent to the queue and can be consumed according to specific rules.

  2. Publish-Subscribe mode: RabbitMQ supports the publish-subscribe mode, in which a producer can publish messages to an exchange (Exchange), and then multiple consumers can subscribe to the exchange and receive corresponding messages.

  3. Routing and filtering: RabbitMQ provides a flexible routing and filtering mechanism, which can route messages to different queues based on the routing key of the message (Routing Key), and filter based on the attributes of the message.

  4. Message confirmation mechanism: RabbitMQ supports message confirmation mechanism. The producer can wait for the consumer's confirmation after sending the message to ensure that the message is successfully processed.

  5. Message persistence: RabbitMQ can persist messages to disk to prevent messages from being lost in the event of system failure.

The following is a code example written in Java that demonstrates how to use RabbitMQ to send and receive messages:

First, we need to add the RabbitMQ Java client library to the project's dependencies. In the Maven project, you can add the following dependencies in the pom.xml file:

<dependency>
    <groupId>com.rabbitmq</groupId>
    <artifactId>amqp-client</artifactId>
    <version>5.12.0</version>
</dependency>

We can then write the code to send the message:

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class SendMessage {
    
    

    private final static String QUEUE_NAME = "hello";

    public static void main(String[] args) throws IOException, TimeoutException {
    
    
        // 创建连接工厂
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("localhost");

        // 创建连接
        Connection connection = factory.newConnection();

        // 创建通道
        Channel channel = connection.createChannel();

        // 声明队列
        channel.queueDeclare(QUEUE_NAME, false, false, false, null);

        // 发送消息
        String message = "Hello, RabbitMQ!";
        channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
        System.out.println("Sent message: " + message);

        // 关闭通道和连接
        channel.close();
        connection.close();
    }
}

Next, we write the code to receive the message:

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class ReceiveMessage {
    
    

    private final static String QUEUE_NAME = "hello";

    public static void main(String[] args) throws IOException, TimeoutException {
    
    
        // 创建连接工厂
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("localhost");

        // 创建连接
        Connection connection = factory.newConnection();

        // 创建通道
        Channel channel = connection.createChannel();

        // 声明队列
        channel.queueDeclare(QUEUE_NAME, false, false, false, null);

        // 创建消费者
        Consumer consumer = new DefaultConsumer(channel) {
    
    
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
    
    
                String message = new String(body, "UTF-8");
                System.out.println("Received message: " + message);
            }
        };

        // 监听队列
        channel.basicConsume(QUEUE_NAME, true, consumer);
    }
}

The above code demonstrates a simple example of sending and receiving messages. First, we ConnectionFactorycreate a connection factory through the class and set the host address of the RabbitMQ server. We then create a connection using a connection factory and a channel using the connection. When sending a message, we use basicPublisha method to send the message to the queue. When receiving a message, we create a consumer and use basicConsumemethods to listen to the queue and process the message when it is received.

Guess you like

Origin blog.csdn.net/qq_51447496/article/details/132889706