deason 1 vuosi sitten
vanhempi
commit
d6d81c3f0a

+ 11 - 5
src/main/java/com/qmth/exam/reserve/job/StudentApplyRecordJob.java

@@ -11,6 +11,7 @@ import com.qmth.exam.reserve.enums.EventType;
 import com.qmth.exam.reserve.service.OperateLogService;
 import com.qmth.exam.reserve.service.StudentApplyService;
 import com.qmth.exam.reserve.util.JsonHelper;
+import org.redisson.api.RLock;
 import org.redisson.api.RQueue;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -18,8 +19,6 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.scheduling.annotation.Scheduled;
 import org.springframework.stereotype.Component;
 
-import java.util.concurrent.locks.Lock;
-
 @Component
 public class StudentApplyRecordJob {
 
@@ -45,8 +44,8 @@ public class StudentApplyRecordJob {
     public void saveStudentApplyRecordJob() {
         long start = System.currentTimeMillis();
 
+        RLock curLock = (RLock) concurrentService.getLock(CacheConstants.LOCK_STUDENT_APPLY_RECORD);
         try {
-            Lock curLock = concurrentService.getLock(CacheConstants.LOCK_STUDENT_APPLY_RECORD);
             if (!curLock.tryLock()) {
                 log.info("[JOB] locking...");
                 return;
@@ -69,11 +68,18 @@ public class StudentApplyRecordJob {
                     log.error("[JOB] offerQueue:{} studentId:{} err:{}", success, value.getStudentId(), e.getMessage());
                 }
             }
-
-            curLock.unlock();
         } catch (Exception e) {
             log.error("[JOB] err:{}", e.getMessage(), e);
         } finally {
+            try {
+                // 解锁前检查当前线程是否持有该锁
+                if (curLock.isLocked() && curLock.isHeldByCurrentThread()) {
+                    curLock.unlock();
+                }
+            } catch (Exception e) {
+                // ignore
+            }
+
             long end = System.currentTimeMillis();
             log.info("[JOB] cost:{}ms", end - start);
         }