【发布时间】:2017-03-10 08:50:55
【问题描述】:
我是使用骆驼的新手,我需要在 JAVA 中构建一个路由来处理一些由 http 请求返回的 xml。我试图通过使用处理器设置路由并将其记录到将使用者设置为http url的文件来解析响应的主体,但它不起作用。然后我尝试设置一个 jms 队列来从队列中抓取并处理它,结果相似。 我想我收到了 200 响应,但我设置它写入的生产者文本文件不起作用,并且 DEBUG 中的 log4j 在隔离问题方面没有太多信息。有没有人对这个问题有任何见解,可以为我指出正确的骆驼方向?提前致谢!
public static void main(String[] args) throws Exception {
CamelContext camelContext = new DefaultCamelContext();
// connect to embedded ActiveMQ JMS broker
ConnectionFactory connectionFactory =
new ActiveMQConnectionFactory("vm://localhost");
camelContext.addComponent("jms",
JmsComponent.jmsComponentAutoAcknowledge(connectionFactory));
try {
camelContext.addRoutes(new RouteBuilder() {
@Override
public void configure() throws Exception {
from("direct:start")
.to("http://tomcatappurl:8080/format=xml?bridgeEndpoint=true")
.process(new OrderProcessor())
.to("log:DEBUG?showBody=true&showHeaders=true")
.log("file:C:/Desktop/camellog1.txt")
.to("log:DEBUG?showBody=true&showHeaders=true")
.log("${headers}")
.convertBodyTo(String.class)
.to("file:C:/Desktop/camellog1.txt")
.log("${in.headers}")
.to("stream:out")
.to("jms");
from("jms:incomingOrders")
.process(new Processor() {
public void process (Exchange exchange) throws Exception {
//HttpServletRequest request = exchange.getIn().getBody(HttpServletRequest.class);
System.out.println("Response received from Google, is streamCaching = " + exchange.getContext().isStreamCaching());
System.out.println("----------------------------------------------IN MESSAGE--------------------------------------------------------------");
System.out.println(exchange.getIn().getBody(String.class));
System.out.println("----------------------------------------------OUT MESSAGE--------------------------------------------------------------");
//System.out.println(exchange.getOut().getBody(String.class)); //Activating this line causes empty response on browser
}
})
.to("file:C:/Users/Desktop/camellog1.txt?fileExist=Append");
}
});
camelContext.start();
} finally {
camelContext.stop();
}
}
【问题讨论】:
-
在您的路线上设置和错误处理程序,这样您可以确保没有发生静默异常..我在处理骆驼路线时遇到过抛出异常但没有在任何地方捕获的情况跨度>
-
那么究竟是什么错误?文件没有被写入?内容不好?你得到什么错误?
标签: java xml http apache-camel activemq