[SpringBoot] Integrate RabbitMQ consumer current limit

consumer side

Directory Structure

import dependencies

Modify yml

Business logic


         Consumer current limit is used to limit the number of messages each time consumers get messages. Note that the premise is manual confirmation mode. And the message can only be obtained after manual confirmation.

consumer side

Directory Structure

import dependencies

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-amqp</artifactId>
        <version>2.5.0</version>
    </dependency>
</dependencies>

Modify yml

spring:
  rabbitmq:
    host: localhost
    port: 5672
    username: guest
    password: guest
    listener:
      simple:
        acknowledge-mode: manual # 手动确认模式
        prefetch: 1 # 每次消费仅1条消息

Business logic

        In order to verify whether manual confirmation is necessary to actually consume messages, I conducted a test as follows: First, I asked the producer to produce two messages in the queue, as shown in Figure 1 below. Next, look at the code logic. When the consumer code is started, it will appear as shown in Figure 2 below: Although the current is indeed limited and there is an unconfirmed message, when we close the application on the consumer side, it will become as shown in Figure 1 again.

                                                                figure 1

/**
 * 消费者的限流机制
 *  1、确保Ack机制为手动机制:acknowledge-mode: manual
 *  2、每次消费消息的个数:prefetch: 1 只有手动确认完后才会拉取下一条消息
 */
@Component
public class QosListener implements ChannelAwareMessageListener {

    @RabbitListener(queues = "test_queue_name")
    @Override
    public void onMessage(Message message, Channel channel) throws Exception {
        System.out.println("消费者接受的消息为:" + new String(message.getBody()));
    }
}

                                                                 figure 2

        So when we change the business logic again: after manual confirmation, we can find that the message has indeed been consumed, as shown in Figure 3. Pay attention: the second parameter whether to sign in batches indicates whether the message will only be read once after the consumer is turned on , and the consumer's current-limiting prefetch indicates that only one message can be read each time . The concepts of the two are different.

/**
 * 消费者的限流机制
 *  1、确保Ack机制为手动机制:acknowledge-mode: manual
 *  2、每次消费消息的个数:prefetch: 1 只有手动确认完后才会拉取下一条消息
 */
@Component
public class QosListener implements ChannelAwareMessageListener {

    @RabbitListener(queues = "test_queue_name")
    @Override
    public void onMessage(Message message, Channel channel) throws Exception {
        Thread.sleep(5000);
        long deliveryTag = message.getMessageProperties().getDeliveryTag();// 消息的唯一标识id
        System.out.println("消费者接受的消息为:" + new String(message.getBody()));
        channel.basicAck(deliveryTag,true);//每5s读一次消息(限流后每次为一条)
    }
}

Guess you like

Origin blog.csdn.net/m0_65563175/article/details/130463379