Java 弹簧槽模板设置模板&;已忽略setRecoveryCallback

Java 弹簧槽模板设置模板&;已忽略setRecoveryCallback,java,spring,spring-boot,rabbitmq,spring-rabbit,Java,Spring,Spring Boot,Rabbitmq,Spring Rabbit,问题:经过固定次数的重试后,我想将消息发送到错误队列,并从原始队列中使用它。我想找到一个通用的解决方案,因为我处理很多不同的消息,根据它们引发的异常,我想采取不同的行动。如何设置RecoveryCallback,使Spring在maxRetries之后触发它 我目前所做的 我尝试设置一个RetryTemplate和一个RecoveryCallback。 当我运行应用程序并将消息发布到test队列时,我希望EListener\receive中的处理失败3次,然后触发我配置的RecoveryCall

问题:经过固定次数的重试后,我想将消息发送到错误队列,并从原始队列中使用它。我想找到一个通用的解决方案,因为我处理很多不同的消息,根据它们引发的异常,我想采取不同的行动。如何设置
RecoveryCallback
,使Spring在
maxRetries
之后触发它

我目前所做的
我尝试设置一个
RetryTemplate
和一个
RecoveryCallback
。 当我运行应用程序并将消息发布到
test
队列时,我希望
EListener\receive
中的处理失败3次,然后触发我配置的
RecoveryCallback
,然后根据上下文将消息路由到特定的错误队列

实际发生的情况 实际上,Spring Boot使用自己的
rabbitmplate
初始化
RabbitAdmin
对象,因此不使用我配置的
rabbitmplate
bean

我有以下目录结构:

rabbit
  |___ EListener.java
  |___ Rabbit.java
test
  |___ Test.java
我在
Rabbit.java

@Configuration
public class Rabbit {

    @Autowired
    ConnectionFactory connectionFactory;

    @Bean
    public RabbitTemplate rabbitTemplate() {
        RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
        rabbitTemplate.setRetryTemplate(createRetryTemplate());
        rabbitTemplate.setRecoveryCallback(createRecoveryCallback());
        return rabbitTemplate;
    }

    createRecoveryCallback() // Omitted for brevity
    createRetryTemplate() // Omitted for brevity
}
EListener.java
文件包含:

@Component
public class EListener {

    @Autowired
    RabbitTemplate rabbitTemplate;

    @RabbitListener(bindings = @QueueBinding(value = @Queue(value = "test", durable = "true"), exchange = @Exchange(value = "test", type = ExchangeTypes.TOPIC, durable = "true", autoDelete = "true"), key = "test"))
    public void receive(Message m) throws Exception {
        System.out.println(m);
        throw new Exception();
    }
}
@SpringBootApplication
@ComponentScan("rabbit")
public class Test {

    public static void main(String[] args) {
        new SpringApplicationBuilder(Test.class).application().run(args);
    }
}
Test.java
包含:

@Component
public class EListener {

    @Autowired
    RabbitTemplate rabbitTemplate;

    @RabbitListener(bindings = @QueueBinding(value = @Queue(value = "test", durable = "true"), exchange = @Exchange(value = "test", type = ExchangeTypes.TOPIC, durable = "true", autoDelete = "true"), key = "test"))
    public void receive(Message m) throws Exception {
        System.out.println(m);
        throw new Exception();
    }
}
@SpringBootApplication
@ComponentScan("rabbit")
public class Test {

    public static void main(String[] args) {
        new SpringApplicationBuilder(Test.class).application().run(args);
    }
}

RetryTemplate
添加到
RabbitTemplate
就是重试发布消息

要在使用者端添加重试,必须向侦听器添加重试侦听器

由于您使用的是
@RabbitListener
,因此建议链位于上,这意味着您必须自己声明一个建议,而不是依赖于启动创建的默认建议


无状态重试在内存中执行重试;statefull重试重新请求消息;它需要messageId属性(或某些其他机制来唯一标识消息)。

是否可以只替换当前用作默认恢复程序的
RejectAndDontRequeueRecoverer
;恢复程序已硬连接到。您可以使用boot的configurer,然后用您自己的拦截器覆盖
adviceChain
,然后我会这样做。如果一开始就有这个,那就太好了。谢谢你的帮助。