【问题标题】:How to handle 4xx(without retry) and 5xx (with retry) exceptions in camel如何处理骆驼中的4xx(不重试)和5xx(重试)异常
【发布时间】:2022-01-25 16:45:50
【问题描述】:

我有一个发出 API 请求的骆驼路线,外部服务可能是 4xx 或 5xx。我已经编写了HttpOperationFailedException 处理程序来处理所有与 HTTP 相关的异常,并且我正在重试所有 Http 异常,无论它是客户端还是服务器端异常。我想以某种方式处理它们,我需要避免客户端异常的reties。

这是我的路线和异常代码,看起来像。任何人都可以建议处理这些情况的最佳方法吗?


  onException(HttpOperationFailedException.class)
        .handled(true)
        .redeliveryDelay(100)
        .maximumRedeliveries(2)
        .log("${exception} Http Communication Exception while making API request")
        .end();


from("direct:start")
        .routeId("restApi")
        .process(exchange -> exchange.getIn().setBody(
            new RequestBody(
                "${headers.camelFileName}")))
        .marshal()
        .json(JsonLibrary.Gson)
        .setHeader(Exchange.HTTP_METHOD, constant("POST"))
        .setHeader("Content-Type",constant("application/json"))
        .to(url)
        .end();

【问题讨论】:

    标签: java spring-boot apache-camel spring-camel camel-rest


    【解决方案1】:

    您可以尝试以下方式:

            onException(HttpOperationFailedException.class)     
                    .choice()
                        .when(simple("${exception.getStatusCode()} == '400'"))
                         //doSomething
                        .endChoice()
                        .when(simple("${exception.getStatusCode()} == '500'"))
                         //doSomething
                        .otherwise()
                        //retries
                        .endChoice()
                    .end()
            ;
    

    【讨论】:

      猜你喜欢
      • 2012-11-21
      • 1970-01-01
      • 2017-06-28
      • 2012-06-26
      • 1970-01-01
      • 1970-01-01
      • 2023-04-06
      • 1970-01-01
      • 2012-09-09
      相关资源
      最近更新 更多