Commit e245a591 authored by 王亚雷's avatar 王亚雷

修复话题通知定时任务重复运行bug

parent 3c34fb02
package com.tanpu.community.manager; package com.tanpu.community.manager;
import com.tanpu.common.redis.RedisHelper;
import com.tanpu.common.uuid.UuidGenHelper;
import com.tanpu.community.service.*; import com.tanpu.community.service.*;
import com.tanpu.community.service.quartz.TopicReportService; import com.tanpu.community.service.quartz.TopicReportService;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Scheduled; import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.time.Duration;
@Slf4j @Slf4j
@Service @Service
@Configuration @Configuration
...@@ -31,6 +37,12 @@ public class ConJobManager { ...@@ -31,6 +37,12 @@ public class ConJobManager {
@Autowired @Autowired
private TopicReportService topicReportService; private TopicReportService topicReportService;
@Resource
private RedisHelper redisHelper;
@Resource
private UuidGenHelper uuidGenHelper;
/** /**
* 定时统计 话题 访问数据,并刷到redis * 定时统计 话题 访问数据,并刷到redis
*/ */
...@@ -72,8 +84,23 @@ public class ConJobManager { ...@@ -72,8 +84,23 @@ public class ConJobManager {
*/ */
@Scheduled(cron = "0 30 9 ? * *") @Scheduled(cron = "0 30 9 ? * *")
public void reportTopicWeekday() { public void reportTopicWeekday() {
log.info("reportTopicWeekday start"); String key = "community:lock:reportTopicWeekday";
topicReportService.reportTopicWeekday(); String uuid = uuidGenHelper.getUuidStr();
Boolean locked = null;
try {
locked = redisHelper.setIfAbsent(key, uuid, Duration.ofSeconds(60));
if (locked) {
log.info("reportTopicWeekday start");
topicReportService.reportTopicWeekday();
}
} finally {
if (Boolean.TRUE.equals(locked)) {
String value = redisHelper.get(key);
if (StringUtils.equals(uuid, value)) {
redisHelper.delete(key);
}
}
}
} }
/** /**
...@@ -81,7 +108,22 @@ public class ConJobManager { ...@@ -81,7 +108,22 @@ public class ConJobManager {
*/ */
@Scheduled(cron = "0 0 9 ? * SAT") @Scheduled(cron = "0 0 9 ? * SAT")
public void reportTopicSaturday() { public void reportTopicSaturday() {
log.info("reportTopicSaturday start"); String key = "community:lock:reportTopicSaturday";
topicReportService.reportTopicSaturday(); String uuid = uuidGenHelper.getUuidStr();
Boolean locked = null;
try {
locked = redisHelper.setIfAbsent(key, uuid, Duration.ofSeconds(60));
if (locked) {
log.info("reportTopicSaturday start");
topicReportService.reportTopicSaturday();
}
} finally {
if (Boolean.TRUE.equals(locked)) {
String value = redisHelper.get(key);
if (StringUtils.equals(uuid, value)) {
redisHelper.delete(key);
}
}
}
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment