Profiling Example : Spring Aspect « Spring « Java






Profiling Example


/*
Pro Spring
By Rob Harrop
Jan Machacek
ISBN: 1-59059-461-4
Publisher: Apress
*/



///////////////////////////////////////////////////////////////////////////////////////
public class WorkerBean {

    public void doSomeWork(int noOfTimes) {
        for(int x = 0; x < noOfTimes; x++) {
            work();
        }
    }
    
    private void work() {
        System.out.print("");
    }
}


///////////////////////////////////////////////////////////////////////////////////////

import java.lang.reflect.Method;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.util.StopWatch;

public class ProfilingInterceptor implements MethodInterceptor {

    public Object invoke(MethodInvocation invocation) throws Throwable {
        // start the stop watch
        StopWatch sw = new StopWatch();
        sw.start(invocation.getMethod().getName());

        Object returnValue = invocation.proceed();

        sw.stop();
        dumpInfo(invocation, sw.getTotalTimeMillis());
        return returnValue;
    }

    private void dumpInfo(MethodInvocation invocation, long ms) {
        Method m = invocation.getMethod();
        Object target = invocation.getThis();
        Object[] args = invocation.getArguments();

        System.out.println("Executed method: " + m.getName());
        System.out.println("On object of type: " + target.getClass().getName());

        System.out.println("With arguments:");
        for (int x = 0; x < args.length; x++) {
            System.out.print("    > " + args[x]);
        }
        System.out.print("\n");

        System.out.println("Took: " + ms + " ms");
    }

}
///////////////////////////////////////////////////////////////////////////////////////

import org.springframework.aop.framework.ProxyFactory;

public class ProfilingExample {

    public static void main(String[] args) {
        WorkerBean bean = getWorkerBean();
        bean.doSomeWork(10000000);
    }
    
    private static WorkerBean getWorkerBean() {
        WorkerBean target = new WorkerBean();
        
        ProxyFactory factory = new ProxyFactory();
        factory.setTarget(target);
        factory.addAdvice(new ProfilingInterceptor());
        
        return (WorkerBean)factory.getProxy();
    }
}


///////////////////////////////////////////////////////////////////////////////////////
           
       








ProfilingExample.zip( 1,479 k)

Related examples in the same category

1.Introduction Config Example
2.Security Example
3.Simple After Returning Advice
4.Simple Before Advice
5.Simple Throws Advice
6.Composable Pointcut Example
7.Control Flow Example
8.Dynamic Pointcut Example
9.Hello World With Pointcut
10.Spring Aspect Introduction Example
11.Static Pointcut Example
12.Name Pointcut Example
13.Name Pointcut Using Advisor
14.Proxy Factory Bean Example
15.Proxy Perf Test
16.Regexp Pointcut Example
17.After Advice Example
18.AspectJ Example from Pro Spring
19.Aspect Hello World Example