简体   繁体   中英

camel route http response xml parsing

I am new to using camel and I need to build a route in JAVA to process some xml returned by http request. I tried to parse the body of the response by setting up a route with a processor and log it to a file setting the consumer as http url but it didn't work. Then I tried to set up a jms queue to grab and process it from the queue with similar result. I think I am getting the 200 response but the producer text file I set it to write to is not working and the log4j in DEBUG is not too informative on isolating the issue. Does anyone have any insight on this issue to point me in the right camel direction? thanks in advance!

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


}

I think your routes are not running.

You need something (like a timer ) to trigger your routes, for example:

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

Camel documentation for Direct component says:

The direct: component provides direct, synchronous invocation of any consumers when a producer sends a message exchange.

So you need something else to start the invocation of the route.

Mind that your first route must finish to the correct JMS queue:

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

without the queue name it will not work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM