在我定义路由的Apache Camel中,我如何并行发送两个或多个http请求并等待他们的'future'以获得进一步处理的响应,就像在Java中使用AsyncHttpClient一样?
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 :(得分: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);