ferhat kul
ferhat kul

Reputation: 43

Apache Camel: Response is get lost when reached explicitly on route

I am getting a strange situation at the code below which simply routes request to Google and returns response.

It works well but when I activate the line commented out as "//Activating this line causes empty response on browser" to print out returned response from http endpoint (Google), response is disappear, nothing is displayed on browser. I thought it might be related with input stream of http response which can be consumed only once and I activated Stream Caching on context but nothing changed.

Apache Camel version is 2.11.0

Any suggestions are greatly appreciated, thanks in advance.

    public class GoogleCaller {
        public static void main(String[] args) throws Exception {
            CamelContext context = new DefaultCamelContext();

        context.addRoutes(new RouteBuilder() {
            public void configure() {
                from("jetty:http://0.0.0.0:8081/myapp/")
                .to("jetty://http://www.google.com?bridgeEndpoint=true&throwExceptionOnFailure=false")
                .process(new Processor() {

                    public void process(Exchange exchange) throws Exception {
                        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
                    }
                });
            }
        });

        context.setTracing(true);
        context.setStreamCaching(true);
        context.start();
    }
}

Upvotes: 1

Views: 2650

Answers (1)

Willem Jiang
Willem Jiang

Reputation: 3291

As you use a custom processor to process the message, you should keep it in mind the in message of the exchange has response message from the google, if you are using exchange.getOut(), camel will create a new empty out message for you and treat it as response message.

Because you don't set the out message body in the processor, it makes sense that you get the empty response in the browser.

Upvotes: 3

Related Questions