MqAspect.java 2.98 KB
Newer Older
yanzg's avatar
yanzg committed
1 2
package com.yanzuoguang.cloud.aop;

yanzg's avatar
yanzg committed
3
import com.yanzuoguang.mq.plan.YzgMqConsumer;
yanzg's avatar
yanzg committed
4
import com.yanzuoguang.util.exception.ExceptionHelper;
yanzg's avatar
yanzg committed
5
import com.yanzuoguang.util.log.Log;
yanzg's avatar
yanzg committed
6
import com.yanzuoguang.util.vo.LogVo;
yanzg's avatar
yanzg committed
7 8 9 10
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
yanzg's avatar
yanzg committed
11
import org.aspectj.lang.reflect.MethodSignature;
yanzg's avatar
yanzg committed
12 13
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
yanzg's avatar
yanzg committed
14 15
import org.springframework.stereotype.Component;

yanzg's avatar
yanzg committed
16 17
import java.lang.reflect.Method;

yanzg's avatar
yanzg committed
18 19 20 21 22 23 24 25 26 27
/**
 * LogsAspect(接口请求日志切面)
 *
 * @author: Kang
 * @time: 2018年04月25日 11:43
 */
@Aspect
@Component
public class MqAspect extends BaseRequestAspect {

yanzg's avatar
yanzg committed
28
    private static final String TAG = MqAspect.class.getSimpleName();
yanzg's avatar
yanzg committed
29 30 31 32 33

    /**
     * exec aop point aspect
     */
    @Pointcut("execution(* *..mq..*Consumer.*(..))")
yanzg's avatar
yanzg committed
34
    public void mqAspect() {
yanzg's avatar
yanzg committed
35 36 37 38 39 40 41 42
    }

    /**
     * 执行环形切面
     *
     * @param joinPoint
     * @return
     */
yanzg's avatar
yanzg committed
43
    @Around(value = "mqAspect()")
yanzg's avatar
yanzg committed
44
    public Object requestWebAround(ProceedingJoinPoint joinPoint) throws Throwable {
yanzg's avatar
yanzg committed
45
        boolean clear = isRabbit(joinPoint);
yanzg's avatar
yanzg committed
46 47
        LogVo log = null;
        long start = System.currentTimeMillis();
yanzg's avatar
yanzg committed
48
        String url = getMethodUrl(joinPoint);
yanzg's avatar
yanzg committed
49
        if (clear) {
yanzg's avatar
yanzg committed
50
            log = startLog(TAG, url, getRequestBody(joinPoint));
yanzg's avatar
yanzg committed
51 52
            clear = clear && log != null;
        }
yanzg's avatar
yanzg committed
53 54
        if (clear) {
            Log.threadBegin();
yanzg's avatar
yanzg committed
55
            start = requestLog(TAG, clear, joinPoint);
yanzg's avatar
yanzg committed
56
        }
yanzg's avatar
yanzg committed
57 58
        Object result = null;
        Exception ex = null;
yanzg's avatar
yanzg committed
59
        try {
yanzg's avatar
yanzg committed
60 61
            result = executeMethod(joinPoint);
            return result;
yanzg's avatar
yanzg committed
62 63
        } catch (Exception e) {
            // 消费消息出错
yanzg's avatar
yanzg committed
64 65
            result = ExceptionHelper.getError(e);
            ex = e;
yanzg's avatar
yanzg committed
66 67
            throw e;
        } finally {
yanzg's avatar
yanzg committed
68
            responseLog(log, TAG, url, clear, joinPoint, start, result, ex);
yanzg's avatar
yanzg committed
69 70 71
        }
    }

yanzg's avatar
yanzg committed
72
    private boolean isRabbit(ProceedingJoinPoint joinPoint) {
yanzg's avatar
yanzg committed
73 74 75 76 77 78 79 80 81 82 83 84
        boolean ret = false;
        if (!(joinPoint.getSignature() instanceof MethodSignature)) {
            return false;
        }
        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
        Method targetMethod = methodSignature.getMethod();
        if (YzgMqConsumer.class.equals(targetMethod.getDeclaringClass())) {
            ret = false;
        } else {
            RabbitListener annotation = targetMethod.getAnnotation(RabbitListener.class);
            if (annotation != null) {
                ret = true;
85
            }
yanzg's avatar
yanzg committed
86 87 88 89
            if (!ret) {
                Message message = getMessage(joinPoint.getArgs());
                if (message != null) {
                    ret = true;
yanzg's avatar
yanzg committed
90 91 92
                }
            }
        }
yanzg's avatar
yanzg committed
93
        return ret;
yanzg's avatar
yanzg committed
94 95
    }

yanzg's avatar
yanzg committed
96
    private Object executeMethod(ProceedingJoinPoint joinPoint) throws Throwable {
yanzg's avatar
yanzg committed
97 98 99
        return joinPoint.proceed();
    }
}