代码
@Aspect
@Component
public class OnsListenerConsumeAspect {
private static final Logger logger = LoggerFactory.getLogger(OnsListenerConsumeAspect.class);
@Pointcut("execution(public * com.aliyun.openservices.ons.api.MessageListener.consume(..))")
public void consumeCut(){
}
@Before(value = "consumeCut()")
public void beforeConsume(JoinPoint joinPoint){
try {
Object[] args = joinPoint.getArgs();
Message message = (Message) args[0];
String traceId = message.getUserProperties(TraceIdentity.HEADER_TRACE_ID);
TraceIdentity identity = new TraceIdentity();
identity.setTraceId(traceId);
identity.setFrom("goods-center");
identity.setFromType("mq-consume");
MDC.put(TraceIdentity.HEADER_TRACE_ID, identity.getTraceId());
MDC.put(TraceIdentity.HEADER_TRACE_SPAN_ID, String.valueOf(identity.getTraceSpanId()));
MDC.put(TraceIdentity.HEADER_TRACE_STORE_ID, String.valueOf(identity.getTraceStoreId()));
MDC.put(TraceIdentity.HEADER_FROM, identity.getFrom());
MDC.put(TraceIdentity.HEADER_FROM_TYPE, identity.getFromType());
ThreadContext.setTraceIdentity(identity);
} catch (Throwable e) {
logger.error("OnsListenerConsumeAspect[before_failed], cause by:{}", e);
}
}
@After(value = "consumeCut()")
public void afterConsume(){
ThreadContext.removeContext();
}
问题记录
确认自己的依赖配置,创建的切面类,切入点和方法都没问题,但是切面类就是不生效,代码不执行,最后重新创建切面类文件解决。
springBoot 使用 @Aspect 不能用 idea创建Aspect类,创建完之后改为class 也不行,要删除文件重新创建class文件