Java Camel:根据响应将文件保留到文件夹
我有这样的DSL:Java Camel:根据响应将文件保留到文件夹,java,apache-camel,Java,Apache Camel,我有这样的DSL: from("file:data/inbox?noop=true") .marshal() .string("UTF-8") .setHeader(Exchange.CONTENT_TYPE,constant("application/json")) .to("http://www.a-service.com")
from("file:data/inbox?noop=true")
.marshal()
.string("UTF-8")
.setHeader(Exchange.CONTENT_TYPE,constant("application/json"))
.to("http://www.a-service.com")
.choice()
.when(new Predicate() {
@Override
public boolean matches(Exchange exchange) {
Message in = exchange.getIn();
String msg = in.getBody(String.class);
System.out.println(" Response: " + msg);
if(msg.contains("\"status\":\"OK\"")){
return true;
}else{
return false;
}
}
})
// OK!!!
.to("file:data/outbox_success")
.otherwise()
// NOT OK !!!
.to("file:data/outbox_fail");
我预计,如果http响应具有“状态”:“OK”,文件将转到“data/outbox\u success”。否则,他们将进入“数据/发件箱\失败”
但这不是我所期望的:是的,文件已复制到“发件箱”但文件中没有内容。
我猜这是因为“In”消息已更改为http响应
那么,如何根据“http”的响应将文件复制到文件夹中?尝试将正文存储在标题中以供以后使用:
from("file:data/inbox?noop=true")
.marshal()
.string("UTF-8")
.setHeader(Exchange.CONTENT_TYPE,constant("application/json"))
.setHeader("fileBody", simple(${body}))
.to("http://www.a-service.0com")
.setHeader("webResponse", simple(${body})) //store the response from the http call
.setBody(simple(${header.fileBody})) //reset body to the original file body
.choice()
.when(new Predicate() {
@Override
public boolean matches(Exchange exchange) {
Message in = exchange.getIn();
String msg = in.getHeader("webResponse");
System.out.println(" Response: " + msg);
if(msg.contains("\"status\":\"OK\"")){
return true;
}else{
return false;
}
}
})
// OK!!!
.to("file:data/outbox_success")
.otherwise()
// NOT OK !!!
.to("file:data/outbox_fail");
在更正Matthew时,您将原始邮件的正文存储两次。一次在标题中,一次在正文中。这在操作上既昂贵又可能消耗双倍的内存。只需将原始主体存储在标题中,并将其保留在那里。