From 02bc650c1b0022ad488bd4d8e51aa98511734327 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?DESKTOP-9ERGOBP=5C=E4=BB=BB=E7=BB=B4=E7=82=B3?= Date: Mon, 20 Oct 2025 12:58:25 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E4=BF=AE=E6=94=B9=E3=80=91=201?= =?UTF-8?q?=E3=80=81=E6=96=B0=E5=A2=9E=E9=A1=B9=E7=9B=AE=E5=90=AF=E5=8A=A8?= =?UTF-8?q?=E6=B8=85=E9=99=A4=E6=89=80=E6=9C=89=E7=9A=84=E4=BB=BB=E5=8A=A1?= =?UTF-8?q?ID=EF=BC=9B2=E3=80=81=E4=BF=AE=E6=94=B9ws=20send=E6=96=B9?= =?UTF-8?q?=E6=B3=95=E5=8F=82=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../admin/autoTools/AutoToolsController.java | 6 ++--- .../module/judgement/service/TaskManager.java | 5 ++-- .../pc/exam/pp/server/config/TaskToStop.java | 24 +++++++++++++++++++ 3 files changed, 30 insertions(+), 5 deletions(-) create mode 100644 exam-server/src/main/java/pc/exam/pp/server/config/TaskToStop.java diff --git a/exam-module-judgement/exam-module-judgement-biz/src/main/java/pc/exam/pp/module/judgement/controller/admin/autoTools/AutoToolsController.java b/exam-module-judgement/exam-module-judgement-biz/src/main/java/pc/exam/pp/module/judgement/controller/admin/autoTools/AutoToolsController.java index dae72a38..1894ed4d 100644 --- a/exam-module-judgement/exam-module-judgement-biz/src/main/java/pc/exam/pp/module/judgement/controller/admin/autoTools/AutoToolsController.java +++ b/exam-module-judgement/exam-module-judgement-biz/src/main/java/pc/exam/pp/module/judgement/controller/admin/autoTools/AutoToolsController.java @@ -212,7 +212,7 @@ public class AutoToolsController { stuTheExamInfo.setNetwork("强"); // 创建对应的线程池 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); } @@ -321,7 +321,7 @@ public class AutoToolsController { stuTheExamInfo.setEndStatus(1); // 返回数据-网络状态 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); examRestartPaperVo.setWsToken(token); @@ -501,7 +501,7 @@ public class AutoToolsController { stuTheExamInfo.setEndStatus(1); // 返回数据-网络状态 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); examRestartPaperVo.setWsToken(token); diff --git a/exam-module-judgement/exam-module-judgement-biz/src/main/java/pc/exam/pp/module/judgement/service/TaskManager.java b/exam-module-judgement/exam-module-judgement-biz/src/main/java/pc/exam/pp/module/judgement/service/TaskManager.java index 57c2c17c..169aabb7 100644 --- a/exam-module-judgement/exam-module-judgement-biz/src/main/java/pc/exam/pp/module/judgement/service/TaskManager.java +++ b/exam-module-judgement/exam-module-judgement-biz/src/main/java/pc/exam/pp/module/judgement/service/TaskManager.java @@ -29,7 +29,7 @@ public class TaskManager { private final Map> 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 的线程是否存在,存在则不进行任何动作 if (tasks.containsKey(userId)) { log.info("任务 {} 已存在,未重复启动", userId); @@ -59,7 +59,8 @@ public class TaskManager { } } 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); }); diff --git a/exam-server/src/main/java/pc/exam/pp/server/config/TaskToStop.java b/exam-server/src/main/java/pc/exam/pp/server/config/TaskToStop.java new file mode 100644 index 00000000..e81b5737 --- /dev/null +++ b/exam-server/src/main/java/pc/exam/pp/server/config/TaskToStop.java @@ -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"); + } +}