2016-05-23 184 views
1

在我正在定義路由的Apache Camel中,如何並行發送兩個或多個http請求,並等待他們的'期貨'以獲取進一步處理的響應,如使用AsyncHttpClient進行Java處理?Apache Camel:如何並行發送兩個http請求並等待響應?

AsyncHttpClient asyncHttpClient = new DefaultAsyncHttpClient(); 
Future<Response> f = asyncHttpClient.prepareGet("http://www.example.com/").execute(); 
Response r = f.get(); 

僅用於上下文,以下路由調用GET聯繫人http調用並同步返回響應。

from("direct:getContact") 
.to("http://host:port/contacts/1453") 
+0

我相信你正在尋找的駱駝異步庫信息:http://camel.apache.org/async.html –

+0

感謝馬修, 我嘗試了異步API與ProducerTemplate並面臨一些困難。你能否看看我是否做得對嗎? http://stackoverflow.com/questions/37409460/apache-camel-producertemplate-not-unmarshalling-the-response – ndsurendra

回答

1

嘗試分割您路由到許多較小的路線。然後你可以在那裏執行必要的解組。

question about unmarshalling http response

from("direct:getContact") 
    .process(new Processor() { 
     @Override 
     public void process(Exchange exchange) throws Exception { 
      CamelContext context = exchange.getContext(); 
      ProducerTemplate producerTemplate = context.createProducerTemplate(); 

      // Asynchronous call to internal route 
      Future<Contact> contact = 
       producerTemplate.asyncRequestBody("direct:invokeSomeRestApi", null, Contact.class); 

      // Do rest of the work 
      exchange.getOut().setBody(contact.get()); 
     } 
    }); 

// Unmarshalling REST response 
JacksonDataFormat jacksonDataFormat = new JacksonDataFormat(); 
jacksonDataFormat.setUnmarshalType(Contact.class); 

// Internal route definition 
from("direct:invokeSomeRestApi") 
    .to("http://localhost:8080/api/contact/2345") 
    .unmarshal(jacksonDataFormat); 
+0

謝謝拉法爾!發現。 – ndsurendra

相關問題