How can RabbitMQ consume messages efficiently

After the previous article introduced how to simply send a message queue, let's take a look at another mode of RabbitMQ, work queue.

What is work queue

What we said in the last article is that a producer produces a message that is consumed by a consumer, as shown in the following figure

The above simple message queue can indeed handle our tasks, but when there are too many tasks in our queue and it takes a long time to process each task, it is obviously not enough to use a consumer to process messages, so we can add consumers to share messages in the message queue for task processing.

That is to say, the following figure

Although I only spend one producer A in the picture above, so in the same way, there can be multiple consumers and multiple producers.

code

send message

public class Send {

    public static final String QUEUE_NAME = "test_word_queue";

    public static void main(String[] args) throws IOException, TimeoutException, InterruptedException {

        // Get connection
        Connection connection = MQConnectUtil.getConnection();

        // Create channel
        Channel channel = connection.createChannel();

        // Declaration queue
        channel.queueDeclare(QUEUE_NAME, false, false, false, null);

        // Simulate sending 20 messages
        for (int i = 0; i < 20; i++) {

            String msg = "Message:" + i;

            channel.basicPublish("", QUEUE_NAME, null, msg.getBytes());

            Thread.sleep(i * 20);

            System.out.println(msg);
        }

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

Consumer A

public class Consumer1 {

    public static final String QUEUE_NAME = "test_word_queue";

    public static void main(String[] args) throws Exception {

        // Get connection
        Connection connection = MQConnectUtil.getConnection();

        // Create channel
        Channel channel = connection.createChannel();

        // Queue declaration
        channel.queueDeclare(QUEUE_NAME, false, false, false, null);

        // Defining consumers
        DefaultConsumer consumer = new DefaultConsumer(channel) {
            @SneakyThrows
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) {
                String msg = new String(body, StandardCharsets.UTF_8);

                System.out.println("consumer[A]-Content:" + msg);

                Thread.sleep(2 * 1000);
            }
        };

        // listen queue 
        channel.basicConsume(QUEUE_NAME, true, consumer);
    }
}

Consumer B

public class Consumer2 {

    public static final String QUEUE_NAME = "test_word_queue";

    public static void main(String[] args) throws Exception {

        // Get connection
        Connection connection = MQConnectUtil.getConnection();

        // Create channel
        Channel channel = connection.createChannel();

        // Queue declaration
        channel.queueDeclare(QUEUE_NAME, false, false, false, null);

        // Defining consumers
        DefaultConsumer consumer = new DefaultConsumer(channel) {
            @SneakyThrows
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) {
                String msg = new String(body, StandardCharsets.UTF_8);

                System.out.println("consumer[B]-Content:" + msg);

                Thread.sleep(1000);
            }
        };

        // listen queue 
        channel.basicConsume(QUEUE_NAME, true, consumer);
    }
}

Let's look at the consumption of consumer A and consumer B

  • Consumer B

  • Consumer A

Have you found any problems? I always sent 20 messages through simulation. Careful students can find that consumer A and consumer B consume the same amount of messages for 10 days, but in consumer A and consumer B, what sleep does not work, It is reasonable to say that consumer B processes more messages than consumer A, so why does this happen?

Default configuration of RabbitMQ work queues

By default, RabbitMQ will send each message to the next consumer in turn, and the number of messages received by each consumer is actually the same. We call this way of distributing messages as round training distribution mode.

In this article, we will briefly introduce so many contents. Children's shoes that are willing to learn must knock on the code. You can't read them if you want to. You can understand them only if you knock them once.

< P style = "text align: Center; font weight: bold; color: ා0e88eb; font size: 20px" > the sun arch is the only one who has contributed a lot</p>

For more information, please pay attention to:

Tags: Programming RabbitMQ

Posted on Fri, 15 May 2020 05:29:02 -0400 by macattack