This commit is contained in:
parent
5702612323
commit
b987415fa2
@ -24,6 +24,8 @@ import java.net.http.HttpResponse;
|
|||||||
import java.time.Duration;
|
import java.time.Duration;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.concurrent.CompletableFuture;
|
import java.util.concurrent.CompletableFuture;
|
||||||
|
import java.util.concurrent.ExecutorService;
|
||||||
|
import java.util.concurrent.Executors;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 定时任务调度测试
|
* 定时任务调度测试
|
||||||
@ -269,7 +271,10 @@ public class RyTask {
|
|||||||
|
|
||||||
|
|
||||||
List<MyReq> req = new ArrayList<>();
|
List<MyReq> req = new ArrayList<>();
|
||||||
|
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负载
|
||||||
@ -303,21 +308,26 @@ public class RyTask {
|
|||||||
Thread.sleep(start - now - perform.getDelayTime());
|
Thread.sleep(start - now - perform.getDelayTime());
|
||||||
log.info("抢票开始了");
|
log.info("抢票开始了");
|
||||||
for (MyReq r : req) {
|
for (MyReq r : req) {
|
||||||
// 发起异步请求,但不等待结果
|
|
||||||
CompletableFuture<HttpResponse<String>> future2 = client.sendAsync(r.getRequest(), HttpResponse.BodyHandlers.ofString());
|
|
||||||
log.info("【用户】:" + r.getUser());
|
log.info("【用户】:" + r.getUser());
|
||||||
future2.thenAccept(response -> {
|
// 发起异步请求,但不等待结果
|
||||||
// 处理响应(例如,记录日志)
|
CompletableFuture<String> future2 = client.sendAsync(r.getRequest(), HttpResponse.BodyHandlers.ofString())
|
||||||
log.info("Response received: " + response.statusCode());
|
.thenApplyAsync(HttpResponse::body, executorService);
|
||||||
log.info(response.body());
|
futures.add(future2);
|
||||||
}).exceptionally(ex -> {
|
|
||||||
// 处理异常
|
|
||||||
ex.printStackTrace();
|
|
||||||
return null;
|
|
||||||
});
|
|
||||||
log.info("测试");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 等待所有请求完成
|
||||||
|
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();
|
||||||
|
|
||||||
//status
|
//status
|
||||||
Perform pStatus2 = new Perform();
|
Perform pStatus2 = new Perform();
|
||||||
|
Loading…
Reference in New Issue
Block a user