Apache camel 使用消息的子字符串进行Apache Camel处理
我想建立一个骆驼路线,做3个httprequest,但在每个请求中,它应该只是身体的一部分 在我的邮件正文中是以下XML:Apache camel 使用消息的子字符串进行Apache Camel处理,apache-camel,Apache Camel,我想建立一个骆驼路线,做3个httprequest,但在每个请求中,它应该只是身体的一部分 在我的邮件正文中是以下XML: <part1> ... </part1> <part2> ... </part2> <part3> ... </part3> ... ... ... 现在,每个部件都应该发送到REST服务。Rest服务响应带有一些数据,这些数据我必须放在下一部分的正文中。我怎样才能解决这个问题 路线应如下所示:
<part1>
...
</part1>
<part2>
...
</part2>
<part3>
...
</part3>
...
...
...
现在,每个部件都应该发送到REST服务。Rest服务响应带有一些数据,这些数据我必须放在下一部分的正文中。我怎样才能解决这个问题
路线应如下所示:
from("activemq:inMsg")
.setBody(xpath("//part1")).inOut("http4://localhost/workingPart1")
.choice()
.when()
.replay().isEqual("ok")
.setBody("<responsePart1>"+replay()+"</responsePart1>" + xpath("//part2")).inOut("http4://localhost/workingPart2")
.choice()
.when()
.replay().isEqual("ok")
.setBody("<responsePart2>"+replay()+"</responsePart2>" + xpath("//part3")).inOut("http4://localhost/workingPart3")
.otherwiese().to("activemq:error")
.end()
.otherwiese().to("activemq:error")
.end()
from(“activemq:inMsg”)
.setBody(xpath(“//part1”).inOut(“http4://localhost/workingPart1”)
.choice()
.when()
.replay().isEqual(“确定”)
.setBody(“+replay()+”+xpath(//part2”).inOut(“http4://localhost/workingPart2”)
.choice()
.when()
.replay().isEqual(“确定”)
.setBody(“+replay()+”+xpath(//part3”).inOut(“http4://localhost/workingPart3”)
.otherwiese().to(“activemq:错误”)
(完)
.otherwiese().to(“activemq:错误”)
(完)
你能帮我找到正确的语法吗
谢谢我想有不止一种可能的解决方案:
ModelCamelContext context = new DefaultCamelContext();
context.addRoutes(new RestBuilder("direct:rest1", "http4://localhost/workingPart1"));
context.addRoutes(new RestBuilder("direct:rest2", "http4://localhost/workingPart2"));
context.addRoutes(new RestBuilder("direct:rest3", "http4://localhost/workingPart3"));
context.addRoutes(new MainRouteBuilder());
ProducerTemplate template = context.createProducerTemplate();
template.sendBody("direct:start", XML);
RestBuilder
类:
public class RestBuilder extends RouteBuilder {
private final String endpoint;
private final String uri;
public RestBuilder(final String endpoint, final String uri) {
this.endpoint = endpoint;
this.uri = uri;
}
@Override
public void configure() {
from(endpoint)
.setHeader(Exchange.HTTP_METHOD, constant(org.apache.camel.component.http4.HttpMethods.POST))
.to(uri)
.choice()
.when(header(Exchange.HTTP_RESPONSE_CODE).isNotEqualTo(Integer.valueOf(200)))
.throwException(new IllegalStateException());
}
}
public class MainRouteBuilder extends RouteBuilder {
@Override
public void configure() {
onException(IllegalStateException.class)
.log("Hm, no good")
.to("activemq:error")
.handled(true);
from("direct:start")
.setProperty("part1").xpath("/parts/part1/*")
.setProperty("part2").xpath("/parts/part2/*")
.setProperty("part3").xpath("/parts/part3/*")
.setBody(simple("${property.part1}"))
.to("direct:rest1")
.setBody(simple("<responsePart1>${body}</responsePart1>${property.part2}"))
.to("direct:rest2")
.setBody(simple("<responsePart2>${body}</responsePart2>${property.part3}"))
.to("direct:rest3");
}
}
MainRouteBuilder
类:
public class RestBuilder extends RouteBuilder {
private final String endpoint;
private final String uri;
public RestBuilder(final String endpoint, final String uri) {
this.endpoint = endpoint;
this.uri = uri;
}
@Override
public void configure() {
from(endpoint)
.setHeader(Exchange.HTTP_METHOD, constant(org.apache.camel.component.http4.HttpMethods.POST))
.to(uri)
.choice()
.when(header(Exchange.HTTP_RESPONSE_CODE).isNotEqualTo(Integer.valueOf(200)))
.throwException(new IllegalStateException());
}
}
public class MainRouteBuilder extends RouteBuilder {
@Override
public void configure() {
onException(IllegalStateException.class)
.log("Hm, no good")
.to("activemq:error")
.handled(true);
from("direct:start")
.setProperty("part1").xpath("/parts/part1/*")
.setProperty("part2").xpath("/parts/part2/*")
.setProperty("part3").xpath("/parts/part3/*")
.setBody(simple("${property.part1}"))
.to("direct:rest1")
.setBody(simple("<responsePart1>${body}</responsePart1>${property.part2}"))
.to("direct:rest2")
.setBody(simple("<responsePart2>${body}</responsePart2>${property.part3}"))
.to("direct:rest3");
}
}
公共类MainRouteBuilder扩展了RouteBuilder{
@凌驾
public void configure(){
OneException(IllegalStateException.class)
.log(“嗯,不好”)
.to(“activemq:错误”)
.已处理(正确);
从(“直接:开始”)
.setProperty(“part1”).xpath(“/parts/part1/*”)
.setProperty(“part2”).xpath(“/parts/part2/*”)
.setProperty(“part3”).xpath(“/parts/part3/*”)
.setBody(简单(“${property.part1}”))
。至(“直接:rest1”)
.setBody(简单(“${body}${property.part2}”))
。至(“直接:rest2”)
.setBody(简单(“${body}${property.part3}”))
。致(“直接:rest3”);
}
}
非常感谢!那就是我要找的!谢谢你的精彩回答!