|
|
@@ -0,0 +1,83 @@
|
|
|
+package beilv.competition.task;
|
|
|
+
|
|
|
+import beilv.competition.domain.Competition;
|
|
|
+import beilv.competition.domain.RedisTask;
|
|
|
+import beilv.competition.mapper.CompetitionMapper;
|
|
|
+import beilv.stadium.domain.Stadium;
|
|
|
+import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.data.redis.core.StringRedisTemplate;
|
|
|
+import org.springframework.scheduling.annotation.Scheduled;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+
|
|
|
+import javax.annotation.Resource;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.Set;
|
|
|
+
|
|
|
+import static beilv.competition.domain.Constant.REDIS_CHANNEL;
|
|
|
+import static beilv.competition.domain.Constant.TASK_QUEUE_QUERY;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 轮询Redis有序集合, 处理到期的任务.
|
|
|
+ * 将到期的任务发布, 由监听者接收
|
|
|
+ * 同时移除其他相同eventId的任务
|
|
|
+ */
|
|
|
+
|
|
|
+@Component
|
|
|
+public class RedisTaskChecker {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private StringRedisTemplate stringRedisTemplate;
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private CompetitionMapper competitionMapper;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 检测到期任务, 并将消息发布
|
|
|
+ */
|
|
|
+ @Scheduled(fixedRate = 1000)
|
|
|
+ public void checkTasks() {
|
|
|
+ long currentTime = System.currentTimeMillis() / 1000;
|
|
|
+
|
|
|
+ // 获取所有有序集合的键
|
|
|
+ Set<String> keys = stringRedisTemplate.keys(TASK_QUEUE_QUERY);
|
|
|
+ if (keys != null && !keys.isEmpty()) {
|
|
|
+ for (String key : keys) {
|
|
|
+ // 获取当前时间之前的所有任务
|
|
|
+ Set<String> tasks = stringRedisTemplate.opsForZSet().rangeByScore(key, 0, currentTime);
|
|
|
+ if (tasks != null && !tasks.isEmpty()) {
|
|
|
+ for (String taskJson : tasks) {
|
|
|
+ try {
|
|
|
+// System.out.println("taskJson => " + taskJson);
|
|
|
+
|
|
|
+ //获取任务信息
|
|
|
+ ObjectMapper objectMapper = new ObjectMapper();
|
|
|
+ RedisTask task = objectMapper.readValue(taskJson, RedisTask.class);
|
|
|
+
|
|
|
+ Competition competition = new Competition();
|
|
|
+ competition.setId(task.getCompetitionId());
|
|
|
+ competition.setCompetitionState(task.getCompetitionState());
|
|
|
+ competitionMapper.updateCompetition(competition);
|
|
|
+
|
|
|
+ //核验当前赛事下已支付的订单
|
|
|
+ Stadium stadium = new Stadium();
|
|
|
+ stadium.setCompetitionId(String.valueOf(task.getCompetitionId()));
|
|
|
+ stadium.setPaymentStatus(task.getPaymentStatus());
|
|
|
+ stadium.setVerificationTime(new Date());
|
|
|
+ competitionMapper.updateBookARace(stadium);
|
|
|
+
|
|
|
+
|
|
|
+ // 移除当前已发布的任务
|
|
|
+ stringRedisTemplate.opsForZSet().remove(key, taskJson);
|
|
|
+ System.out.println("Removed task: " + taskJson);
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+
|