What is the message publish-subscribe model in RabbitMQ? How to achieve?

What is the message publish-subscribe model in RabbitMQ? How to achieve?

The message publish-subscribe pattern in RabbitMQ is a common messaging pattern used to broadcast messages to multiple consumers. In this mode, a producer sends messages to an exchange (Exchange), and the exchange broadcasts the message to all queues bound to it (Queue). Each queue has a consumer that receives messages and processes them.

The following is an example of using Java code to implement the RabbitMQ message publish-subscribe mode:

First, we need to create a connection factory and set the host address of the RabbitMQ server. Then, use the connection factory to create a connection, and use the connection to create a channel. Next, we declare a switch and set the switch type to fanout, indicating that messages will be broadcast to all queues bound to it.

// 创建连接工厂
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");

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

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

// 声明交换机
channel.exchangeDeclare("my_exchange", "fanout");

When declaring a switch, we need to specify the name and type of the switch. Here we set the name of the switch to my_exchangeand the type to fanout.

We can then basicPublishsend messages to the exchange by calling methods. When sending a message, we need to set the name of the switch to the name of the destination switch and routingKeyset the parameters to an empty string.

String message = "Hello, RabbitMQ!";
channel.basicPublish("my_exchange", "", null, message.getBytes());

In the above code, we set the name of the exchange to my_exchange, indicating that messages will be broadcast to all queues bound to it.

Next, we can create multiple queues and bind these queues to the switch. Each queue has a consumer that receives messages and processes them.

// 创建队列并绑定到交换机
String queueName1 = channel.queueDeclare().getQueue();
channel.queueBind(queueName1, "my_exchange", "");

String queueName2 = channel.queueDeclare().getQueue();
channel.queueBind(queueName2, "my_exchange", "");

In the above code, we use queueDeclarethe method to create an anonymous queue and get the name of the queue. We then queueBindbind the queue to the switch using the method, setting the name of the switch to my_exchangeand routingKeythe parameters to an empty string.

Finally, we can receive messages through consumers. In the consumer, we need to use basicConsumemethods to specify the queue and message processing logic to be consumed.

channel.basicConsume(queueName1, true, 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 from queue 1: " + message);
    }
});

channel.basicConsume(queueName2, true, 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 from queue 2: " + message);
    }
});

In the above code, we use basicConsumethe method to specify the queue to consume, set the name of the queue to queueName1and queueName2, and pass in a custom DefaultConsumerobject. In handleDeliverythe method we can handle the received message.

Through the above steps, we can implement the message publish-subscribe mode in RabbitMQ. The producer sends the message to the exchange, and the exchange broadcasts the message to all queues bound to it. Each queue has a consumer to receive and process the message.

It should be noted that the message in the message publish-subscribe mode is broadcast to all queues, so each queue will receive the same message. If you need to implement point-to-point delivery of messages, you can use RabbitMQ's message routing mode.

Guess you like

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