Review RabbitMQ implementation message latency

I. overview

          In RabbitMQ, by default, the message will not expire when the message expiration parameter is not set. Even if the message is not consumed, the message will always be stored in the queue.

Second queue message lifetime TTL

        TTL (time to live), the time that the message survives, that is, the validity period of the message. We can set TTL to meet the requirement of message survival time. When the message's lifetime exceeds the TTL and has not been consumed, the message will become a dead letter.

        TTL settings:

        1. When we declare a queue, set the message validity period in the attribute. In this case, all messages entering the queue will have the same validity period. (in this way, when the expiration time of the message queue is set, the expired message will be deleted, because there is a message queue after the message enters RabbitMQ, and the header of the queue is the earliest message. At this time, the queue only needs a scheduled task to scan whether there are expired messages from the header, and if there are, it will be deleted directly.)

        Set queue message expiration in configuration class:

@Bean
Queue queue() {
    Map<String, Object> args = new HashMap<>();
    args.put("x-message-ttl", 10000);
    return new Queue(QUEUE_DEMO, true, false, false, args);
}

        Message sending:

@RestController
public class TestController {
    @Autowired
    RabbitTemplate rabbitTemplate;

    @GetMapping("/test")
    public void test() {
        Message message = MessageBuilder.withBody("test queue".getBytes())
                .build();
        rabbitTemplate.convertAndSend(QueueConfig.QUEUE_DEMO, message);
    }
}

        effect:

        

          At this time, there are d (message persistence in message queue) and TTL (message expiration setting) information in the Features attribute in the queue.

         2. When we send a message, we set the validity period of the message, so that different messages will have different validity periods due to different settings. (different from setting the expiration time of the message queue, this method will not be deleted immediately after the message expires, but will be deleted only when the message is delivered to the consumer. Because the validity periods of different messages declared in this method are different, the queue message traversal is required to know which message expires, and the performance consumption is compared when there are many messages.)         

        Note: when both cases are set, the message will be valid for a shorter time.

III. configuration class of message queue

@Configuration
public class QueueConfig {

    //queue
    public static final String QUEUE_DEMO = "queue_demo";
    //Switch
    public static final String JEXCHANGE_DEMO = "exchange_demo";
    public static final String ROUTING_KEY = "routing_key";

    @Bean
    Queue queue() {
        return new Queue(QUEUE_DEMO, true, false, false);
    }

    @Bean
    DirectExchange directExchange() {
        return new DirectExchange(EXCHANGE_DEMO, true, false);
    }

    @Bean
    Binding binding() {
        return BindingBuilder.bind(queue())
                .to(directExchange())
                .with(ROUTING_KEY);
    }
}

        Configuration class analysis:

        1. Configure a message Queue and a new Queue:

            New queue (parameter 1: the name of the queue, parameter 2: whether the messages in the message queue are persistent, parameter 3: whether the message queue is exclusive (generally false), parameter 4: whether the queue is automatically deleted when there are no subscribed consumers in the message queue (applicable to temporary queues)).

        2. Configure the switch and new a DirectExchange.

        3. Bind the switch and queue together.

Four message sending interfaces in the queue

@RestController
public class TestController {
    @Autowired
    RabbitTemplate rabbitTemplate;

    @GetMapping("/test")
    public void test() {
        Message message = MessageBuilder.withBody("test queue".getBytes())
                .setExpiration("10000")
                .build();
        rabbitTemplate.convertAndSend(QueueConfig.QUEUE_DEMO, message);
    }
}

        Set the expiration time of 10 seconds when creating the message object.

  reference material: Will messages in RabbitMQ expire? (qq.com)https://mp.weixin.qq.com/s?__biz=MzI1NDY0MTkzNQ==&mid=2247494827&idx=1&sn=c7e6d24cc9ce7d21f3f450a41b565ddf&scene=21#wechat_redirect

Keywords: RabbitMQ Distribution MQ

Added by Genux on Mon, 25 Oct 2021 02:40:57 +0300