![](/img/trans.png)
[英]How to handle HTTP response with a 4xx/5xx status code, Java servlet/HttpURLConnection
[英]How to handle 4xx(without retry) and 5xx (with retry) exceptions in camel
我有一条骆驼路线,它发出 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();
您可以尝试以下方式:
onException(HttpOperationFailedException.class)
.choice()
.when(simple("${exception.getStatusCode()} == '400'"))
//doSomething
.endChoice()
.when(simple("${exception.getStatusCode()} == '500'"))
//doSomething
.otherwise()
//retries
.endChoice()
.end()
;
您可以使用条件陷阱,您可以针对每种情况进行谓词
onException(HttpOperationFailedException.class)
.onWhen(exchangeProperty("CamelExceptionCaught").isEqualTo("404")).to("direct:go404")
.onWhen(exchangeProperty("CamelExceptionCaught").isEqualTo("500")).to("direct:go500");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.