当前位置:   article > 正文

RabbitMq死信队列_rabbitmq 死信队列的作用

rabbitmq 死信队列的作用

死信队列的作用

死信交换机有什么用呢? 在创建队列的时候 可以给这个队列附带一个交换机, 那么这个队列作废的消息就会被重新发到附带的交换机,然后让这个交换机重新路由这条消息。

死信消息产生的来源

  • 消息被拒绝(basic.reject或basic.nack)并且requeue=false
  • 消息TTL过期
  • 队列达到最大长度(队列满了,无法再添加数据到mq中)

死信队列处理的方式

  • 丢弃,如果不是很重要,可以选择丢弃
  • 记录死信入库,然后做后续的业务分析或处理
  • 通过死信队列,由负责监听死信的应用程序进行处理
    在这里插入图片描述

消息超时进入死信队列

通俗的说,就是消息产生之后,因为设置了超时时间,在这段时间内消息没有被消费就会被扔到死信队列里面。

 // 交换机名称
    private static final String DESTINATION_NAME = "rabbitMq_topic";
    //消息队列
    private static final String queueName = "topic_queue";
    //routingKey
    private static final String routingKey = "topic.#";

    //配置死信队列
    private static final String dlxExchangeName = "dlx.exchange";
    private static final String dlxQueueName = "dlx.queue";
    private static final String dlxRoutingKey = "#";

    @Test
    public void producer() throws IOException, TimeoutException {
   
        //获取连接
        Connection connection = MQConnectionUtils.newConnection();
        //创建通道
        Channel channel = connection.createChannel();
        Map<String, Object> arguments = new HashMap<String, Object>(16);
        // 为队列设置队列交换器
        arguments.put("x-dead-letter-exchange", dlxExchangeName);
        // 设置队列中的消息 60s 钟后过期
        arguments.put("x-message-ttl", 60000);
        //正常生产者绑定交换机 参数1 交换机名称 参数2 交换机类型
        channel.exchangeDeclare(DESTINATION_NAME, "topic", true, false, null);
        //消费声明队列
        channel.queueDeclare(queueName, true, false, false, arguments);
        //消费者队列绑定交换机 绑定路由件 路由键
        channel.queueBind(queueName, DESTINATION_NAME, routingKey);

        String message = new
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/黑客灵魂/article/detail/1003075
推荐阅读
相关标签
  

闽ICP备14008679号