添加异常日志记录方法

This commit is contained in:
2023-08-18 11:26:14 +08:00
parent 9332b68f3f
commit 717ab75837
2 changed files with 18 additions and 3 deletions

View File

@@ -32,5 +32,11 @@ public class EnhanceMessageConstant {
public final String RETRY_PREFIX = "RETRY_"; public final String RETRY_PREFIX = "RETRY_";
//单次异常失败
public final String IDENTITY_SINGLE = "IDENTITY_SINGLE";
//多次重试后,异常记录
public final String IDENTITY_RETRY = "IDENTITY_RETRY";
} }

View File

@@ -52,8 +52,9 @@ public abstract class EnhanceConsumerMessageHandler<T extends BaseMessage> {
* *
* @param message 待处理消息 * @param message 待处理消息
*/ */
protected abstract void handleMaxRetriesExceeded(T message); protected void handleMaxRetriesExceeded(T message) {
saveExceptionMsgLog(message,EnhanceMessageConstant.IDENTITY_RETRY);
}
/** /**
* 是否需要根据业务规则过滤消息,去重逻辑可以在此处处理 * 是否需要根据业务规则过滤消息,去重逻辑可以在此处处理
@@ -97,6 +98,14 @@ public abstract class EnhanceConsumerMessageHandler<T extends BaseMessage> {
return DELAY_LEVEL; return DELAY_LEVEL;
} }
/**
* 发生异常时,进行错误信息入库保存
* 默认没有实现类子类可以实现该方法调用feign接口进行入库保存
*/
protected void saveExceptionMsgLog(T message, String identity) {
}
/** /**
* 使用模板模式构建消息消费框架,可自由扩展或删减 * 使用模板模式构建消息消费框架,可自由扩展或删减
*/ */
@@ -118,7 +127,7 @@ public abstract class EnhanceConsumerMessageHandler<T extends BaseMessage> {
long costTime = System.currentTimeMillis() - now; long costTime = System.currentTimeMillis() - now;
log.info("消息{}消费成功,耗时[{}ms]", message.getKey(), costTime); log.info("消息{}消费成功,耗时[{}ms]", message.getKey(), costTime);
} catch (Exception e) { } catch (Exception e) {
log.error("消息{}消费异常", message.getKey(), e); saveExceptionMsgLog(message,EnhanceMessageConstant.IDENTITY_SINGLE);
// 是捕获异常还是抛出,由子类决定 // 是捕获异常还是抛出,由子类决定
if (throwException()) { if (throwException()) {
//抛出异常由DefaultMessageListenerConcurrently类处理 //抛出异常由DefaultMessageListenerConcurrently类处理