This commit is contained in:
parent
b987415fa2
commit
d404ded7e0
@ -273,8 +273,6 @@ 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负载
|
||||||
@ -312,20 +310,12 @@ 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);
|
||||||
futures.add(future2);
|
future2.thenAccept(response -> {
|
||||||
|
log.info(response);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// 等待所有请求完成
|
|
||||||
CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).join();
|
|
||||||
|
|
||||||
// 输出所有请求的响应(可选)
|
|
||||||
for (CompletableFuture<String> future : futures) {
|
|
||||||
try {
|
|
||||||
System.out.println(future.get());
|
|
||||||
} catch (Exception e) {
|
|
||||||
log.error(e.getMessage());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// 关闭线程池
|
// 关闭线程池
|
||||||
executorService.shutdown();
|
executorService.shutdown();
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user