注:必须是rabbitmq消费者中没有catch住的异常,才能起效。

直接上代码:

配置文件:

  rabbitmq:
    host: 192.168.0.105
    username: wz
    password: wangzheng
    virtual-host: /ll
    template: #一下配置时发送消息方配置
      exchange: ll.doc.exchange
    publisher-confirms: true
    port: 5672
    listener:
      simple:
        retry:
          enabled: true
          max-attempts: 5
          initial-interval: 5000ms
        default-requeue-rejected: false

config配置实体:

package com.langke.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.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.retry.MessageRecoverer;
import org.springframework.amqp.rabbit.retry.RepublishMessageRecoverer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @ClassName RabbitMqConfig
 * @Description rabbitmq 配置文件
 * @Author wangzhengi
 * @Date 2022/10/13 9:17
 * @Version 1.0
 */
@Configuration
public class RabbitMqConfig {

    @Bean
    public DirectExchange errorExchange(){
        return new DirectExchange("lanke-error-exchange",true,false);
    }

    @Bean
    public Queue errorQueue(){
        return new Queue("error-queue",true);
    }

    @Bean
    public Binding errorBinding(DirectExchange errorExchange , Queue errorQueue){
        return BindingBuilder.bind(errorQueue).to(errorExchange).with("error-message-insert");
    }

    @Bean
    public MessageRecoverer messageRecoverer(RabbitTemplate rabbitTemplate){
        return new RepublishMessageRecoverer(rabbitTemplate,"lanke-error-exchange","error-message-insert");
    }
}

这样的话,消费者中超过5此依然失败的,就会进入死信队列了,到时候,用个消费者绑定到死信队列上,提醒人工干预即可

更多推荐