httpclient 复用
All checks were successful
continuous-integration/drone Build is passing

This commit is contained in:
liukang 2025-03-05 00:13:22 +08:00
parent d404ded7e0
commit 868fa5df6d

View File

@ -273,6 +273,8 @@ public class RyTask {
List<MyReq> req = new ArrayList<>();
ExecutorService executorService = Executors.newFixedThreadPool(100);
HttpClient client = HttpClient.newHttpClient();
// 创建一个请求列表
List<CompletableFuture<String>> futures = new ArrayList<>();
for (Apply a : applies) {
// 准备JSON负载
@ -310,12 +312,20 @@ public class RyTask {
// 发起异步请求但不等待结果
CompletableFuture<String> future2 = client.sendAsync(r.getRequest(), HttpResponse.BodyHandlers.ofString())
.thenApplyAsync(HttpResponse::body, executorService);
future2.thenAccept(response -> {
log.info(response);
});
futures.add(future2);
}
// 等待所有请求完成
CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).join();
// 输出所有请求的响应可选
for (CompletableFuture<String> future : futures) {
try {
log.info(future.get());
} catch (Exception e) {
log.error(e.getMessage());
}
}
// 关闭线程池
executorService.shutdown();