栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

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

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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

您无法使用属性来执行此操作,必须自己配置重试建议。

如果您需要帮助,稍后再举一个例子。

requeue-rejected
在容器级别(在堆栈上重试以下)。

编辑

@SpringBootApplicationpublic 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
)。我已经提出一个问题来支持这一点。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/412070.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号