【修改】 修改考试剩余时间异常闪动问题

This commit is contained in:
DESKTOP-9ERGOBP\任维炳
2025-10-20 11:02:57 +08:00
parent 4095ef88f9
commit c708a30443
2 changed files with 24 additions and 8 deletions

View File

@@ -157,11 +157,9 @@ public class AutoToolsController {
securityProperties.getTokenHeader(), securityProperties.getTokenParameter()); securityProperties.getTokenHeader(), securityProperties.getTokenParameter());
// 获取登录用户 // 获取登录用户
LoginUser loginUser = SecurityFrameworkUtils.getLoginUser(); LoginUser loginUser = SecurityFrameworkUtils.getLoginUser();
// userId 是否存在 // 通过ID 直接停止
String wsKey = taskManager.getTaskById(String.valueOf(loginUser.getId())); if (loginUser != null) {
if (wsKey != null) { taskManager.stopTaskByUserId(String.valueOf(loginUser.getId()));
// 停止任务
taskManager.stopTask(wsKey);
} }
int startTime = 0; int startTime = 0;
// 查找对应的task // 查找对应的task
@@ -213,7 +211,9 @@ public class AutoToolsController {
// 返回数据-网络状态 // 返回数据-网络状态
stuTheExamInfo.setNetwork(""); stuTheExamInfo.setNetwork("");
// 创建对应的线程池 // 创建对应的线程池
if (loginUser != null) {
taskManager.startTask(stuInTheExam, stuTheExamInfo, token, countdown, new AtomicInteger(0), loginUser.getId() + "_" + stuInTheExam.getTaskId() + "_" + stuInTheExam.getPaperId()); taskManager.startTask(stuInTheExam, stuTheExamInfo, token, countdown, new AtomicInteger(0), loginUser.getId() + "_" + stuInTheExam.getTaskId() + "_" + stuInTheExam.getPaperId());
}
return CommonResult.success(token); return CommonResult.success(token);
} }
return CommonResult.success("未登录"); return CommonResult.success("未登录");
@@ -261,8 +261,10 @@ public class AutoToolsController {
securityProperties.getTokenHeader(), securityProperties.getTokenParameter()); securityProperties.getTokenHeader(), securityProperties.getTokenParameter());
ExamRestartPaperVo examRestartPaperVo = new ExamRestartPaperVo(); ExamRestartPaperVo examRestartPaperVo = new ExamRestartPaperVo();
// 如果继续考试需要关闭之前的重新开始ws // 如果继续考试需要关闭之前的重新开始ws
String taskManagerId = loginUser.getId() + "_" + stuInTheExam.getTaskId() + "_" + stuInTheExam.getPaperId(); // 通过ID 直接停止
taskManager.stopTask(taskManagerId); if (loginUser != null) {
taskManager.stopTaskByUserId(String.valueOf(loginUser.getId()));
}
// 获取试卷详情 // 获取试卷详情
ExamPaperVo examPaperVo = educationPaperQuService.selectPaperQuListByPaperId(stuInTheExam.getPaperId()); ExamPaperVo examPaperVo = educationPaperQuService.selectPaperQuListByPaperId(stuInTheExam.getPaperId());
// 获取剩余的时间 // 获取剩余的时间
@@ -353,6 +355,12 @@ public class AutoToolsController {
userId += "_" + taskId + "_" + paperId; userId += "_" + taskId + "_" + paperId;
// 删除对应的线程池 // 删除对应的线程池
taskManager.stopTask(userId); taskManager.stopTask(userId);
// 为了确保停止成功,笨办法在请求另一个停止接口
try {
taskManager.stopTaskByUserId(String.valueOf(userId));
} catch (Exception e) {
e.printStackTrace();
}
return CommonResult.success(true); return CommonResult.success(true);
} }

View File

@@ -93,6 +93,14 @@ public class TaskManager {
.orElse(null); .orElse(null);
} }
/**通过ID结束任务*/
public void stopTaskByUserId(String userId) {
tasks.keySet().stream()
.filter(key -> key.startsWith(userId + "_"))
.findFirst().ifPresent(this::stopTask);
}
/** 全部停止(可选) */ /** 全部停止(可选) */
public void stopAll() { public void stopAll() {
tasks.forEach((k, f) -> f.cancel(false)); tasks.forEach((k, f) -> f.cancel(false));