KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > examples > callback > Callback


1 /**************************************************************************************
2  * Copyright (c) Jonas BonŽr, Alexandre Vasseur. All rights reserved. *
3  * http://aspectwerkz.codehaus.org *
4  * ---------------------------------------------------------------------------------- *
5  * The software in this package is published under the terms of the LGPL license *
6  * a copy of which has been included with this distribution in the license.txt file. *
7  **************************************************************************************/

8 package examples.callback;
9
10 import org.codehaus.aspectwerkz.joinpoint.StaticJoinPoint;
11 import org.codehaus.aspectwerkz.annotation.Around;
12
13 import java.util.concurrent.Executor JavaDoc;
14 import java.util.concurrent.Executors JavaDoc;
15 import java.util.concurrent.ThreadFactory JavaDoc;
16
17 /**
18  * Callback call from an async aspect
19  * Run with VM options:
20  * -javaagent:lib\aspectwerkz-core-2.0beta1.jar
21  * -Daspectwerkz.definition.file=src\jdk15\samples\examples\callback\aop.xml
22  * And optianally:
23  * -Daspectwerkz.transform.verbose=true
24  * <p/>
25  * Note: you can avoid use of -D...file=...aop.xml if you have the aop.xml in a META-INF folder somewhere in the classpath.
26  *
27  * @author <a HREF="mailto:alex AT gnilux DOT com">Alexandre Vasseur</a>
28  */

29 public class Callback {
30
31     /**
32      * This method is a callback - could be looked up with a custom @ or etc
33      */

34     public void callback(String JavaDoc message) {
35         System.out.println("Callback.callback " + message);
36     }
37
38     /**
39      * Triggers the time consuming operation on callee
40      * Note that when used with "this(..) pcd, the caller method cannot be static else we won't match
41      * since caller instance is not available..
42      */

43     public void work() {
44         System.out.println("Callback.work - start");
45         Callee callee = new Callee();
46         callee.longOp(10000);
47         System.out.println("Callback.work - end");
48     }
49
50     /**
51      * Sample
52      *
53      * @param args
54      * @throws Throwable
55      */

56     public static void main(String JavaDoc args[]) throws Throwable JavaDoc {
57         Callback caller = new Callback();
58         caller.work();
59     }
60
61     /**
62      * Callee does a time consuming operation
63      */

64     public static class Callee {
65
66         public void longOp(int howLong) {
67             System.out.println("Callback$Callee.longOp for " + howLong);
68             for (int i = 0; i < howLong; i++) {
69                 ;
70             }
71         }
72     }
73
74     public static class AsyncAspect {
75
76         /**
77          * Java 5 thread utils
78          */

79         private Executor JavaDoc m_threadPool = Executors.newCachedThreadPool(
80                 new ThreadFactory JavaDoc() {
81                     public Thread JavaDoc newThread(Runnable JavaDoc target) {
82                         Thread JavaDoc t = new Thread JavaDoc(target);
83                         t.setDaemon(true);// use of daemon to run from Ant
84
return t;
85                     }
86                 }
87         );
88
89         // a bit tedious to match inner class so I am a bit lazy here.
90
@Around("call(* *..*.*Callee.longOp(int)) && args(howLong) && this(caller)")
91                 public Object JavaDoc doAsync(final StaticJoinPoint sjp, int howLong, final Callback caller) throws Throwable JavaDoc {
92             System.out.println("[AOP powered] - Callback$AsyncAspect.doAsync - for this long: " + howLong);
93             m_threadPool.execute(
94                     new Runnable JavaDoc() {
95                         public void run() {
96                             try {
97                                 // proceed in a new thread
98
sjp.proceed();
99                                 // when done, triggers the callback
100
caller.callback(
101                                         "[AOP powered] - I am done there .. " + Thread.currentThread().getName()
102                                 );
103                             } catch (Throwable JavaDoc e) {
104                                 throw new RuntimeException JavaDoc(e);
105                             }
106                         }
107                     }
108             );
109             return null;
110         }
111     }
112
113
114 }
115
Popular Tags