2016-05-23 182 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

相关问题