My Quotes


When U were born , you cried and the world rejoiced
Live U'r life in such a way that when you go
THE WORLD SHOULD CRY






Monday, June 9, 2014

Spring AOP Log4j

  • Spring-aop.xml – I am AOPing all packages using the pointcut.
      
      
    
         
                                      
        
                            
                            
    
                            
    
                                              
       
    
  • Import this XML into your main spring xml
  • Next is the source for the performance logging advice
    package com.test.common;
    
    import java.lang.reflect.Method;
    
    import org.springframework.aop.AfterReturningAdvice;
    import org.springframework.aop.MethodBeforeAdvice;
    import org.apache.log4j.Logger;
    
    public class PerformanceLoggingAdvice implements MethodBeforeAdvice, AfterReturningAdvice {
        /** Time in milliseconds */
     long startTime = 0;
    
     /** Time in milliseconds */
     long finishTime = 0;
    
     protected static final Logger loggerObj =Logger.getLogger(PerformanceLoggingAdvice.class);
     
        public PerformanceLoggingAdvice() {
        }
    
        @Override
        public void afterReturning(Object returnValue,
      Method method, Object[] args, Object target) throws Throwable {
     finishTime = System.currentTimeMillis();
     double totalDuration = finishTime - startTime;
     loggerObj.info("Finished executing method " + method.getName()
      + " on object " + target.getClass().getName() + " in "
      + totalDuration / 1000 + " seconds.");
        }
    
        @Override
        public void before(Method method, Object[] args,
      Object target) throws Throwable {
     startTime = System.currentTimeMillis();
     loggerObj.info("Executing method " + method.getName()
      + " on object " + target.getClass().getName());
        }
    
    }
    
  • pom.xml file entries you may need





  • 
                aopalliance
                aopalliance
                1.0
    
    
                org.aspectj
                aspectjrt
                1.8.0
    
    
                org.aspectj
                aspectjweaver
                1.8.0
    
    
                cglib
                cglib-nodep
                3.1
    
    
                org.springframework
                spring-aop
                3.1.3
    
    
                org.springframework
                spring-aspects
                3.1.3
    
    


    Here is the logger from the log files

    2014-06-09 12:41:45,491 INFO (PerformanceLoggingAdvice.java:35) - Executing method onMessage on object com.test..client.CalculationsMessageListener
    2014-06-09 12:41:45,493 INFO (PerformanceLoggingAdvice.java:35) - Executing method calculateAvailThrottler on object com.test..handler.CalculationServiceHandler
    2014-06-09 12:41:45,616 INFO (PerformanceLoggingAdvice.java:35) - Executing method getPrioritizeReleaseTemp on object com.test..service.ThrottlerCalcService
    2014-06-09 12:41:45,617 INFO (PerformanceLoggingAdvice.java:35) - Executing method getPrioritizeReleaseTemp on object com.test..dao.impl.PreReleaseDAOImpl
    2014-06-09 12:41:45,957 INFO (PerformanceLoggingAdvice.java:26) - Finished executing method getPrioritizeReleaseTemp on object com.test..dao.impl.PreReleaseDAOImpl in 0.34 seconds.
    2014-06-09 12:41:45,960 INFO (PerformanceLoggingAdvice.java:35) - Executing method reCalculateLiquidity on object com.test..dao.impl.AdjustmentsDAOImpl
    2014-06-09 12:41:46,178 INFO (PerformanceLoggingAdvice.java:26) - Finished executing method reCalculateLiquidity on object com.test..dao.impl.AdjustmentsDAOImpl in 0.218 seconds.
    2014-06-09 12:41:46,178 INFO (PerformanceLoggingAdvice.java:26) - Finished executing method getPrioritizeReleaseTemp on object com.test..service.ThrottlerCalcService in 0.218 seconds.
    2014-06-09 12:41:46,187 INFO (PerformanceLoggingAdvice.java:26) - Finished executing method calculateAvailThrottler on object com.test..handler.CalculationServiceHandler in 0.227 seconds.
    2014-06-09 12:41:46,188 INFO (PerformanceLoggingAdvice.java:26) - Finished executing method onMessage on object com.test..client.CalculationsMessageListener in 0.228 seconds.

    No comments :