package com.tzy.aop;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.MDC;
import org.springframework.stereotype.Component;

import java.util.UUID;

/**
 * 自定义aop
 *
 * @author Administrator
 *
 */
@Component
@Aspect
@Slf4j
public class MqConsumerAOP {

	private static final String TRACE_ID = "TRACE_ID";

	@Pointcut("@annotation(org.springframework.amqp.rabbit.annotation.RabbitListener)")
	public void mqPointCut() {
	}

	@Before("mqPointCut()")
	public void mqConsumerBefore() {
		String tid = UUID.randomUUID().toString().replace("-", "");
		if(MDC.get(TRACE_ID)==null){
			MDC.put(TRACE_ID, tid);
		}
	}

	@After("mqPointCut()")
	public void mqConsumerAfter() {
		MDC.remove(TRACE_ID);
	}


}
