首页 > 解决方案 > Camel SFTP Producer 重试特定类型的错误

问题描述

如果出现错误,我们需要对骆驼 SFTP 生产者延迟重试一定次数。通过使用 maximumReconnectAttempts 和 reconnectDelay,camel 尝试重试所有类型的错误,但只想重试某些类型的错误,如 socketException 或 connectionexception 或 jschExceptions 并避免重试身份验证异常。这是我们为此使用的示例代码。我们如何配置为仅重试某些类型的错误而不是全部?

from("file:///test/dummy/moveFailed=error&antInclude=*.txt&initialDelay=60000").routeId("test")
.log(LoggingLevel.DEBUG, " Message sending to Destination")
.setHeader(Exchange.FILE_NAME, simple("Test${date:now:yyyyMMdd}.CSV"))
.to("sftp://username@host/destinationpassword=password123&reconnectDelay=3000&maximumReconnectAttempts=5")
.log(LoggingLevel.INFO,"event=${in.header.event} || File successfully transmitted to Destination")
.end();

标签: javaapache-cameljschjbossfuse

解决方案


如果您想控制每个异常的行为,您可以执行以下操作:

onException(MyRetryableServiceException.class)
    .useOriginalMessage()
    .redeliveryDelay(1000)
    .backOffMultiplier(1.5)
    .maximumRedeliveries(3)
    .retryAttemptedLogLevel(LoggingLevel.WARN)
    .useExponentialBackOff();

当 aMyRetryableServiceException被抛出时,消息将按照 重新传递maximumRedeliveries。您可以定义多个onException或将要重试的那些包装在单个异常中...

这优先于默认错误处理程序,请参阅异常子句骆驼错误处理

其他所有内容都将转到处理的默认错误,该错误将重试所有类型的异常(以前的骆驼 2.x,版本 >= 2.0 不重试)。除非你覆盖它。

因此,一种可能的解决方案是定义OnException跳过重试身份验证错误,如果您使用的是骆驼 < 2.0,则保留默认错误处理程序以重试其他错误处理程序

您也可以禁用默认错误处理程序noErrorHandler();或对其进行自定义,例如:

errorHandler(
    deadLetterChannel("seda:errors")
    .logNewException(true)
    .log("Unable to process ${body}")
    .maximumRedeliveries(1)
    .retryAttemptedLogLevel(LoggingLevel.WARN)
    .useExponentialBackOff()
);

推荐阅读