zoukankan      html  css  js  c++  java
  • RabitMq过期时间TTL

    第一种:给消息设置过期时间  启动一个插件
    
     @Bean
        public DirectExchange DirectExchange() {
            return new DirectExchange("ttl_direct_exchange", true, false);
        }
    
         //给消息设置过期时间
        @Bean
        public Queue ttlMessageQueue() {
            return new Queue("ttl.message.direct.queue", true);
        }
    
        @Bean
        public Binding ttlMessageBind() {
            return BindingBuilder.bind(ttlMessageQueue()).to(DirectExchange()).with("ttlMessage");
        }
    
    
    
    
    public void ttlMessage() {
        String orderId= UUID.randomUUID().toString();
        String exchangeName3="ttl_direct_exchange";
        MessagePostProcessor messagePostProcessor=new MessagePostProcessor() {
            //设置消息的过期时间
            @Override
            public Message postProcessMessage(Message message) throws AmqpException {
                message.getMessageProperties().setExpiration("5000");
                message.getMessageProperties().setContentEncoding("UTF-8");
                return message;
            }
        };
        String ttl="ttlMessage";
        rabbitTemplate.convertAndSend(exchangeName3,ttl,orderId);
    }
    

     第二种给队列设置过期时间:

       @Bean
        public Queue ttlQueue() {
            Map<String,Object> args=new HashMap<>();
            args.put("x-message-ttl",5000);//这里过期时间一定是一个INT类型
            return new Queue("ttl.direct.queue", true,false,false,args);
        }
          //绑定过期队列与路由
        @Bean
        public Binding ttlBind() {
            return BindingBuilder.bind(ttlQueue()).to(DirectExchange()).with("ttl");
        }
    
    
     public void makeOrder() {
            String orderId= UUID.randomUUID().toString();
            System.out.println("订单ok");
            //带有过期时间的ttl
            String exchangeName3="ttl_direct_exchange";
            String ttl="ttl";
            rabbitTemplate.convertAndSend(exchangeName3,ttl,orderId);
        }
    

       如果:设置了两种过期时间,以时间短的为基准,因为无论是队列先过期,还是队列里面的消息先过期,另一个单独存在都没有意义

    队列过期和消息过期的最大区别是,队列过期了里面的信息可以当作别处用,去处理,比如死信队列,

    而消息过期就是单纯的过期,就是这条消息没有了

    一点点学习,一丝丝进步。不懈怠,才不会被时代淘汰
  • 相关阅读:
    html5--html实现乘法口诀表
    html5--switch选择结构的优化
    CSS盒子模型
    html5--项目实战-仿天猫(移动端页面)
    关于运动
    自然拼读法长元音
    揭开自然拼读法(Phonics)的神秘面纱
    ExtJs自学教程(1):一切从API開始
    四个好看的CSS样式表格
    【Linux】linux经常使用基本命令
  • 原文地址:https://www.cnblogs.com/wangbiaohistory/p/14584402.html
Copyright © 2011-2022 走看看