Rabbitmq-TTL过期时间

Rabbitmq高级-TTL过期时间

1、任意类型的交换机都可以作为死信队列,这里用的是direct路由模式。

2、过期队列:过期队列中的消息是可以转存到死信队列中的。

3、过期信息:队列不是过期队列,只是在队列中给消息设置了过期时间,此时的消息过期之后直接清除,过期信息不能写入死信队列中。

4、如果一个交换机绑定了多个队列,并且都设置了不同的过期时间时,此时的过期时间以最短的时间为准。

设置过期队列

消息发送者

package com.zhang.rabbitmq.springbootrabbitmqproducer.service;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.UUID;

/**
* ttl过期时间
*/
@Component
public class TTLDirectService {
   @Autowired
   private RabbitTemplate rabbitTemplate;

   public void TTLMorikOrder(String userId, String producerId, Integer number){
       String orderId = UUID.randomUUID().toString();
       System.out.println("订单id为:" + orderId);
       //定义交换机名称
       String exchangeName = "TTL_direct_exchange";
       //定义路由key
       String routeKey = "ttl";
       rabbitTemplate.convertAndSend(exchangeName, routeKey, orderId);
  }
}

config配置类:交换机和过期队列的绑定

  • 过期队列,也就是在队列中添加过期时间,其实也就是普通队列;(x-message-ttl)参数可以在页面中获取

     

     

package com.zhang.rabbitmq.springbootrabbitmqproducer.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class TTLConfigurationProducer {

   //声明交换机与类型
   @Bean
   public DirectExchange TTLDirectExchange(){
       return new DirectExchange("TTL_direct_exchange", true,false);
  }

   //声明路由
   @Bean
   public Queue TTLqueue(){
       Map<String, Object> arge = new HashMap<>();
       arge.put("x-message-ttl",5000);//这里一定是int类型参数。
       //参数1:队列名称 参数2:是否持久化   参数3:排他性     参数4:是否自动删除     参数5:过期时间
       return new Queue("ttl.direct.queue",true,false,false, arge);
  }

   //绑定交换机与队列
   @Bean
   public Binding TTLQueueByEchange(){
       return BindingBuilder.bind(TTLqueue()).to(TTLDirectExchange()).with("ttl");
  }
}

启动类

@Test//过期队列
void TTLDirect(){
   ttlDirectService.TTlMorikOrder("2","4",19);
}

 

设置过期消息

 

 

package com.zhang.rabbitmq.springbootrabbitmqproducer.service;

import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.UUID;

/**
* 过期消息-消息生产者
*/
@Service
public class TTLMessageDirectService {

   @Autowired
   private RabbitTemplate rabbitTemplate;

   public void TTLMessageMakeOrder(String userId, String producerId, Integer number) {
       String orderId = UUID.randomUUID().toString();
       System.out.println("生成订单号:" + orderId);
       //声明交换价名称
       String exchangeName = "ttl_direct_exchange";
       //声明路由key
       String routingKey = "ttlMessage";

       //设置消息过期时间
       MessagePostProcessor messagePostProcessor = new MessagePostProcessor() {
           @Override
           public Message postProcessMessage(Message message) throws AmqpException {
               //设置过期时间
               message.getMessageProperties().setExpiration("5000");//消息过期时间,参数为字符串类型
               //设置字符集编码
               message.getMessageProperties().setContentEncoding("utf-8");
               return message;
          }
      };
       //发送消息
       rabbitTemplate.convertAndSend(exchangeName,routingKey,orderId,messagePostProcessor);
  }
}

config配置类

package com.zhang.rabbitmq.springbootrabbitmqproducer.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
* 过期队列-TTL-交换机和队列的绑定
*/
@Configuration
public class TTLDirectConfiguration {

   //声明交换机和类型
   @Bean
   public DirectExchange TTLDirectExchange(){
       return new DirectExchange("ttl_direct_exchange",true,false);
  }

   //声明过期队列-ttl
   @Bean
   public Queue TTLQueue(){
       //设置过期时间
       Map<String, Object> arge = new HashMap<>();
       arge.put("x-message-ttl", 5000);
       return new Queue("ttl.direct.queue",true,false,false,arge);
  }

   //声明队列
   @Bean
   public Queue TTLMessageQueue(){
       return new Queue("ttl.message.direct.queue",true);
  }

   //绑定交换机与队列
   @Bean
   public Binding TTLQueueByExchange(){
       return BindingBuilder.bind(TTLQueue()).to(TTLDirectExchange()).with("ttl");
  }
   @Bean
   public Binding TTLMessageQueueByExchange(){
       return BindingBuilder.bind(TTLMessageQueue()).to(TTLDirectExchange()).with("ttlMessage");
  }
}

启动类

@Test//过期消息
void TTLMessage(){
   ttlMessageDirectService.TTLMessageMakeOrder("2","5",3);
}

 

posted @ 2021-06-19 17:42  初夏_雨露  阅读(536)  评论(0)    收藏  举报