Browse Source

修复定时任务

wangliang 5 years ago
parent
commit
77eafd3a4e

+ 1 - 6
stmms-ms-main/src/main/java/cn/com/qmth/stmms/ms/StartRunning.java

@@ -30,11 +30,8 @@ public class StartRunning implements CommandLineRunner {
     @Autowired
     RandomUtil randomUtil;
 
-    @Resource
-    ScheduledTask scheduledTask;
-
     @Override
-    public void run(String... args) throws Exception {
+    public void run(String... args) {
         LOGGER.info("服务器启动时执行 start");
         Work work = workRepo.findByActiveTrue();
         if (Objects.isNull(work)) {
@@ -42,8 +39,6 @@ public class StartRunning implements CommandLineRunner {
         } else {
             randomUtil.getRandom(work.getId(), false);
         }
-        scheduledTask.repairRepeatSecretNumberTask();
-        scheduledTask.markTaskJob();
         LOGGER.info("服务器启动时执行 end");
     }
 }

+ 17 - 14
stmms-ms-main/src/main/java/cn/com/qmth/stmms/ms/quartz/ScheduledTask.java

@@ -16,6 +16,8 @@ import cn.com.qmth.stmms.ms.marking.config.MarkingConfig;
 import cn.com.qmth.stmms.ms.marking.service.MarkingService;
 import com.alibaba.fastjson.JSONObject;
 import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.InitializingBean;
+import org.springframework.scheduling.annotation.Async;
 import org.springframework.scheduling.annotation.Scheduled;
 import org.springframework.stereotype.Component;
 import org.springframework.transaction.annotation.Transactional;
@@ -31,7 +33,8 @@ import java.util.*;
  * @Date: 2020/1/16
  */
 @Component
-public class ScheduledTask {
+@Async
+public class ScheduledTask implements InitializingBean {
     private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(ScheduledTask.class);
 
     @Resource
@@ -65,11 +68,9 @@ public class ScheduledTask {
 
     /**
      * 修复密号重复 30秒一次
-     *
-     * @throws Exception
      */
     @Scheduled(cron = "0/30 * * * * ?")
-    public void repairRepeatSecretNumberTask() throws Exception {
+    public void repairRepeatSecretNumberTask() {
         if (Objects.nonNull(randomUtil.getRandomMap()) && randomUtil.getRandomMap().size() > 0) {
             Work work = workRepo.findByActiveTrue();
             if (Objects.nonNull(work)) {
@@ -144,12 +145,11 @@ public class ScheduledTask {
      * 修复试卷密号
      *
      * @param workId
-     * @throws Exception
      */
     @Transactional
-    public void repairRepeatPaper(Long workId) throws Exception {
+    public void repairRepeatPaper(Long workId) {
         try {
-            String sql = "select * from (select p.random_seq, count(p.random_seq) as seq from paper p where p.work_id = ? group by p.random_seq) temp where temp.seq > 1";
+            String sql = "select * from (select p.random_seq, count(p.random_seq) as seq from paper p where p.work_id = ? group by p.random_seq) temp where temp.seq > 1 LIMIT 500";
             List list = sqlUtil.execSqlForMapNative(sql, workId);
             Set papers = new HashSet();
             if (Objects.nonNull(list) && list.size() > 0) {
@@ -184,12 +184,11 @@ public class ScheduledTask {
      * 修复任务密号
      *
      * @param workId
-     * @throws Exception
      */
     @Transactional
-    public void repairRepeatTask(Long workId) throws Exception {
+    public void repairRepeatTask(Long workId) {
         try {
-            String sql = "select * from (select mt.random_seq_new, count(mt.random_seq_new) as seq from mark_task mt where mt.work_id = ? group by mt.random_seq_new) temp where temp.seq > 1";
+            String sql = "select * from (select mt.random_seq_new, count(mt.random_seq_new) as seq from mark_task mt where mt.work_id = ? group by mt.random_seq_new) temp where temp.seq > 1 LIMIT 500";
             List list = sqlUtil.execSqlForMapNative(sql, workId);
             Set tasks = new HashSet();
             if (Objects.nonNull(list) && list.size() > 0) {
@@ -226,9 +225,8 @@ public class ScheduledTask {
      * @param workId
      * @param examNumber
      * @return
-     * @throws Exception
      */
-    private Long getRandomPaper(Long workId, String examNumber) throws Exception {
+    private Long getRandomPaper(Long workId, String examNumber) {
         int count = 0, result = 0;
         Long random = 0L;
         while (true) {
@@ -255,9 +253,8 @@ public class ScheduledTask {
      * @param workId
      * @param examNumber
      * @return
-     * @throws Exception
      */
-    private Long getRandomTask(Long markerId, Long paperId, Long workId, String examNumber) throws Exception {
+    private Long getRandomTask(Long markerId, Long paperId, Long workId, String examNumber) {
         int count = 0, result = 0;
         Long random = 0L;
         while (true) {
@@ -275,4 +272,10 @@ public class ScheduledTask {
         }
         return random;
     }
+
+    @Override
+    public void afterPropertiesSet() {
+        this.repairRepeatSecretNumberTask();
+        this.markTaskJob();
+    }
 }