In real time we often come across a situation where we have to log the execution time of our methods,
It will be a hectic job to update all the method in our project to log execution of each method, Spring make it simple using its AOP Around Advice Concept.
For this we have to update our spring configuration file in general case it is Portlet-Name.xml
write below code in your Portlet-Name.xml
<aop:config proxy-target-class="true">
<aop:aspect id="myAspect" ref="log">
<aop:pointcut id="myArroundAdviceOperation" expression="execution(* com.foo.liferay.controller.Foo..*(..))" />
<aop:around pointcut-ref="myArroundAdviceOperation" method="aroundAdvice" />
</aop:aspect>
</aop:config>
<bean id="log" class="com.foo.liferay.logger.Log" />
note: attribute proxy-target-class="true" is to be provide only if we are getting exception related to proxy other wise without this attribute we can proceed.
In the above code at <aop:pointcut we have to give our class name with its full package and the point cut id(in this example id="myArroundAdviceOperation" ) has to provide to
<aop:around pointcut-ref as shown above
we need to create a Log class, as in above code i have created inside package com.foo.liferay.logger.Log
package com.foo.liferay.logger;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import org.aspectj.lang.ProceedingJoinPoint;
public class Log {
private static final com.liferay.portal.kernel.log.Log LOG = LogFactoryUtil.getLog(Log.class);
/**
* This is a first method which is being called
* before any method defined in controller and service layer.
*
* @param proceedingJoinPoint the proceedingJoinPoint is a point where method is binding.
* @return Object This is a type of return parameter of the method.
* @exception Throwable During the joint point of the method.
*
*/
public Object aroundAdvice(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
LOG.info("aroundAdvice is calling");
long startTime = System.currentTimeMillis();
LOG.info("START METHOD : "+proceedingJoinPoint.getSignature().getName());
Object obj=proceedingJoinPoint.proceed();
LOG.info("END METHOD : "+proceedingJoinPoint.getSignature().getName());
long timeTaken = System.currentTimeMillis() - startTime;
LOG.info("EXECUTION TIME : " + timeTaken + " MILISECONDS.");
LOG.info("--------------------------------------------------------------");
return obj;
}
}
In this above class one method called aroundAdvice is created ,The @Around has more power and provides more control for end-user to get deal with ProceedingJoinPoint.
now we have to add some dependency to use this spring tool in our pom.xml if you have created maven project
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.6.11</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.6.11</version>
</dependency>
well thats enough for checking your method execution time save all the changes and try loading your portlet when your class Foo will load then inside Foo class all method execution time you will get in your log in my case it is catalina.log.
0 comments:
Post a Comment