spring-boot - Spring rabbit 重试传递被拒绝的消息..可以吗?

标签 spring-boot rabbitmq spring-amqp spring-rabbit

我有以下配置

spring.rabbitmq.listener.prefetch=1
spring.rabbitmq.listener.concurrency=1
spring.rabbitmq.listener.retry.enabled=true
spring.rabbitmq.listener.retry.max-attempts=3
spring.rabbitmq.listener.retry.max-interval=1000
spring.rabbitmq.listener.default-requeue-rejected=false //I have also changed it to true but the same behavior still happens

在我的听众中,我抛出异常 AmqpRejectAndDontRequeueException 拒绝消息并强制兔子不要尝试重新传递它 ......但是兔子将它重发了 3 次,然后最终将其路由到死信队列。

这是根据我提供的配置的标准行为还是我错过了什么?

最佳答案

您必须将重试策略配置为不为该异常重试。

你不能用属性来做到这一点,你必须自己配置重试建议。

如果您需要帮助,我稍后会发布一个示例。
requeue-rejected位于容器级别(在堆栈上重试下方)。

编辑

@SpringBootApplication
public class So39853762Application {

    public static void main(String[] args) throws Exception {
        ConfigurableApplicationContext context = SpringApplication.run(So39853762Application.class, args);
        Thread.sleep(60000);
        context.close();
    }

    @RabbitListener(queues = "foo")
    public void foo(String foo) {
        System.out.println(foo);
        if ("foo".equals(foo)) {
            throw new AmqpRejectAndDontRequeueException("foo"); // won't be retried.
        }
        else {
            throw new IllegalStateException("bar"); // will be retried
        }
    }

    @Bean
    public ListenerRetryAdviceCustomizer retryCustomizer(SimpleRabbitListenerContainerFactory containerFactory,
            RabbitProperties rabbitPropeties) {
        return new ListenerRetryAdviceCustomizer(containerFactory, rabbitPropeties);
    }

    public static class ListenerRetryAdviceCustomizer implements InitializingBean {

        private final SimpleRabbitListenerContainerFactory containerFactory;

        private final RabbitProperties rabbitPropeties;

        public ListenerRetryAdviceCustomizer(SimpleRabbitListenerContainerFactory containerFactory,
                RabbitProperties rabbitPropeties) {
            this.containerFactory = containerFactory;
            this.rabbitPropeties = rabbitPropeties;
        }

        @Override
        public void afterPropertiesSet() throws Exception {
            ListenerRetry retryConfig = this.rabbitPropeties.getListener().getRetry();
            if (retryConfig.isEnabled()) {
                RetryInterceptorBuilder<?> builder = (retryConfig.isStateless()
                        ? RetryInterceptorBuilder.stateless()
                        : RetryInterceptorBuilder.stateful());
                Map<Class<? extends Throwable>, Boolean> retryableExceptions = new HashMap<>();
                retryableExceptions.put(AmqpRejectAndDontRequeueException.class, false);
                retryableExceptions.put(IllegalStateException.class, true);
                SimpleRetryPolicy policy =
                        new SimpleRetryPolicy(retryConfig.getMaxAttempts(), retryableExceptions, true);
                ExponentialBackOffPolicy backOff = new ExponentialBackOffPolicy();
                backOff.setInitialInterval(retryConfig.getInitialInterval());
                backOff.setMultiplier(retryConfig.getMultiplier());
                backOff.setMaxInterval(retryConfig.getMaxInterval());
                builder.retryPolicy(policy)
                    .backOffPolicy(backOff)
                    .recoverer(new RejectAndDontRequeueRecoverer());
                this.containerFactory.setAdviceChain(builder.build());
            }
        }

    }

}

注意:您当前无法将策略配置为重试所有异常,“除了”此异常 - 您必须对要重试的所有异常进行分类(并且它们不能是 AmqpRejectAndDontRequeueException 的父类(super class))。我开通了issue to support this .

关于spring-boot - Spring rabbit 重试传递被拒绝的消息..可以吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39853762/

相关文章:

java - 一次连续向10000个客户端发布数据

java - Spring Boot 测试加载 ApplicationContext 失败

rabbitmq - 在 RabbitMQ 中通过属性或 header 获取消息

java - 无法将 Message 对象从 RabbitMQ 转换为 java 类

spring-amqp - 如何为@RabbitListener 注解编写集成测试?

rest - ControllerAdvice ResponseBodyAdvice 未能包含字符串响应

synchronization - 如何将消费者与rabbitmq同步

ios - 无法使用 Swift 连接到远程 RabbitMQ 服务器

java - Spring Listener Container - 事务管理器 - Spring Retry - 多个事务之间

java - Spring Boot - 当程序失去与数据库的连接时如何执行方法?