幸运24-雪花主键-mq-无锁化唯一主键插入

This commit is contained in:
2025-09-11 22:27:56 +08:00
parent 5562bbafc6
commit 2b2c615b28
13 changed files with 235 additions and 38 deletions

View File

@@ -24,7 +24,7 @@ public class BillTask extends BaseTask {
@Scheduled(cron = "0 */5 * * * ?")
public void retryBillQueue() {
log.info("retryBillQueue start ...");
Map<String, BillMessage> map = billMessageService.getRecordMessMap().randomEntries(10000);
Map<String, BillMessage> map = billMessageService.getRecordMessMap().randomEntries(2000);
if (CollectionUtils.isEmpty(map)) {
return;
}

View File

@@ -13,6 +13,7 @@ import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import javax.annotation.Resource;
import java.time.Duration;
@@ -56,7 +57,34 @@ public class Lucky24Task {
String val = entry.getValue();
Lucky24Message giftMessage = JSON.parseObject(val, Lucky24Message.class);
if (curTime - giftMessage.getCreateTime() > gapTime) {
lucky24MessageService.handleGiftMessage(giftMessage);
lucky24MessageService.handleMessage(giftMessage);
}
} catch (Exception e) {
log.error("retryLucky24Queue error", e);
}
});
log.info("retryLucky24Queue end ...");
}
/**
* 重新消费队列的消息
*/
@Scheduled(cron = "0 */5 * * * ?")
public void retryLucky24QueueV2() {
log.info("retryLucky24Queue start ...");
Map<String, Lucky24Message> map = lucky24MessageService.getRecordMessMap().randomEntries(2000);
if (CollectionUtils.isEmpty(map)) {
return;
}
long curTime = System.currentTimeMillis();
long gapTime = 1000 * 60 * 10; // 十分钟内没被消费
map.entrySet().parallelStream().forEach(entry -> {
try {
String messId = entry.getKey();
Lucky24Message giftMessage = entry.getValue();
if (curTime - giftMessage.getCreateTime() > gapTime) {
lucky24MessageService.handleMessageV2(giftMessage);
}
} catch (Exception e) {
log.error("retryLucky24Queue error", e);
@@ -105,10 +133,4 @@ public class Lucky24Task {
}
}
public static void main(String[] args) {
Date abc = DateTimeUtil.convertStrToDate("2024-12-18 05:00:00", DateTimeUtil.DEFAULT_DATETIME_PATTERN);
ZonedDateTime now = abc.toInstant().atZone(ZoneId.of("Asia/Riyadh"));
long zoneIdHour = Duration.between(abc.toInstant(), DateTimeUtil.converLocalDateTimeToDate(now.toLocalDateTime()).toInstant()).toHours();
System.out.println(now + "_" + DateTimeUtil.convertDate(abc, DateTimeUtil.DEFAULT_DATETIME_PATTERN) + "_" + zoneIdHour);
}
}