1-8 (5). RabbitMQ高级特性-消费端限流和消息过期时间

1.消费端限流

具体流程如下:
(1)设置手动确认机制;
(2)设置prefetch=n,n即为消费端一次从队列拿取n条消息,直到n条消息消费成功,下次才再拿n条

# 配置RabbitMQ的基本信息
spring:
  rabbitmq:
    host: 127.0.0.1
    port: 5672
    username: guest
    password: guest
    virtual-host: /
    #设置手动确认机制
    listener:
      simple:
        acknowledge-mode: manual
        prefetch: 2
      direct:
        acknowledge-mode: manual
        prefetch: 2

2.TTL

time to live(存活时间)
设置分为两种方式
(1)队列所有消息统一过期,在
1-8 (1). SpringBoot整合RabbitMQ-消息生产者
基础上,创建队列时,设置queue的ttl

package cn.sysu.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitMQComfig {

    public static final String EXCHANGE_NAME = "ming-topic-exchange";
    public static final String QUEUE_NAME1 = "ming-queue1";
    public static final String QUEUE_NAME2 = "ming-queue2";
    public static final String QUEUE_NAME3 = "ming-queue-ttl";

    //1.创建交换机
    @Bean("topic-exchange")
    public Exchange createExchange(){
        return ExchangeBuilder.topicExchange(EXCHANGE_NAME).durable(true).build();
    }

    //2.创建交队列
    @Bean("queue1")
    public Queue createQueue1(){
        return QueueBuilder.durable(QUEUE_NAME1).build();
    }
    @Bean("queue2")
    public Queue createQueue2(){
        return QueueBuilder.durable(QUEUE_NAME2).build();
    }
    @Bean("queue3")
    public Queue createQueue3(){
        return QueueBuilder.durable(QUEUE_NAME3).ttl(10000).build();
    }

    //3.队列与交换机的关系 Binding
    @Bean
    public Binding bindQueueAndExchange1(@Qualifier("queue1") Queue queue,@Qualifier("topic-exchange") Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with("test1.#").noargs();
    }

    @Bean
    public Binding bindQueueAndExchange2(@Qualifier("queue2") Queue queue,@Qualifier("topic-exchange") Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with("test2.#").noargs();
    }

    @Bean
    public Binding bindQueueAndExchange3(@Qualifier("queue3") Queue queue,@Qualifier("topic-exchange") Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with("test3.#").noargs();
    }

}

测试结果
1-8 (5). RabbitMQ高级特性-消费端限流和消息过期时间_第1张图片10秒后
1-8 (5). RabbitMQ高级特性-消费端限流和消息过期时间_第2张图片

(2)某一条消息的过期时间
队列统一过期其实不怎么友好,有些消息重要,有些不太重要,因此针对某些消息过期设置过期时间
主要通过MessagePostProcessor实现

测试类编写:

package cn.sysu;

import cn.sysu.config.RabbitMQComfig;
import org.junit.jupiter.api.Test;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
public class ProducerApplicationTest {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Test
    void contextLoads() {

        rabbitTemplate.convertAndSend(RabbitMQComfig.EXCHANGE_NAME,"test1.hello","test-message1");
        rabbitTemplate.convertAndSend(RabbitMQComfig.EXCHANGE_NAME,"test2.word","test-message2");
    }

    @Test
    void testConfirm() {
        rabbitTemplate.setConfirmCallback(new RabbitTemplate.ConfirmCallback() {
            @Override
            public void confirm(CorrelationData correlationData, boolean b, String s) {
                System.out.println("消息投递后执行confirm方法");
                System.out.println(correlationData);
                System.out.println(b);
                System.out.println(s);
            }
        });
        rabbitTemplate.convertAndSend(RabbitMQComfig.EXCHANGE_NAME,"test1.hello","test-confirm");
    }

    @Test
    void testReturn() {
        //设置交换机处理失败消息的模式
        rabbitTemplate.setMandatory(true);

        rabbitTemplate.setReturnCallback(new RabbitTemplate.ReturnCallback() {
            @Override
            public void returnedMessage(Message message, int i, String s, String s1, String s2) {
                System.out.println("return方法执行了");
            }
        });
        rabbitTemplate.convertAndSend(RabbitMQComfig.EXCHANGE_NAME,"testReturn.ming","test-return");
    }


    //测试队列消息统一过期
    @Test
    void testTTL1() {
        rabbitTemplate.setMandatory(true);

        rabbitTemplate.setReturnCallback(new RabbitTemplate.ReturnCallback() {
            @Override
            public void returnedMessage(Message message, int i, String s, String s1, String s2) {
                System.out.println("return方法执行了");
            }
        });
        rabbitTemplate.convertAndSend(RabbitMQComfig.EXCHANGE_NAME,"test3.ttl","test-ttl");
    }

    //测试消息单独过期
    @Test
    void testTTL2() {

        MessagePostProcessor messagePostProcessor = new MessagePostProcessor() {
            @Override
            public Message postProcessMessage(Message message) throws AmqpException {
                //设置消息5秒后过期
                message.getMessageProperties().setExpiration("5000");
                return message;
            }
        };
        rabbitTemplate.convertAndSend(RabbitMQComfig.EXCHANGE_NAME,"test2.ttl2","test-ttl2",messagePostProcessor);
    }

}

1-8 (5). RabbitMQ高级特性-消费端限流和消息过期时间_第3张图片

5秒后
1-8 (5). RabbitMQ高级特性-消费端限流和消息过期时间_第4张图片
注意:
(1)如果两种方式都采用后,消息将以过期时间短的为准;
(2)单独消息过期,必须位于队列的首端才会判断是否过期

博主的坚持 离不开大家评论和点赞,感谢大家支持!!!

你可能感兴趣的:(1.,SpringBoot,RabbitMQ,spring,boot,rabbitmq)