fix:本地消息表优化

This commit is contained in:
zhongzb
2023-10-03 19:25:42 +08:00
parent e891167b6b
commit 282134fd88
7 changed files with 48 additions and 7 deletions

View File

@@ -1,8 +1,10 @@
package com.abin.mallchat.transaction.aspect;
import cn.hutool.core.date.DateUtil;
import com.abin.mallchat.transaction.annotation.SecureInvoke;
import com.abin.mallchat.transaction.domain.dto.SecureInvokeDTO;
import com.abin.mallchat.transaction.domain.entity.SecureInvokeRecord;
import com.abin.mallchat.transaction.service.SecureInvokeHolder;
import com.abin.mallchat.transaction.service.SecureInvokeService;
import com.abin.mallchat.utils.JsonUtils;
import lombok.extern.slf4j.Slf4j;
@@ -17,6 +19,7 @@ import org.springframework.stereotype.Component;
import org.springframework.transaction.support.TransactionSynchronizationManager;
import java.lang.reflect.Method;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@@ -39,7 +42,7 @@ public class SecureInvokeAspect {
boolean async = secureInvoke.async();
boolean inTransaction = TransactionSynchronizationManager.isActualTransactionActive();
//非事务状态,直接执行,不做任何保证。
if (!inTransaction) {
if (SecureInvokeHolder.isInvoking() || !inTransaction) {
return joinPoint.proceed();
}
Method method = ((MethodSignature) joinPoint.getSignature()).getMethod();
@@ -53,6 +56,7 @@ public class SecureInvokeAspect {
SecureInvokeRecord record = SecureInvokeRecord.builder()
.secureInvokeDTO(dto)
.maxRetryTimes(secureInvoke.maxRetryTimes())
.nextRetryTime(DateUtil.offsetMinute(new Date(), (int) SecureInvokeService.RETRY_INTERVAL_MINUTES))
.build();
secureInvokeService.invoke(record, async);
return null;

View File

@@ -0,0 +1,24 @@
package com.abin.mallchat.transaction.service;
import java.util.Objects;
/**
* Description:
* Author: <a href="https://github.com/zongzibinbin">abin</a>
* Date: 2023-10-02
*/
public class SecureInvokeHolder {
private static final ThreadLocal<Boolean> INVOKE_THREAD_LOCAL = new ThreadLocal<>();
public static boolean isInvoking() {
return Objects.nonNull(INVOKE_THREAD_LOCAL.get());
}
public static void setInvoking() {
INVOKE_THREAD_LOCAL.set(Boolean.TRUE);
}
public static void invoked() {
INVOKE_THREAD_LOCAL.remove();
}
}

View File

@@ -104,6 +104,7 @@ public class SecureInvokeService {
public void doInvoke(SecureInvokeRecord record) {
SecureInvokeDTO secureInvokeDTO = record.getSecureInvokeDTO();
try {
SecureInvokeHolder.setInvoking();
Class<?> beanClass = Class.forName(secureInvokeDTO.getClassName());
Object bean = SpringUtil.getBean(beanClass);
List<String> parameterStrings = JsonUtils.toList(secureInvokeDTO.getParameterTypes(), String.class);
@@ -118,6 +119,8 @@ public class SecureInvokeService {
log.error("SecureInvokeService invoke fail", e);
//执行失败,等待下次执行
retryRecord(record, e.getMessage());
} finally {
SecureInvokeHolder.invoked();
}
}