定时任务
This commit is contained in:
parent
5673e0b46a
commit
d7d58bde22
61
ruoyi-admin/src/main/java/com/ruoyi/web/task/RyTask.java
Normal file
61
ruoyi-admin/src/main/java/com/ruoyi/web/task/RyTask.java
Normal file
@ -0,0 +1,61 @@
|
|||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
@ -163,7 +163,7 @@ public class Constants
|
|||||||
/**
|
/**
|
||||||
* 定时任务白名单配置(仅允许访问的包名,如其他需要可以自行添加)
|
* 定时任务白名单配置(仅允许访问的包名,如其他需要可以自行添加)
|
||||||
*/
|
*/
|
||||||
public static final String[] JOB_WHITELIST_STR = { "com.ruoyi.quartz.task" };
|
public static final String[] JOB_WHITELIST_STR = { "com.ruoyi.quartz.task", "com.ruoyi.web.task" };
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 定时任务违规的字符
|
* 定时任务违规的字符
|
||||||
|
@ -34,7 +34,6 @@
|
|||||||
<groupId>com.ruoyi</groupId>
|
<groupId>com.ruoyi</groupId>
|
||||||
<artifactId>ruoyi-common</artifactId>
|
<artifactId>ruoyi-common</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
</project>
|
</project>
|
@ -1,28 +0,0 @@
|
|||||||
package com.ruoyi.quartz.task;
|
|
||||||
|
|
||||||
import org.springframework.stereotype.Component;
|
|
||||||
import com.ruoyi.common.utils.StringUtils;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 定时任务调度测试
|
|
||||||
*
|
|
||||||
* @author ruoyi
|
|
||||||
*/
|
|
||||||
@Component("ryTask")
|
|
||||||
public class RyTask
|
|
||||||
{
|
|
||||||
public void ryMultipleParams(String s, Boolean b, Long l, Double d, Integer i)
|
|
||||||
{
|
|
||||||
System.out.println(StringUtils.format("执行多参方法: 字符串类型{},布尔类型{},长整型{},浮点型{},整形{}", s, b, l, d, i));
|
|
||||||
}
|
|
||||||
|
|
||||||
public void ryParams(String params)
|
|
||||||
{
|
|
||||||
System.out.println("执行有参方法:" + params);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void ryNoParams()
|
|
||||||
{
|
|
||||||
System.out.println("执行无参方法");
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,11 +1,16 @@
|
|||||||
package com.ruoyi.system.service.impl;
|
package com.ruoyi.system.service.impl;
|
||||||
|
|
||||||
|
import java.text.SimpleDateFormat;
|
||||||
|
import java.util.Calendar;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.TimeZone;
|
||||||
|
|
||||||
|
import com.ruoyi.common.exception.job.TaskException;
|
||||||
import com.ruoyi.common.utils.DateUtils;
|
import com.ruoyi.common.utils.DateUtils;
|
||||||
import com.ruoyi.quartz.domain.SysJob;
|
import com.ruoyi.quartz.domain.SysJob;
|
||||||
import com.ruoyi.quartz.service.ISysJobService;
|
import com.ruoyi.quartz.service.ISysJobService;
|
||||||
|
import org.quartz.SchedulerException;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import com.ruoyi.system.mapper.PerformMapper;
|
import com.ruoyi.system.mapper.PerformMapper;
|
||||||
@ -74,16 +79,45 @@ public class PerformServiceImpl implements IPerformService {
|
|||||||
//未开始
|
//未开始
|
||||||
perform.setStatus("1");
|
perform.setStatus("1");
|
||||||
|
|
||||||
|
int result = performMapper.insertPerform(perform);
|
||||||
|
initJob(perform.getId(), perform.getName(), perform.getStartTime(), userName);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 任务
|
||||||
|
*/
|
||||||
|
private void initJob(Long pid, String name, Date date, String userName) throws SchedulerException, TaskException {
|
||||||
|
Calendar calendar = Calendar.getInstance();
|
||||||
|
calendar.setTime(date);
|
||||||
|
calendar.add(Calendar.HOUR_OF_DAY, -1);
|
||||||
|
// 获取年、月、日、时、分、秒
|
||||||
|
int year = calendar.get(Calendar.YEAR);
|
||||||
|
int month = calendar.get(Calendar.MONTH) + 1; // 月份从0开始,所以加1
|
||||||
|
int day = calendar.get(Calendar.DAY_OF_MONTH);
|
||||||
|
int hour = calendar.get(Calendar.HOUR_OF_DAY); // 24小时制
|
||||||
|
int minute = calendar.get(Calendar.MINUTE);
|
||||||
|
int second = calendar.get(Calendar.SECOND);
|
||||||
|
String cron = String.format("%d %d %d %d %d ? %d", second, minute, hour, day, month, year);
|
||||||
|
|
||||||
|
// 定义日期格式
|
||||||
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||||
|
|
||||||
|
// 格式化当前日期和时间
|
||||||
|
String formattedDate = sdf.format(date);
|
||||||
|
|
||||||
SysJob job = new SysJob();
|
SysJob job = new SysJob();
|
||||||
job.setJobName(perform.getName() + " " + perform.getStartTime());
|
job.setJobName(name + " " + formattedDate);
|
||||||
job.setCronExpression("* * * * * ?");
|
job.setCronExpression(cron);
|
||||||
job.setInvokeTarget("ryTask.ryParams('ry')");
|
job.setInvokeTarget("ryTask.rob(" + pid + "L)");
|
||||||
job.setJobGroup("DEFAULT");
|
job.setJobGroup("DEFAULT");
|
||||||
job.setMisfirePolicy("2");
|
job.setMisfirePolicy("3");
|
||||||
job.setStatus("0");
|
job.setStatus("0");
|
||||||
job.setCreateBy(userName);
|
job.setCreateBy(userName);
|
||||||
jobService.insertJob(job);
|
jobService.insertJob(job);
|
||||||
return performMapper.insertPerform(perform);
|
|
||||||
|
job.setInvokeTarget("ryTask.rob(" + pid + "L, " + job.getJobId() + "L)");
|
||||||
|
jobService.updateJob(job);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
Reference in New Issue
Block a user