gpt4 book ai didi

java - Apache Camel : How to send two http requests in parallel and wait for the responses?

转载 作者:行者123 更新时间:2023-11-30 10:44:29 25 4
gpt4 key购买 nike

在我定义路由的 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")

最佳答案

尝试将您的路线分成许多更小的路线。然后您可以在那里执行必要的解码。

参见 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);

关于java - Apache Camel : How to send two http requests in parallel and wait for the responses?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37388376/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com