This commit is contained in:
parent
d404ded7e0
commit
868fa5df6d
@ -273,6 +273,8 @@ public class RyTask {
|
|||||||
List<MyReq> req = new ArrayList<>();
|
List<MyReq> req = new ArrayList<>();
|
||||||
ExecutorService executorService = Executors.newFixedThreadPool(100);
|
ExecutorService executorService = Executors.newFixedThreadPool(100);
|
||||||
HttpClient client = HttpClient.newHttpClient();
|
HttpClient client = HttpClient.newHttpClient();
|
||||||
|
// 创建一个请求列表
|
||||||
|
List<CompletableFuture<String>> futures = new ArrayList<>();
|
||||||
|
|
||||||
for (Apply a : applies) {
|
for (Apply a : applies) {
|
||||||
// 准备JSON负载
|
// 准备JSON负载
|
||||||
@ -310,12 +312,20 @@ public class RyTask {
|
|||||||
// 发起异步请求,但不等待结果
|
// 发起异步请求,但不等待结果
|
||||||
CompletableFuture<String> future2 = client.sendAsync(r.getRequest(), HttpResponse.BodyHandlers.ofString())
|
CompletableFuture<String> future2 = client.sendAsync(r.getRequest(), HttpResponse.BodyHandlers.ofString())
|
||||||
.thenApplyAsync(HttpResponse::body, executorService);
|
.thenApplyAsync(HttpResponse::body, executorService);
|
||||||
future2.thenAccept(response -> {
|
futures.add(future2);
|
||||||
log.info(response);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 等待所有请求完成
|
||||||
|
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();
|
executorService.shutdown();
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user