【发布时间】:2022-10-15 11:24:54
【问题描述】:
我正在使用 https://github.com/AsyncHttpClient/async-http-client 发送异步 POST 请求。
例子:
try {
CompletableFuture<org.asynchttpclient.Response> whenResponse = asyncHttpClient()
.preparePost("https://some-site.com/v1/subscription1")
.setHeader("Content-Type","application/json")
.setHeader("Accept", "application/json")
.setBody(getData())
.execute()
.toCompletableFuture()
.exceptionally(t -> {
// handle error
})
.thenApply(
response -> { return response; }
);
return whenResponse.join();
} catch (Exception e) {
// handle error
}
是否可以重构它以异步将相同的数据/正文发送到多个 URL?
最好概述最有效的方法(此处可以避免循环)?
【问题讨论】:
-
@AlexR 看起来 allOf() 会起作用。但我不确定如何在一个循环中实现多个 asyncHttpClient() 。你能举个例子吗?
标签: java asynchttpclient