【修改】 1、新增项目启动清除所有的任务ID;2、修改ws send方法参数

This commit is contained in:
DESKTOP-9ERGOBP\任维炳
2025-10-20 12:58:25 +08:00
parent ad2b0b510e
commit 02bc650c1b
3 changed files with 30 additions and 5 deletions

View File

@@ -212,7 +212,7 @@ public class AutoToolsController {
stuTheExamInfo.setNetwork(""); stuTheExamInfo.setNetwork("");
// 创建对应的线程池 // 创建对应的线程池
if (loginUser != null) { if (loginUser != null) {
taskManager.startTask(stuInTheExam, stuTheExamInfo, token, countdown, new AtomicInteger(0), loginUser.getId() + "_" + stuInTheExam.getTaskId() + "_" + stuInTheExam.getPaperId()); taskManager.startTask(loginUser.getId(), stuInTheExam, stuTheExamInfo, token, countdown, new AtomicInteger(0), loginUser.getId() + "_" + stuInTheExam.getTaskId() + "_" + stuInTheExam.getPaperId());
} }
return CommonResult.success(token); return CommonResult.success(token);
} }
@@ -321,7 +321,7 @@ public class AutoToolsController {
stuTheExamInfo.setEndStatus(1); stuTheExamInfo.setEndStatus(1);
// 返回数据-网络状态 // 返回数据-网络状态
stuTheExamInfo.setNetwork(""); stuTheExamInfo.setNetwork("");
taskManager.startTask(stuInTheExam, stuTheExamInfo, token, countdown, new AtomicInteger(0), loginUser.getId() + "_" + stuInTheExam.getTaskId() + "_" + stuInTheExam.getPaperId()); taskManager.startTask(loginUser.getId(), stuInTheExam, stuTheExamInfo, token, countdown, new AtomicInteger(0), loginUser.getId() + "_" + stuInTheExam.getTaskId() + "_" + stuInTheExam.getPaperId());
// 将数组的值进行复制 // 将数组的值进行复制
BeanUtils.copyProperties(examPaperVo, examRestartPaperVo); BeanUtils.copyProperties(examPaperVo, examRestartPaperVo);
examRestartPaperVo.setWsToken(token); examRestartPaperVo.setWsToken(token);
@@ -501,7 +501,7 @@ public class AutoToolsController {
stuTheExamInfo.setEndStatus(1); stuTheExamInfo.setEndStatus(1);
// 返回数据-网络状态 // 返回数据-网络状态
stuTheExamInfo.setNetwork(""); stuTheExamInfo.setNetwork("");
taskManager.startTask(stuInTheExam, stuTheExamInfo, token, countdown, new AtomicInteger(0), stuId + "_" + stuInTheExam.getTaskId() + "_" + paperId); taskManager.startTask(Long.valueOf(stuId), stuInTheExam, stuTheExamInfo, token, countdown, new AtomicInteger(0), stuId + "_" + stuInTheExam.getTaskId() + "_" + paperId);
// 将数组的值进行复制 // 将数组的值进行复制
BeanUtils.copyProperties(examPaperVo, examRestartPaperVo); BeanUtils.copyProperties(examPaperVo, examRestartPaperVo);
examRestartPaperVo.setWsToken(token); examRestartPaperVo.setWsToken(token);

View File

@@ -29,7 +29,7 @@ public class TaskManager {
private final Map<String, ScheduledFuture<?>> tasks = new ConcurrentHashMap<>(); private final Map<String, ScheduledFuture<?>> tasks = new ConcurrentHashMap<>();
/** 开始任务(每秒执行一次) */ /** 开始任务(每秒执行一次) */
public void startTask(StuInTheExam stuInTheExam, StuTheExamInfo stuTheExamInfo, String token, AtomicInteger countdown, AtomicInteger counter, String userId) { public void startTask(Long userIds, StuInTheExam stuInTheExam, StuTheExamInfo stuTheExamInfo, String token, AtomicInteger countdown, AtomicInteger counter, String userId) {
// 判断 token 的线程是否存在,存在则不进行任何动作 // 判断 token 的线程是否存在,存在则不进行任何动作
if (tasks.containsKey(userId)) { if (tasks.containsKey(userId)) {
log.info("任务 {} 已存在,未重复启动", userId); log.info("任务 {} 已存在,未重复启动", userId);
@@ -59,7 +59,8 @@ public class TaskManager {
} }
} }
stuTheExamInfo.setTime(formatLongDuration(remaining)); stuTheExamInfo.setTime(formatLongDuration(remaining));
webSocketSenderApi.sendObject(UserTypeEnum.ADMIN.getValue(), "InTheExam", stuTheExamInfo); webSocketSenderApi.sendObject(UserTypeEnum.ADMIN.getValue(), userIds, "InTheExam", stuTheExamInfo);
// webSocketSenderApi.sendObject(UserTypeEnum.ADMIN.getValue(), "InTheExam", stuTheExamInfo);
}); });
return scheduler.scheduleAtFixedRate(task, 0, 1, TimeUnit.SECONDS); return scheduler.scheduleAtFixedRate(task, 0, 1, TimeUnit.SECONDS);
}); });

View File

@@ -0,0 +1,24 @@
package pc.exam.pp.server.config;
import jakarta.annotation.PostConstruct;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import pc.exam.pp.module.judgement.service.TaskManager;
@Slf4j
@Component
public class TaskToStop {
@Resource
TaskManager taskManager;
@PostConstruct
public void init() {
// 清除所有的任务
taskManager.stopAll();
log.info("✅ 已经清除所有任务ID");
}
}