deason 1 year ago
parent
commit
f2c4a458f4

+ 3 - 0
src/main/java/com/qmth/exam/reserve/bean/apply/ApplyRecordCacheBean.java

@@ -11,6 +11,9 @@ public class ApplyRecordCacheBean implements IModel {
 
     private static final long serialVersionUID = 2804383719693396546L;
 
+    @ApiModelProperty(value = "考生ID")
+    private Long studentId;
+
     @ApiModelProperty(value = "考点ID")
     private Long examSiteId;
 

+ 6 - 0
src/main/java/com/qmth/exam/reserve/cache/CacheConstants.java

@@ -54,6 +54,12 @@ public interface CacheConstants {
      */
     String CACHE_STUDENT_APPLY_RECORD = "$cache:student_apply_record:%s";
 
+    /**
+     * 考生预约记录队列
+     * $queue:student_apply_record
+     */
+    String QUEUE_STUDENT_APPLY_RECORD = "$queue:student_apply_record";
+
     /**
      * 某考生预约操作锁
      * $lock:student_apply:{studentId}

+ 28 - 1
src/main/java/com/qmth/exam/reserve/cache/impl/ApplyTaskCacheService.java

@@ -12,6 +12,7 @@ import com.qmth.exam.reserve.service.StudentApplyService;
 import com.qmth.exam.reserve.service.StudentService;
 import org.apache.commons.collections4.CollectionUtils;
 import org.redisson.api.RAtomicLong;
+import org.redisson.api.RBlockingQueue;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -186,8 +187,9 @@ public class ApplyTaskCacheService implements CacheConstants {
             if (CollectionUtils.isNotEmpty(list)) {
                 for (StudentApplyEntity entity : list) {
                     ApplyRecordCacheBean bean = new ApplyRecordCacheBean();
-                    bean.setTimePeriodId(entity.getTimePeriodId());
+                    bean.setStudentId(entity.getStudentId());
                     bean.setExamSiteId(entity.getExamSiteId());
+                    bean.setTimePeriodId(entity.getTimePeriodId());
                     bean.setCancel(entity.getCancel());
                     bean.setOperateId(entity.getOperateId());
                     bean.setOperateTime(entity.getUpdateTime());
@@ -220,4 +222,29 @@ public class ApplyTaskCacheService implements CacheConstants {
         log.info("SET cacheKey:{} hashKey:{}", cacheKey, hashKey);
     }
 
+    /**
+     * 推送至考生预约记录队列
+     */
+    public void pushStudentApplyRecordQueue(ApplyRecordCacheBean value) {
+        if (value == null) {
+            return;
+        }
+
+        RBlockingQueue<ApplyRecordCacheBean> queue = redisClient.getRedissonClient()
+                .getBlockingQueue(QUEUE_STUDENT_APPLY_RECORD);
+        boolean success = queue.offer(value);
+        if (!success) {
+            throw new RuntimeException("推送至考生预约记录队列失败");
+        }
+    }
+
+    /**
+     * 获取考生预约记录队列数量
+     */
+    public int getStudentApplyRecordQueueSize() {
+        RBlockingQueue<ApplyRecordCacheBean> queue = redisClient.getRedissonClient()
+                .getBlockingQueue(QUEUE_STUDENT_APPLY_RECORD);
+        return queue.size();
+    }
+
 }

+ 50 - 0
src/main/java/com/qmth/exam/reserve/job/StudentApplyRecordJob.java

@@ -0,0 +1,50 @@
+package com.qmth.exam.reserve.job;
+
+import com.qmth.exam.reserve.bean.apply.ApplyRecordCacheBean;
+import com.qmth.exam.reserve.cache.CacheConstants;
+import com.qmth.exam.reserve.cache.RedisClient;
+import com.qmth.exam.reserve.service.StudentApplyService;
+import org.redisson.api.RBlockingQueue;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.scheduling.annotation.Scheduled;
+import org.springframework.stereotype.Component;
+
+@Component
+public class StudentApplyRecordJob {
+
+    private static final Logger log = LoggerFactory.getLogger(StudentApplyRecordJob.class);
+
+    @Autowired
+    private StudentApplyService studentApplyService;
+
+    @Autowired
+    private RedisClient redisClient;
+
+    /**
+     * 定时将“考生预约记录队列”中的数据保存至数据库
+     * 注:每N秒执行一次
+     */
+    // @Scheduled(fixedDelay = 30000, initialDelay = 30000)
+    public void saveStudentApplyRecordJob() {
+        long start = System.currentTimeMillis();
+        try {
+            RBlockingQueue<ApplyRecordCacheBean> queue = redisClient.getRedissonClient()
+                    .getBlockingQueue(CacheConstants.QUEUE_STUDENT_APPLY_RECORD);
+
+            log.info("[QUEUE] curSize:{}", queue.size());
+            while (!queue.isEmpty()) {
+                ApplyRecordCacheBean value = queue.take();
+                log.info("[QUEUE] {}_{}_{}", value.getStudentId(), value.getExamSiteId(), value.getTimePeriodId());
+                // todo
+            }
+        } catch (Exception e) {
+            log.error("[QUEUE] job err:{}", e.getMessage(), e);
+        } finally {
+            long end = System.currentTimeMillis();
+            log.info("[QUEUE] job cost {}ms", end - start);
+        }
+    }
+
+}