2013-08-18 16 views
1

我在下面的代碼中發現一個奇怪的情況,它只是將請求路由到Google並返回響應。Apache Camel:當在路由上明確到達時,響應會丟失

它運作良好,但是當我激活註釋掉作爲行「//激活此行導致瀏覽器的空響應」打印出從HTTP端點(谷歌)返回的響應,反應消失,顯示在沒有瀏覽器。我認爲這可能與http響應的輸入流有關,它只能被使用一次,並且我在上下文中激活了Stream Caching,但沒有任何變化。

Apache的駱駝版本是2.11.0

任何建議都大爲讚賞,謝謝提前。

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

回答

2

當你使用一個定製的處理器來處理消息,你應該記住它在交易所的消息已經從谷歌響應消息,如果你正在使用exchange.getOut(),駱駝將創建一個新的空出來的消息,並將其視爲響應消息。

因爲您沒有在處理器中設置出消息正文,所以在瀏覽器中獲得空響應是有道理的。

+0

正如Willem正確指出的那樣,當你使用exchange.getOut()時,它會創建一條新消息,這就是爲什麼你會在路由上消失的原因。你可以通過使用--exchange.getOut.setBody(exchange.getIn()。getBody(String.class))來顯式地設置Out消息,但總是更好地更改In消息本身。閱讀更多 - http:// camel。 apache.org/using-getin-or-getout-methods-on-exchange.html – U2one

+0

@ferhatkul不要使用.getOut()作爲retreiving的東西 - 只是爲了設置一個新的消息。在你的情況 - 不要使用它。 來自一個處理器(jetty或不是)的「out」消息將成爲下一個「in」消息。自定義處理器的行爲是在「進程」方法執行完畢後將「in」消息複製到「out」消息。 –

+0

@ U2one,您提供的鏈接http://camel.apache.org/using-getin-or-getout-methods-on-exchange.html您在哪裏澄清了一切,謝謝 –