码迷,mamicode.com
首页 > 编程语言 > 详细

trace spring

时间:2018-10-19 19:47:56      阅读:204      评论:0      收藏:0      [点我收藏+]

标签:wired   port   gty   ring   rest   ota   ==   beans   bin   

package xx.com.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RestController;


import javax.servlet.http.HttpServletRequest;
import java.lang.annotation.Annotation;
import java.util.Date;
import java.util.Vector;

@Aspect
@Configuration
public class performace {

    @Autowired
    HttpServletRequest request;

    static final ThreadLocal<TraceInfo> localRequest=new ThreadLocal<>();
    static final ThreadLocal<Boolean> localIsChild=new ThreadLocal<>();

    public static TraceInfo getLocalRequest() {
        return localRequest.get();
    }
    public static void setParentRequest(TraceInfo traceInfo) {
        localRequest.set(traceInfo);
        localIsChild.set(true);
    }

//    @Around("execution(* xx.com..*(..))")
//    @Around("!within(is(FinalType)) && execution(* *..*(..))")
    public Object aspectAround(ProceedingJoinPoint point) throws Throwable {


        String methodName = point.getSignature().getName();
        String type=point.getSignature().getDeclaringType().getName()+":"+methodName;
        TraceInfo traceInfo=new TraceInfo(type,new Date());
        traceInfo.setChilds(new Vector<>());
        if(localIsChild.get()!=null&&localIsChild.get()){
            traceInfo.setChildThread(true);
        }
        boolean isMain=false;
        if(localRequest.get()==null) {
            isMain=true;
            localRequest.set(traceInfo);
        }
        Object ret=point.proceed();
        traceInfo.setEndDate(new Date());

        if(isMain) {
            try {
                request.getAttribute("a");
            } catch (Exception e) {
                localRequest.remove();
                System.out.println("*** 非请求执行:" + traceInfo.getType() + " : " + String.valueOf(traceInfo.getEndDate().getTime() - traceInfo.getStartDate().getTime()));
                return ret;
            }
        }

        if(!isMain){
            localRequest.get().getChilds().add(traceInfo);
        }

        for (Annotation an : point.getTarget().getClass().getAnnotations()) {
            if (an instanceof RestController) {
                //end controller
                    trace(localRequest.get(), 0);
                localRequest.remove();
                break;
            }
        }

//        traces=new Vector<>();
//        request.setAttribute("traces", traces);
        return ret;

    }

    private void trace(TraceInfo traceInfo,int deep){

        char[] space=new char[deep];
        for(int i=0;i<deep;i++){
            if(i==deep-1){
                if(traceInfo.isChildThread()){
                    space[i] = ‘→‘;
                }else {
                    space[i] = ‘┞‘;
                }
                break;
            }
            space[i]=‘ ‘;
        }
        System.out.println(
                new String(space)+
                traceInfo.getType()+
                " : "+
                String.valueOf(traceInfo.getEndDate().getTime()-traceInfo.getStartDate().getTime()));
        if(traceInfo.getChilds()==null){
            return;
        }
        for(TraceInfo child:traceInfo.getChilds()){
            trace(child,deep+1);
        }

    }


    public static class TraceInfo{


        private Date startDate;
        private Date endDate;
        private String type;
        private Vector<TraceInfo> childs;
        private boolean childThread=false;

        public TraceInfo(String type,Date time){
            this.startDate=time;
            this.type=type;
        }

        public Vector<TraceInfo> getChilds() {
            return childs;
        }

        public void setChilds(Vector<TraceInfo> childs) {
            this.childs = childs;
        }

        public Date getEndDate() {
            return endDate;
        }


        public void setEndDate(Date endDate) {
            this.endDate = endDate;
        }

        public Date getStartDate() {
            return startDate;
        }

        public void setStartDate(Date startDate) {
            this.startDate = startDate;
        }

        public String getType() {
            return type;
        }

        public void setType(String type) {
            this.type = type;
        }

        public boolean isChildThread() {
            return childThread;
        }

        public void setChildThread(boolean childThread) {
            this.childThread = childThread;
        }
    }

}

 

trace spring

标签:wired   port   gty   ring   rest   ota   ==   beans   bin   

原文地址:https://www.cnblogs.com/a-xu/p/9818353.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!