bravo-定时任务
This commit is contained in:
@@ -0,0 +1,106 @@
|
||||
package com.accompany.scheduler.task.luckyBag;
|
||||
|
||||
import com.accompany.business.message.BravoMessage;
|
||||
import com.accompany.business.message.Lucky24Message;
|
||||
import com.accompany.business.service.gift.BravoMessageService;
|
||||
import com.accompany.business.service.lucky.BravoRecordService;
|
||||
import com.accompany.common.redis.RedisKey;
|
||||
import com.accompany.common.utils.DateTimeUtil;
|
||||
import com.accompany.core.model.PartitionInfo;
|
||||
import com.accompany.core.service.common.JedisService;
|
||||
import com.accompany.core.service.partition.PartitionInfoService;
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.time.Duration;
|
||||
import java.time.ZoneId;
|
||||
import java.time.ZonedDateTime;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ThreadPoolExecutor;
|
||||
|
||||
@Component
|
||||
@Slf4j
|
||||
public class BravoTask {
|
||||
|
||||
@Autowired
|
||||
private PartitionInfoService partitionInfoService;
|
||||
@Autowired
|
||||
private BravoRecordService service;
|
||||
@Resource(name = "bizExecutor")
|
||||
private ThreadPoolExecutor bizExecutor;
|
||||
|
||||
@Autowired
|
||||
private JedisService jedisService;
|
||||
@Autowired
|
||||
private BravoMessageService bravoMessageService;
|
||||
|
||||
/**
|
||||
* 重新消费队列的消息
|
||||
*/
|
||||
@Scheduled(cron = "0 */5 * * * ?")
|
||||
public void retryBravoQueue() {
|
||||
log.info("retryBravoQueue start ...");
|
||||
Map<String, String> map = jedisService.hgetAll(RedisKey.lucky_24_status.getKey());
|
||||
if (map == null || map.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
long curTime = System.currentTimeMillis();
|
||||
long gapTime = 1000 * 60 * 10; // 十分钟内没被消费
|
||||
|
||||
map.entrySet().parallelStream().forEach(entry -> {
|
||||
try {
|
||||
String messId = entry.getKey();
|
||||
String val = entry.getValue();
|
||||
BravoMessage giftMessage = JSON.parseObject(val, BravoMessage.class);
|
||||
if (curTime - giftMessage.getCreateTime() > gapTime) {
|
||||
bravoMessageService.handleGiftMessage(giftMessage);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("retryBravoQueue error", e);
|
||||
}
|
||||
});
|
||||
log.info("retryBravoQueue end ...");
|
||||
}
|
||||
|
||||
@Scheduled(cron = "0 2 * * * ? ")
|
||||
public void bravoRecordStat() {
|
||||
Date now = new Date();
|
||||
List<PartitionInfo> partitionInfoList = partitionInfoService.listAll();
|
||||
for (PartitionInfo partitionInfo : partitionInfoList) {
|
||||
ZonedDateTime zdt = DateTimeUtil.convertWithZoneId(now, partitionInfo.getZoneId());
|
||||
ZonedDateTime hourAgo = zdt.minusHours(1L);
|
||||
log.info("[BravoRecordStat] zdt {} hourAgo {}, zdtDay {} hourAgoDay {}",
|
||||
zdt, hourAgo, zdt.getDayOfYear(), hourAgo.getDayOfWeek());
|
||||
if (zdt.getDayOfYear() == hourAgo.getDayOfYear()){
|
||||
continue;
|
||||
}
|
||||
bizExecutor.execute(() -> {
|
||||
// 获取当天的第一秒
|
||||
ZonedDateTime startOfDay = hourAgo.withHour(0)
|
||||
.withMinute(0)
|
||||
.withSecond(0)
|
||||
.withNano(0);
|
||||
Date systemStartTime = DateTimeUtil.converLocalDateTimeToDate(startOfDay.withZoneSameInstant(ZoneId.systemDefault()).toLocalDateTime());
|
||||
Date startTime = DateTimeUtil.converLocalDateTimeToDate(startOfDay.toLocalDateTime());
|
||||
|
||||
long zoneIdHour = Duration.between(systemStartTime.toInstant(), startTime.toInstant()).toHours();
|
||||
|
||||
// 获取当天的最后一秒
|
||||
ZonedDateTime endOfDay = hourAgo.withHour(23)
|
||||
.withMinute(59)
|
||||
.withSecond(59)
|
||||
.withNano(999999999);
|
||||
Date systemEndTime = DateTimeUtil.converLocalDateTimeToDate(endOfDay.withZoneSameInstant(ZoneId.systemDefault()).toLocalDateTime());
|
||||
|
||||
service.statDate(partitionInfo.getId(), systemStartTime, systemEndTime, zoneIdHour);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user