SPRINGAOP to monitor the execution time of each method in the service layer

Source: Internet
Author: User

Using AOP is too convenient and especially suitable for this type of scenario.

The code is as follows, where the information to be counted is written to the log file, or it can be designed to be written into a table.

Package Com.ecsoft.interceptor;import Org.aspectj.lang.ProceedingJoinPoint;import Org.aspectj.lang.annotation.Around;import Org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.reflect.MethodSignature;import Org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.stereotype.Component;@Aspect@Component Public classTimeinterceptor {Private StaticLogger log = Loggerfactory.GetLogger(Timeinterceptor.class);//Service layer Statistics time slices, type must be final string type, the variables to be used in annotations can only be static constant type     Public Static FinalString point ="Execution (* com.ecsoft.service). *impl.* (..)) ";/**  * Statistical method execution time-consuming around surround notification     * @param joinpoint      * @return      */      @Around(point) PublicObjectTimearound(Proceedingjoinpoint joinpoint) {//Define return object, get parameters required by methodObject obj =NULL; object[] args = Joinpoint.Getargs();LongStartTime = System.Currenttimemillis();Try{obj = Joinpoint.Proceed(args); }Catch(Throwable e) {log.Error("Statistics a Method execution time wrapping notification Error", e); }//Gets the method name of the execution        LongEndTime = System.Currenttimemillis(); Methodsignature signature = (methodsignature) joinpoint.getsignature(); String methodName = signature.Getdeclaringtypename() +"."+ signature.GetName();//Print time-consuming information         This.Printexectime(MethodName, StartTime, endTime);returnObj }/**  * Printing method Execution time-consuming information, if more than a certain time, before printing     * @param methodName      * @param startTime      * @param endTime      */      Private void Printexectime(String MethodName,LongStartTime,LongEndTime) {LongDifftime = Endtime-starttime;//More than 1 seconds of record        if(Difftime > +) {log.Info(MethodName +":"+ Difftime +": MS"); }      }  }

SPRINGAOP to monitor the execution time of each method in the service layer

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.