要求:调用GET服务,如果响应是200以外的任何其他状态,则需要抛出 HttpOperationFailedException ,这样我可以在父路由上使用onException处理204异常。
我能够使用以下代码实现它:
from("direct:parent")
.onException(HttpOperationFailedException.class)
.onWhen(exchange ->{
HttpOperationFailedException exe = exchange.getException(HttpOperationFailedException.class);
if(204 == exe.getStatusCode()){
return true;
}
return false;
})
.setBody(constant(null))
.end()
.to("direct:a");
from("direct:a")
.recipientList("false")
.simple("http4://localhost:8022/test/service?okStatusCodeRange=200-201")
.convertBodyTo(String.class);
但是,使用以下代码时不会抛出异常:
from("direct:parent")
.onException(HttpOperationFailedException.class)
.onWhen(exchange ->{
HttpOperationFailedException exe = exchange.getException(HttpOperationFailedException.class);
if(204 == exe.getStatusCode()){
return true;
}
return false;
})
.setBody(constant(null))
.end()
.to("direct:a");
from("direct:a")
.to("http4://localhost:8022/test/service?okStatusCodeRange=200-201")
.convertBodyTo(String.class);
有人会解释需要更改哪些内容而不是收件人列表吗?
答案 0 :(得分:0)
当您将HTTP URI调用从direct:parent
传播到direct:a
但在direct:parent
中维护嵌套异常子句时,direct:a
中抛出的异常是 NOT < / strong>向上传播到父路线。但是,您应该做的是将嵌套的异常子句重构为全局异常子句。
我创建了一个简单的测试用例,它可以模拟204消息上抛出的异常,也可以调用实际服务,并在204响应时失败:
import org.apache.camel.Produce;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.builder.AdviceWithRouteBuilder;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.http.common.HttpOperationFailedException;
import org.apache.camel.test.junit4.CamelTestSupport;
import org.junit.Test;
import java.util.HashMap;
import java.util.Map;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.nullValue;
public class Http4ExceptionHandlingTest extends CamelTestSupport {
@Produce(uri = "direct:parent")
protected ProducerTemplate template;
@Override
public boolean isUseAdviceWith() {
return true;
}
@Override
protected RouteBuilder createRouteBuilder() {
return new RouteBuilder() {
@Override
public void configure() throws Exception {
onException(HttpOperationFailedException.class)
.onWhen(exchange -> {
HttpOperationFailedException
exe = exchange.getException(HttpOperationFailedException.class);
return 204 == exe.getStatusCode();
})
.log("HTTP exception handled")
.handled(true)
//.continued(true)
.setBody(constant(null));
from("direct:parent").routeId("parent")
// .onException(HttpOperationFailedException.class)
// .onWhen(exchange -> {
// HttpOperationFailedException
// exe = exchange.getException(HttpOperationFailedException.class);
// return 204 == exe.getStatusCode();
// })
// .setBody(constant(null))
// .end()
.log("Parent start");
.to("direct:a")
.log("Parent done");
from("direct:a").routeId("a")
.log("a start")
.to("http4://localhost:8022/test/service?okStatusCodeRange=200-201")
.convertBodyTo(String.class)
.log("a done");
}
};
}
@Test
public void testExceptionHandling() throws Exception {
// comment the following line out if you want to invoke the real service instead!
weaveRoute();
context.start();
Object response = template.requestBody("foo");
assertThat(response, is(nullValue()));
}
@Test
public void testSuccessfulResponse() throws Exception {
// comment the following line out if you want to invoke the real service instead!
weaveRoute();
context.start();
Object response = template.requestBody("bar");
assertThat(response, is(equalTo("bar")));
}
private void weaveRoute() throws Exception {
context.getRouteDefinition("a").adviceWith(context, new AdviceWithRouteBuilder() {
@Override
public void configure() throws Exception {
this.interceptSendToEndpoint("http4*")
.skipSendToOriginalEndpoint()
.process(exchange -> {
String body = exchange.getIn().getBody(String.class);
if ("foo".equals(body)) {
Map<String, String> headers = new HashMap<>();
String location = "";
HttpOperationFailedException exe =
new HttpOperationFailedException("http://bla", 204, "No Content", location,
headers, "response body");
throw exe;
}
});
}
});
}
}
我在原始路由中保留了原始的嵌套异常子句,因此您可以比较结果。全局异常处理程序得到了一个额外的.handled(true
)`语句,该语句突破了当前路由。因此,文件说明如下:
如果handle为true,则将处理抛出的异常,并且Camel将不会继续在原始路由中进行路由,但会中断。但是,您可以在onException中配置将使用的路由。如果需要向调用者创建一些自定义响应消息,或者因为抛出该异常而执行任何其他处理,则使用此路由。 (Source)
未将捕获的异常设置为.handled(true)
将导致堆栈跟踪的呈现,而不是继续执行。
我添加了更多日志语句来可视化异常处理中的行为。在执行上面显示的代码时,您将获得如下输出:
[INFO ] - - Parent start [ ] [parent] [ ] [main]
[INFO ] - - a start [ ] [a] [ ] [main]
[INFO ] - - HTTP exception handled [ ] [a] [ ] [main]
您可以使用.handled(true)
代替.continued(true)
,以便按照文档记录继续执行:
如果继续为真,那么Camel将捕获异常,实际上只是忽略它并继续在原始路由中路由。但是,如果您在onException中配置了路由,它将首先路由该路由,然后继续在原始路由中路由。
在全局异常子句中使用已启用的.continued(true)
和已禁用的.handled(true)
运行测试将生成以下日志:
[INFO ] - - Parent start [ ] [parent] [ ] [main]
[INFO ] - - a start [ ] [a] [ ] [main]
[INFO ] - - HTTP exception handled [ ] [a] [ ] [main]
[ERROR] - - Failed delivery for (MessageId: ...). Exhausted after delivery attempt: 1 caught: null. Handled and continue routing.
Message History
---------------------------------------------------------------------------------------------------------------------------------------
RouteId ProcessorId Processor Elapsed (ms)
[parent ] [parent ] [direct://parent ] [ 8]
[parent ] [log9 ] [log ] [ 0]
[parent ] [to4 ] [direct:a ] [ 8]
[a ] [log7 ] [log ] [ 1]
[a ] [to3 ] [http4://localhost:8022/test/service?okStatusCodeRange=200-201 ] [ 8]
[ ] [process2 ] [Processor@0x3c7f66c4 ] [ 8]
[a ] [log6 ] [log ] [ 1]
[a ] [setBody2 ] [setBody[{null}] ] [ 0]
Stacktrace
--------------------------------------------------------------------------------------------------------------------------------------- [ ] [o.a.c.p.DefaultErrorHandler] [ ] [main]
org.apache.camel.http.common.HttpOperationFailedException: HTTP operation failed invoking http://bla with statusCode: 204, redirectLocation:
at at.erpel.messaginghub.services.unit.routes.rest.Http4ExceptionHandlingTest$2.lambda$configure$1(Http4ExceptionHandlingTest.java:103)
at org.apache.camel.processor.DelegateSyncProcessor.process(DelegateSyncProcessor.java:63)
...
[INFO ] - - a done [ ] [a] [ ] [main]
[INFO ] - - Parent done [ ] [parent] [ ] [main]
从简化日志中可以看出,.continued(true)
不会突破路由,但会记录消息历史记录以及忽略异常的堆栈跟踪。
如果父路由中存在nesed exception子句,而子路由确实遇到异常,则将.handled(true)
或.continuted(true)
添加到嵌套异常子句中无效,因为实际异常被捕获子路由不会传播到父嵌套异常子句,因此根本不会处理。
我已经针对模拟以及真实服务测试了代码示例,该服务在收到204
正文时返回了foo
响应,并在收到任何其他内容时返回200
响应。因此,?okStatusCodeRange=200-201
配置参数在我的情况下按预期工作。对于completenes:我使用的是Camel 2.17.0。