camel路由http响应xml解析

gzjq41n4  于 9个月前  发布在  Apache
关注(0)|答案(1)|浏览(74)

我是一个使用camel的新手,我需要在JAVA中构建一个路由来处理HTTP请求返回的一些XML。我试图通过用处理器设置一个路由来解析响应的主体,并将其记录到一个文件中,将消费者设置为http url,但它不起作用。然后,我尝试设置一个jms队列,从队列中抓取并处理它,结果类似。我想我得到了200响应,但我设置的生产者文本文件不工作,调试中的log4j在隔离问题上没有太多的信息。有没有人在这个问题上有任何见解,以指出我在正确的 Camel 方向?先谢谢你!

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();
    }

}
pbwdgjma

pbwdgjma1#

我觉得你的路线没有运行。
你需要一些东西(比如timer)来触发你的路由,例如:

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

Camel documentation for Direct component说:
当生产者发送消息交换时,**direct:**组件提供对任何消费者的直接、同步调用。
因此,您需要其他东西来启动路由的调用。
请注意,您的第一个路由必须到达正确的JMS队列:

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

如果没有队列名称,它将无法工作。

相关问题