CatAdvice.java 1.8 KB
Newer Older
苗卫卫 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
package com.starcharge.base.config;

import com.starcharge.wios.task.ScheduleTask;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

//import com.dianping.cat.Cat;
//import com.dianping.cat.message.Transaction;
import com.ihidea.core.support.exception.ServiceException;

/**
 * 拦截service层方法,记录cat日志
 * 
 * @author lilin
 * @version [版本号, 2019年1月16日]
 */
@Component
@Aspect
public class CatAdvice {
    private Logger logger = LoggerFactory.getLogger(CatAdvice.class);
    /**
     * 定义pointcut
     */
    @Pointcut("execution(public * com.starcharge..*.*(..))")
    public void pointcut() {
        
    }
    
    /**
     * 拦截要执行的目标方法
     *
     * @param joinPoint joinPoint
     * @return Object
     */
    @Around("pointcut()")
    public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
        try {

            return pjp.proceed();

        } catch (Throwable e) {

            if (e instanceof ServiceException) {
                logger.error( "[doAround]全局异常出口-Service,原因={}", ExceptionUtils.getFullStackTrace(e));
                throw new ServiceException(((ServiceException) e).getCode(), e.getMessage());
            } else {
                logger.error( "[doAround]全局异常出口-Throwable,原因={}", ExceptionUtils.getFullStackTrace(e));
                // 如果是生产环境,直接抛出
                throw new ServiceException(e.getMessage());

            }
        } finally {

        }

    }
}