All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.dexcoder.commons.interceptor.RunBinderInterceptor Maven / Gradle / Ivy

package com.dexcoder.commons.interceptor;

import org.apache.poi.ss.formula.functions.T;
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.transaction.interceptor.TransactionAspectSupport;

import com.dexcoder.commons.exceptions.DexcoderException;
import com.dexcoder.commons.result.RunBinder;

/**
 * 业务拦截器,主要拦截异常及错误信息,最好配合RunBinderOnMvcDestroyInterceptor使用防止内存溢出
 * 
 * Created by liyd on 4/9/14.
 */
@Aspect
public class RunBinderInterceptor {

    /** 日志对象 */
    private static final Logger LOG         = LoggerFactory.getLogger(RunBinderInterceptor.class);

    /** 执行时间超过打印warn日志毫秒数 */
    private static final long   LOG_TIMEOUT = 1000;

    @Pointcut("@within(org.springframework.stereotype.Service)")
    public void serviceAnnotation() {

    }

    @Pointcut("bean(*ServiceImpl)")
    public void serviceName() {

    }

    @Around("serviceAnnotation() || serviceName()")
    public Object around(ProceedingJoinPoint pjp) {

        //被拦截的类
        String targetClass = pjp.getTarget().getClass().getName();

        //被拦截方法
        String targetMethod = pjp.getSignature().getName();

        //当前时间毫秒数
        long startTime = System.currentTimeMillis();

        LOG.debug("start:[class={},method={},startTime={}]", new Object[] { targetClass, targetMethod, startTime });

        //返回结果
        Object result = null;
        try {
            //此处调用业务方法
            result = pjp.proceed();

        } catch (DexcoderException dexcoderException) {

            RunBinderTransactionAspectSupport.setRollbackOnly();
            RunBinder.addError(dexcoderException);
            LOG.info(String.format("已知异常,方法:[class=%s,method=%s],信息:[resultCode={},resultMsg={}]", targetClass,
                targetMethod), dexcoderException.getResultCode(), dexcoderException.getResultMsg());
            //ignore
        } catch (Throwable throwable) {
            RunBinderTransactionAspectSupport.setRollbackOnly();
            RunBinder.addError("UN_KNOWN_EXCEPTION", "未知异常");
            LOG.error(String.format("未知异常,方法:[class=%s,method=%s]", targetClass, targetMethod), throwable);
            //ignore
        }

        long finishTime = System.currentTimeMillis();

        LOG.debug("finished:[class={},method={},finishTime={}]", new Object[] { targetClass, targetMethod, finishTime });

        long useTime = finishTime - startTime;

        if (useTime > LOG_TIMEOUT) {

            LOG.warn("Long processing time:[class={},method={},used time={}]", new Object[] { targetClass,
                    targetMethod, useTime });
        }
        return result;

    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy