Apache Camel -条件重试最大次数取决于交换属性;如果不满足条件,则为0

6ovsh4lw  于 2023-01-05  发布在  Apache
关注(0)|答案(1)|浏览(108)

如何根据交换的某些属性动态地设置重试次数?
我想发送一个事件到目的地,然后处理响应,如果这个事件是positive == true,那么我想同步重试3次;如果没有,就不要重试。

from(RETRY_ONLINE_ENDPOINT)
        .routeId(RETRY_ONLINE_ROUTE_ID)
        .choice()
            .when(simple("${exchangeProperty.positive} != true"))
                .onException(HttpOperationFailedException.class)
                .log(LoggingLevel.INFO, "Caught: " + simple("${exchangeProperty.CamelExceptionCaught}") + ", retried attempts: " + simple("${header.CamelRedeliveryCounter}"))
                .maximumRedeliveries(3)
                .handled(true)
                .bean(PostRetryBean.class)
            .endChoice()
            .otherwise()
                .bean(PostRetryBean.class)
            .endChoice()
        .end();

但我得到异常onException()必须设置为顶级错误。
如果我将onException()移到顶层,则编译无法通过。MaximizeRetryTimes不能跟在when()后面。
那么,如何有条件地设置最大重试次数呢?

s4chpxco

s4chpxco1#

现在我发现:使用onWhen()额外检查条件以决定是否使用此onException()路由。

/**
     * Sets an additional predicate that should be true before the onException is triggered.
     * <p/>
     * To be used for fine grained controlling whether a thrown exception should be intercepted by this exception type
     * or not.
     *
     * @param  predicate predicate that determines true or false
     * @return           the builder
     */
    public OnExceptionDefinition onWhen(@AsPredicate Predicate predicate) {
        setOnWhen(new WhenDefinition(predicate));
        return this;
    }

所以它变成了:

onException(HttpOperationFailedException.class).onWhen(simple("${exchangeProperty.positive} == true"))
                .maximumRedeliveries(3)
                .handled(true)
                .bean(RecoverableExceptionHandler.class)
                .to(REPLY_QUEUE_ENDPOINT);

onException(HttpOperationFailedException.class).onWhen(simple("${exchangeProperty.positive} == false"))
                .handled(true)
                .bean(RecoverableExceptionHandler.class)
                .to(REPLY_QUEUE_ENDPOINT);

相关问题