Просмотр исходного кода

自动交卷,添加全局锁

lideyin 5 лет назад
Родитель
Сommit
9598e9a466

+ 31 - 20
examcloud-core-oe-task-service/src/main/java/cn/com/qmth/examcloud/core/oe/task/service/pipeline/HandInExamExecutor.java

@@ -7,9 +7,11 @@ import cn.com.qmth.examcloud.commons.helpers.pipeline.TaskContext;
 import cn.com.qmth.examcloud.core.oe.task.service.ExamRecordDataService;
 import cn.com.qmth.examcloud.core.oe.task.service.ExamingSessionService;
 import cn.com.qmth.examcloud.examwork.api.ExamStudentCloudService;
+import cn.com.qmth.examcloud.support.Constants;
 import cn.com.qmth.examcloud.support.enums.ExamRecordStatus;
 import cn.com.qmth.examcloud.support.examing.ExamRecordData;
 import cn.com.qmth.examcloud.support.examing.ExamingSession;
+import cn.com.qmth.examcloud.web.helpers.SequenceLockHelper;
 import com.google.common.collect.Lists;
 import com.sun.org.apache.xpath.internal.operations.Bool;
 import org.apache.commons.io.FileUtils;
@@ -40,34 +42,43 @@ public class HandInExamExecutor implements NodeExecuter<Long, ExamRecordData, Lo
     @Override
     public List<KeyValuePair<Long, ExamRecordData>> execute(Long key, ExamRecordData examRecordData, TaskContext context) throws Exception {
 
-        List<KeyValuePair<Long, ExamRecordData>> resultList = new ArrayList<>();
-        KeyValuePair<Long, ExamRecordData> keyValuePair = new KeyValuePair<>(key, examRecordData);
+        String sequenceLockKey = Constants.EXAM_CONTROL_LOCK_PREFIX + examRecordData.getStudentId();
 
-        //处理正在进行中的考试
-        if (examRecordData.getExamRecordStatus() == ExamRecordStatus.EXAM_ING) {
-            ExamingSession examingSession = examingSessionService.getExamingSession(examRecordData.getStudentId());
+        try {
+            //添加考试控制全局锁
+            SequenceLockHelper.getLock(sequenceLockKey);
 
-            // 如果考试会话不存在/超过考试时间/超过断点续考时间,自动交卷
-            if (null == examingSession || isOverExamTime(examingSession) || isOverBreakpointTime(examingSession)) {
-                Date now = new Date();
+            List<KeyValuePair<Long, ExamRecordData>> resultList = new ArrayList<>();
+            KeyValuePair<Long, ExamRecordData> keyValuePair = new KeyValuePair<>(key, examRecordData);
 
-                //更改内存中的交卷状态
-                examRecordData.setExamRecordStatus(ExamRecordStatus.EXAM_AUTO_HAND_IN);
-                examRecordData.setCleanTime(now);
+            //处理正在进行中的考试
+            if (examRecordData.getExamRecordStatus() == ExamRecordStatus.EXAM_ING) {
+                ExamingSession examingSession = examingSessionService.getExamingSession(examRecordData.getStudentId());
 
-                examRecordDataService.saveExamRecordDataCache(examRecordData.getId(), examRecordData);
+                // 如果考试会话不存在/超过考试时间/超过断点续考时间,自动交卷
+                if (null == examingSession || isOverExamTime(examingSession) || isOverBreakpointTime(examingSession)) {
+                    Date now = new Date();
 
-                keyValuePair.setValue(examRecordData);
-                resultList.add(keyValuePair);
+                    //更改内存中的交卷状态
+                    examRecordData.setExamRecordStatus(ExamRecordStatus.EXAM_AUTO_HAND_IN);
+                    examRecordData.setCleanTime(now);
 
-                // 删除redis会话
-                examingSessionService.deleteExamingSession(examRecordData.getStudentId());
-                return resultList;
+                    examRecordDataService.saveExamRecordDataCache(examRecordData.getId(), examRecordData);
+
+                    keyValuePair.setValue(examRecordData);
+                    resultList.add(keyValuePair);
+
+                    // 删除redis会话
+                    examingSessionService.deleteExamingSession(examRecordData.getStudentId());
+                    return resultList;
+                }
             }
-        }
 
-        resultList.add(keyValuePair);
-        return resultList;
+            resultList.add(keyValuePair);
+            return resultList;
+        } finally {
+            SequenceLockHelper.releaseLock(sequenceLockKey);
+        }
     }
 
     /**