幸运24-雪花主键-mq-组织do和insertIgnore拆分

This commit is contained in:
2025-09-25 11:23:21 +08:00
parent 5118107ead
commit 4f3ddab6f7
2 changed files with 26 additions and 25 deletions

View File

@@ -208,12 +208,10 @@ public class Lucky24GiftSendService {
private void sendMq(Map<Long, Lucky24Record> recordMap) {
Map<String, Lucky24Message> caches = new HashMap<>(recordMap.size());
List<Lucky24Message> messageList = new ArrayList<>();
DefaultIdentifierGenerator idGenerator = DefaultIdentifierGenerator.getInstance();
for (Lucky24Record record: recordMap.values()){
long id = identifierGenerator.nextId(null).longValue();
String messId = idGenerator.nextUUID(null);
Long id = identifierGenerator.nextId(null).longValue();
String messId = id.toString();
Lucky24Message message = new Lucky24Message();
message.setId(id);

View File

@@ -60,12 +60,18 @@ public class Lucky24MessageService implements InitializingBean {
public void handleMessage(Lucky24Message giftMessage) {
Room room = null != giftMessage.getRoomUid()? roomService.getRoomByUid(giftMessage.getRoomUid()): null;
log.info("【处理lucky24 mq】 开始处理 giftMessage: {}", JSON.toJSONString(giftMessage));
Lucky24Record record = buildLucky24Record(giftMessage);
int row = insertLucky24RecordIgnore(giftMessage, record);
if (row <= 0){
return;
}
Room room = null != giftMessage.getRoomUid() ? roomService.getRoomByUid(giftMessage.getRoomUid()) : null;
Gift gift = giftService.getGiftById(giftMessage.getGiftId());
Date createTime = new Date(giftMessage.getCreateTime());
Lucky24Record record = insertRecordIgnore(giftMessage);
// 收礼者收益
Lucky24GiftConfig config = sendService.getConfig();
Lucky24GiftConfig partitionConfig = config.getRatioByPartitionId(giftMessage.getPartitionId());
@@ -75,7 +81,7 @@ public class Lucky24MessageService implements InitializingBean {
log.info("【处理lucky24 mq】 收礼收益已发放 messId: {} incomeAllot: {}", giftMessage.getMessId(), JSON.toJSONString(receiverIncomeAllot));
// 异步报错不会触发mq重试
if (!CollectionUtils.isEmpty(config.getFollowUidList()) && config.getFollowUidList().contains(record.getUid())){
if (!CollectionUtils.isEmpty(config.getFollowUidList()) && config.getFollowUidList().contains(record.getUid())) {
robotMsgService.pushFollowUser(record.getUid(), record.getReceiverUid(), record.getRoomUid());
}
@@ -86,16 +92,11 @@ public class Lucky24MessageService implements InitializingBean {
recordMessMap.fastRemove(giftMessage.getMessId());
}
private Lucky24Record insertRecordIgnore(Lucky24Message giftMessage) {
long startTime = System.currentTimeMillis();
long getPoolTime = 0;
long insertTime = 0;
private Lucky24Record buildLucky24Record(Lucky24Message giftMessage) {
Lucky24Record record = new Lucky24Record();
if (null == giftMessage.getId()){
giftMessage.setId(identifierGenerator.nextId( null).longValue());
if (null == giftMessage.getId()) {
giftMessage.setId(identifierGenerator.nextId(null).longValue());
}
record.setId(giftMessage.getId());
@@ -110,12 +111,11 @@ public class Lucky24MessageService implements InitializingBean {
record.setPoolType(giftMessage.getPoolType());
record.setPoolId(giftMessage.getPoolId());
if (null == giftMessage.getPoolType() && null != giftMessage.getPoolId()){
if (null == giftMessage.getPoolType() && null != giftMessage.getPoolId()) {
Lucky24Pool pool = poolMapper.selectById(giftMessage.getPoolId());
if (null != pool){
if (null != pool) {
record.setPoolType(pool.getType());
}
getPoolTime = System.currentTimeMillis();
}
record.setIsSupplement(giftMessage.getIsSupplement());
@@ -125,13 +125,16 @@ public class Lucky24MessageService implements InitializingBean {
record.setCreateTime(new Date(giftMessage.getCreateTime()));
record.setStockResult(giftMessage.getStockResult());
return record;
}
private int insertLucky24RecordIgnore(Lucky24Message giftMessage, Lucky24Record record) {
long startTime = System.currentTimeMillis();
int insertRow = recordService.insertRecordIgnore(record);
long endTime = System.currentTimeMillis();
log.info("insertLucky24RecordIgnore row {} performance - getPool: {}ms, insert: {}ms, total: {}ms",
log.info("insertLucky24RecordIgnore row {} performance - total: {}ms",
insertRow,
getPoolTime - startTime,
insertTime - getPoolTime,
endTime - startTime);
if (insertRow > 0){
@@ -139,7 +142,7 @@ public class Lucky24MessageService implements InitializingBean {
giftMessage.getMessId(), record.getId(), JSON.toJSONString(record));
}
return record;
return insertRow;
}
@Override
@@ -147,4 +150,4 @@ public class Lucky24MessageService implements InitializingBean {
recordMessMap = redissonClient.getMap(RedisKey.lucky_24_record_message.getKey());
}
}
}