【RabbitMQ】08 深入部分P2 消费限流 & TTL

发布时间:2022-07-03 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了【RabbitMQ】08 深入部分P2 消费限流 & TTL脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

 

 

1、消费限流设置

【RabbitMQ】08 深入部分P2 消费限流 & TTL

 就是设置项的2个调整,当然还有前面的手动确认的监听改动处理

https://www.bilibili.COM/video/BV15k4y1k7Ep?p=26

 

2、消息过时设置 TTL(Time To Live)

https://www.bilibili.com/video/BV15k4y1k7Ep?p=27

RabbITMQ可以设置消息的存活时间,同样也能设置队列的存活时间

【RabbitMQ】08 深入部分P2 消费限流 & TTL

新建一个10秒存活消息的队列:

【RabbitMQ】08 深入部分P2 消费限流 & TTL

再新建一个Topic交换机:

【RabbitMQ】08 深入部分P2 消费限流 & TTL

在交换机设置中绑定队列:

【RabbitMQ】08 深入部分P2 消费限流 & TTL

 

现在发布一条消息,如果10秒内没有消费者服务来接收消息,则RabbitMQ10秒后销毁消息

【RabbitMQ】08 深入部分P2 消费限流 & TTL

队列列表盯着这个队列不停刷新等到10秒,这条消息就会被移除了

【RabbitMQ】08 深入部分P2 消费限流 & TTL

 

SPRing&Java代码配置:

<?XMl version="1.0" encoding="UTF-8"?>
<beans xMLns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:rabbit="http://www.springframework.org/schema/rabbit"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       https://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/rabbit
       http://www.springframework.org/schema/rabbit/spring-rabbit.xsd">
    <!--加载配置文件-->
    <context:proPErty-placeholder location="classpath:rabbitmq.properties"/>

    <!-- 定义rabbitmq connectionFactory

        publisher-confirms="true" 消息发送可确认
        publisher-returns="true"
    -->
    <rabbit:connection-factory id="connectionFactory" host="${rabbitmq.host}"
                               port="${rabbitmq.port}"
                               username="${rabbitmq.username}"
                               password="${rabbitmq.password}"
                               virtual-host="${rabbitmq.virtual-host}"
                               publisher-confirms="true"
                               publisher-returns="true"
    />
    <!--定义管理交换机、队列-->
    <rabbit:admin connection-factory="connectionFactory"/>

    <!--定义rabbitTemplate对象操作可以在代码中方便发送消息-->
    <rabbit:template id="rabbitTemplate" connection-factory="connectionFactory"/>

    <rabbit:queue id="ttl-queue" name="confirm-test-queue" >
        <rabbit:queue-arguments>
            <!--  过期时间设置10秒 注意这里要指定类型数字 -->
            <entry key="x-message-ttl" value="10000" value-type="java.lang.Integer" />
        </rabbit:queue-arguments>
    </rabbit:queue>
    <!-- 交换机设置-->
    <rabbit:topic-exchange name="ttl-exchange" >
        <rabbit:bindings>
            <rabbit:binding queue="ttl-queue" pattern="ttl.#"  />
        </rabbit:bindings>
    </rabbit:topic-exchange>
</beans>

测试代码:

    /**
     * 消息存活设置
     */
    @Test
    public void ttlTest() {
        for (int i = 0; i < 10; i++) {
            rabbitTemplate.convertAndSend("ttl-exchange", "ttl.info", "hello ttl timeout test ....");
        }
    }

队列经过10秒就会销毁这些消息:

【RabbitMQ】08 深入部分P2 消费限流 & TTL

 如果设置消息的TTL

    /**
     * 消息存活设置
     */
    @Test
    public void ttlTest() {
//        for (int i = 0; i < 10; i++) {
//            rabbitTemplate.convertAndSend("ttl-exchange", "ttl.info", "hello ttl timeout test ....");
//        }

        rabbitTemplate.convertAndSend(
                "ttl-exchange",
                "ttl.info",
                "hello ttl timeout test ....",
                message -> {
                    message.getMessageProperties().setExpiration("60000"); // 消息的过期时间 字符串设置  这个时间不能超过队列销毁的时间
                    return message;
                });
    }

队列时限 和 消息时限 的规则就是,取时间短的生效

队列过期 销毁队列内的所有消息

消息过期 只有消息在队列顶端处被判断是否过期,才会被销毁(因为案例只写了一个,直接推到顶端销毁了)

 

 

 

 

 

 

 

 

 

脚本宝典总结

以上是脚本宝典为你收集整理的【RabbitMQ】08 深入部分P2 消费限流 & TTL全部内容,希望文章能够帮你解决【RabbitMQ】08 深入部分P2 消费限流 & TTL所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。