【问题标题】:camel route http response xml parsing骆驼路由http响应xml解析
【发布时间】: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


【解决方案1】:

我认为你的路线没有运行。

你需要一些东西(比如timer)来触发你的路由,例如:

from("timer:myTimer?period=30s")
    .to("direct:start");

骆驼documentation for Direct component 说:

direct: 组件在生产者发送消息交换时提供对任何消费者的直接、同步调用。

所以你需要其他东西来启动路由的调用。

请注意,您的第一条路线必须完成到正确的 JMS 队列:

// ... cut
.to("file:C:/Desktop/camellog1.txt")
.log("${in.headers}")
.to("stream:out")
.to("jms:incomingOrders");

没有队列名称将无法工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-02-17
    • 2023-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-27
    • 1970-01-01
    相关资源
    最近更新 更多