Profiling Example : Spring Aspect : Spring : Java examples (example source code) Organized by topic

Java
C++
PHP


Java  »  Spring   » [  Spring Aspect  ]  Screenshots 
 



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 invocationthrows 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();
    }
}


///////////////////////////////////////////////////////////////////////////////////////
           
       
Download: ProfilingExample.zip   ( 1,479  K )  
Related examples in the same category
1.  Introduction Config ExampleHas Download File
2.  Security ExampleHas Download File
3.  Simple After Returning AdviceHas Download File
4.  Simple Before AdviceHas Download File
5.  Simple Throws AdviceHas Download File
6.  Composable Pointcut ExampleHas Download File
7.  Control Flow ExampleHas Download File
8.  Dynamic Pointcut Example Has Download File
9.  Hello World With PointcutHas Download File
10.  Spring Aspect Introduction ExampleHas Download File
11.  Static Pointcut ExampleHas Download File
12.  Name Pointcut ExampleHas Download File
13.  Name Pointcut Using AdvisorHas Download File
14.  Proxy Factory Bean ExampleHas Download File
15.  Proxy Perf TestHas Download File
16.  Regexp Pointcut ExampleHas Download File
17.  After Advice ExampleHas Download File
18.  AspectJ Example from Pro SpringHas Download File
19.  Aspect Hello World ExampleHas Download File
























Home| Contact Us
Copyright 2003 - 04 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.