简体   繁体   English

如何在 Spring-Integration 中重试整个轮询流程

[英]how to retry an entire poller flow in Spring-Integration

I've been pulling my hairs on how to implement the retry pattern (retry the WHOLE flow) for a Spring-Integration poller flow.我一直在研究如何为 Spring-Integration 轮询器流程实现重试模式(重试整个流程)。 Please find below my (erroneous) source-code (doesn't work).请在下面找到我的(错误的)源代码(不起作用)。

What am I doing wrong ?我究竟做错了什么 ?
(if I put a breakpoint on the line throwing an exception, it's only hit once) (如果我在抛出异常的行上设置断点,它只会命中一次)

thanks a lot in advance for your time and your expertise.非常感谢您抽出时间和您的专业知识。

Best Regards此致

nkjp知乎

PS: maybe try to extend AbstractHandleMessageAdvice with a RetryTemplate ? PS:也许尝试使用 RetryTemplate 扩展 AbstractHandleMessageAdvice ?

return IntegrationFLows.from(SOME_QUEUE_CHANNEL)  
.transform(p -> p, e -> e.poller(Pollers.fixedDelay(5000)  
    .advice(RetryInterceptorBuilder.stateless().maxAttempts(5).backOffOptions(1,2,10).build())))  
.transform(p -> {  
  if (true) {
    throw new RuntimeException("KABOOM");
  }
  return p;
})
.channel(new NullChannel())
.get();

If you add poller.advice() , then an Advice is applied to the whole flow starting with poll() method.如果添加poller.advice() ,则Advice将应用于从poll()方法开始的整个流程。 Since you have already polled a message from that queue, there is nothing to poll from it on the next attempt.由于您已经从该队列轮询了一条消息,因此在下次尝试时没有任何消息可以轮询。 It is kinda anti-pattern to use retry for non-transactional queues: you don't rollback transactions so your data doesn't come back to store to be available for the next poll() .对非事务性队列使用重试是一种反模式:您不会回滚事务,因此您的数据不会返回存储以供下一个poll()

There is no way at the moment to retry a whole sub-flow from some point, but you definitely can use a RequestHandlerRetryAdvice on the specific erroneous endpoint like that your transform() with KABOOM exception:目前无法从某个点重试整个子流,但是您绝对可以在特定的错误端点上使用RequestHandlerRetryAdvice ,例如您的transform()KABOOM异常:

.transform(p -> {
                        if (true) {
                            throw new RuntimeException("KABOOM");
                        }
                        return p;
                    }, e -> e.advice(new RequestHandlerRetryAdvice()))

See its setRetryTemplate(RetryTemplate retryTemplate) for more retry options instead of just 3 attempts by default.查看它的setRetryTemplate(RetryTemplate retryTemplate)以获取更多重试选项,而不是默认情况下只有 3 次尝试。

To make for a sub-flow, we need to consider to implement a HandleMessageAdvice .为了创建一个子流,我们需要考虑实现一个HandleMessageAdvice Something like this:像这样的东西:

.transform(p -> p, e -> e.poller(Pollers.fixedDelay(500000))
                            .advice(new HandleMessageAdvice() {

                                RetryOperationsInterceptor delegate =
                                        RetryInterceptorBuilder.stateless()
                                                .maxAttempts(5)
                                                .backOffOptions(1, 2, 10)
                                                .build();

                                @Override
                                public Object invoke(MethodInvocation invocation) throws Throwable {
                                    return delegate.invoke(invocation);
                                }
                            }))

But again: it's not a poller advice., it is an endpoint one on its MessageHandler.handleMessage() .但同样:它不是poller建议。它是MessageHandler.handleMessage()上的一个端点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM