RabbitMQ switch mode

Before getting to the point, explain that switch mode is a general term. It is not a separate mode (including subscription mode, routing mode and topic mode). Switch mode is a more commonly used mode, mainly for data synchronization.

First of all, the subscription mode is almost the same as the literal meaning. It means that one producer, multiple consumers, and the same message is obtained by multiple consumers. First, look at the figure on the official website

 

The overall execution process is the same as in the figure. The producer sends the message to the switch, and then the queue is bound to the switch. The message is sent to the queue by the switch. Each queue has its own consumer. such

A message is obtained by multiple consumers, and if a new consumer joins the queue and binds to the switch directly, the coupling between the systems is greatly reduced. One more thing to note is that

When we send the message to a switch without queue binding, the message will be lost, because the message can only be stored in the queue, and the switch only exchanges, not stores!

Producer Code:

public class Send {

    private final static String EXCHANGE_NAME = "exchange_name"; //Switch name

    public static void main(String[] argv) throws Exception {
        // Get MQ connections and channels
        Connection connection = ConnectionUtil.getConnection();
        Channel channel = connection.createChannel();

        // Claim switch
        channel.exchangeDeclare(EXCHANGE_NAME, "fanout");

        // Message content
        String message = "Producer message";
        channel.basicPublish(EXCHANGE_NAME, "", null, message.getBytes());
        System.out.println(" Send out '" + message + "'");

        channel.close();
        connection.close();
    }
}

 

 

Consumer one code:

public class Recv {

    private final static String QUEUE_NAME = "test_queue_ex";

    private final static String EXCHANGE_NAME = "exchange_name";

    public static void main(String[] argv) throws Exception {
        Connection connection = ConnectionUtil.getConnection();
        Channel channel = connection.createChannel();
        // Declaration queue
        channel.queueDeclare(QUEUE_NAME, false, false, false, null);
        // Bind queue to switch
        channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "");
        channel.basicQos(1);
        // Define consumers for the queue
        QueueingConsumer consumer = new QueueingConsumer(channel);
        // Listen to the queue and return to finish manually
        channel.basicConsume(QUEUE_NAME, false, consumer);
        // Get message
        while (true) {
            QueueingConsumer.Delivery delivery = consumer.nextDelivery();
            String message = new String(delivery.getBody());
            System.out.println(" Consumer one '" + message + "'");
            Thread.sleep(10);

            channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
        }
    }
}

Consumer 2 Code:

public class Recv2 {

    private final static String QUEUE_NAME = "test_queue_ex2";

    private final static String EXCHANGE_NAME = "exchange_name";

    public static void main(String[] argv) throws Exception {
        Connection connection = ConnectionUtil.getConnection();
        Channel channel = connection.createChannel();
        channel.queueDeclare(QUEUE_NAME, false, false, false, null);
        channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "");
        channel.basicQos(1);
        QueueingConsumer consumer = new QueueingConsumer(channel);
        channel.basicConsume(QUEUE_NAME, false, consumer);
        while (true) {
            QueueingConsumer.Delivery delivery = consumer.nextDelivery();
            String message = new String(delivery.getBody());
            System.out.println("Consumer 2 '" + message + "'");
            Thread.sleep(10);
            channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
        }
    }
}

After running the code, you can see

 

The message sent by the producer has been stored in the switch. See the following figure for binding relationship:

 

Therefore, it can be concluded that a message is consumed by multiple consumers. There are some defects in subscription mode. Sometimes not all data need to be synchronized, so it is not reasonable to use subscription mode to synchronize data. So the routing mode is used.

 

Routing mode

The official website is as follows:

 

Similar to the subscription mode, but the type becomes the direct type, and the routing mode first sends messages to the switch by the producer, and then judges which switch the messages are sent to according to the binding key. The following picture:

 

The difference between subscription mode and producer mode is that the producer should declare the message type before sending the message, that is, which kind of consumer will get the message

 

 

When consumers and producers keep the same type, they can receive messages sent by the corresponding producers. This allows you to filter out unwanted message types.

Thematic mode

The personal feeling of theme mode is the same as that of like keyword in sql statement. It does not need to guarantee the same message type, as long as it is similar, messages can be received. Compared with routing mode,

The topic pattern matching rate is relatively low, but the function has improved a lot, reducing the creation of routing key, as shown in the figure:

 

Type becomes topic type. As for other aspects, it's the same as routing mode.

Tags: Java SQL

Posted on Sun, 10 Nov 2019 08:41:42 -0500 by kausee