62 lines
1.8 KiB
Java
62 lines
1.8 KiB
Java
![]() |
package com.ruoyi.web.task;
|
||
|
|
||
|
import com.ruoyi.common.utils.StringUtils;
|
||
|
import com.ruoyi.quartz.domain.SysJob;
|
||
|
import com.ruoyi.quartz.service.ISysJobService;
|
||
|
import org.quartz.SchedulerException;
|
||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||
|
import org.springframework.stereotype.Component;
|
||
|
|
||
|
import java.net.URI;
|
||
|
import java.net.http.HttpClient;
|
||
|
import java.net.http.HttpRequest;
|
||
|
import java.net.http.HttpResponse;
|
||
|
import java.util.concurrent.CompletableFuture;
|
||
|
|
||
|
/**
|
||
|
* 定时任务调度测试
|
||
|
*
|
||
|
* @author ruoyi
|
||
|
*/
|
||
|
@Component("ryTask")
|
||
|
public class RyTask {
|
||
|
@Autowired
|
||
|
private ISysJobService jobService;
|
||
|
|
||
|
/**
|
||
|
* 抢票
|
||
|
*
|
||
|
* @param pid
|
||
|
*/
|
||
|
public void rob(Long pid, Long jobId) throws SchedulerException {
|
||
|
System.out.println(pid + "," + jobId);
|
||
|
|
||
|
for (int i = 0; i < 100; i++) {
|
||
|
System.out.println("开始");
|
||
|
HttpClient client = HttpClient.newHttpClient();
|
||
|
HttpRequest request = HttpRequest.newBuilder()
|
||
|
.uri(URI.create("http://example.com"))
|
||
|
.GET()
|
||
|
.build();
|
||
|
// 发起异步请求,但不等待结果
|
||
|
CompletableFuture<HttpResponse<String>> future2 = client.sendAsync(request, HttpResponse.BodyHandlers.ofString());
|
||
|
future2.thenAccept(response -> {
|
||
|
// 处理响应(例如,记录日志)
|
||
|
System.out.println("Response received: " + response.statusCode());
|
||
|
}).exceptionally(ex -> {
|
||
|
// 处理异常
|
||
|
ex.printStackTrace();
|
||
|
return null;
|
||
|
});
|
||
|
System.out.println("异步任务完成");
|
||
|
}
|
||
|
|
||
|
|
||
|
//执行完成停止任务
|
||
|
SysJob job = new SysJob();
|
||
|
job.setJobId(jobId);
|
||
|
job.setStatus("1");
|
||
|
jobService.changeStatus(job);
|
||
|
}
|
||
|
}
|