RabbitMQ的高级特性(三)-----TTL

TTL(Time To Live):存活时间/过期时间。

  • 当消息到达存活时间后,还没有被消费,会被自动清除。
  • RabbitMQ可以对消息设置过期时间,也可以对整个队列(Queue)设置过期时间。
  • 如果设置了消息的过期时间,也设置了队列的过期时间,它以时间短的为准。
  • 如果对交换机,队列做了修改。需要判断是否在mq中存在,存在先删再加,不存在直接加。否则会报错。io异常
    image
  • 队列统一过期-控制后台演示:
    1. 增加队列,设置过期时间参数
      image
    2. 增加交换机
      image
    3. 绑定交换机和队列,设置路由key。
      image
    4. 发送消息:Delivery mode:2-Persistent表示需要进行持久化
      image
    5. 查看消息,可以看到消息,但十秒之后,消息自动消失,因为我们设置了十秒消息过期
      image
  • 队列统一过期-代码演示:
    1. spring-rabbitmq-producer.xml配置
      <?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 -->
      	<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="test_queue_confirm" name="test_queue_confirm"></rabbit:queue>
      	<rabbit:direct-exchange name="test_exchange_confirm">
      		<rabbit:bindings>
      			<rabbit:binding queue="test_queue_confirm" key="confirm"></rabbit:binding>
      		</rabbit:bindings>
      	</rabbit:direct-exchange>
      
      	<!--TTL 队列-->
      	<rabbit:queue name="test_queue_ttl" id="test_queue_ttl">
      		<!--设置queue的参数-->
      		 <rabbit:queue-arguments>
      			 <!--
      			   设置x-message-ttl队列的过期时间
      			   默认情况下value-type的类型是String类型,但时间的类型是number类型,所以需要设置成integer类型
      			 -->
      			 <entry key="x-message-ttl" value="10000" value-type="java.lang.Integer"></entry>
      		 </rabbit:queue-arguments>
      	</rabbit:queue>
      
      	<!--设置交换机-->
      	<rabbit:topic-exchange name="test_exchange_ttl">
      		<!--交换机绑定队列-->
      		<rabbit:bindings>
      			<rabbit:binding pattern="ttl.#" queue="test_queue_ttl"></rabbit:binding>
      		</rabbit:bindings>
      	</rabbit:topic-exchange>
      
      </beans>
      
    2. 发送消息
      @Test
      public void testTTL() {
      	 for (int i = 0; i < 10; i++) {
      	   rabbitTemplate.convertAndSend("test_exchange_ttl","ttl.hehe","message ttl");
      	 }
      }
      
    3. 查看控制台,发现有10条消息,十秒之后自动过期
  • 消息过期-代码演示:
    /**
     * TTL:过期时间
     *  1. 队列统一过期
     *  2. 消息单独过期
     *  如果设置了消息的过期时间,也设置了队列的过期时间,它以时间短的为准。
     */
    @Test
    public void testMessageTtl() {
    	// 消息后处理对象,设置一些消息的参数信息
    	MessagePostProcessor messagePostProcessor = new MessagePostProcessor() {
    
    		@Override
    		public Message postProcessMessage(Message message) throws AmqpException {
    			//1.设置message的信息
    			// 第二个方法:消息的过期时间 ,5秒之后过期
    			message.getMessageProperties().setExpiration("5000");
    			//2.返回该消息
    			return message;
    		}
    	};
    
    	//消息单独过期
    	rabbitTemplate.convertAndSend("test_exchange_ttl","ttl.hehe","message ttl....",messagePostProcessor);
    }
    
posted @ 2021-11-15 19:39  lq-12040  阅读(49)  评论(0编辑  收藏  举报