KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > examples > async > AsyncAspect


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 BSD-style license *
6  * a copy of which has been included with this distribution in the license.txt file. *
7  **************************************************************************************/

8 package examples.async;
9
10 import org.codehaus.aspectwerkz.joinpoint.JoinPoint;
11 import org.codehaus.aspectwerkz.joinpoint.MethodSignature;
12 import org.codehaus.aspectwerkz.annotation.*;
13
14 import java.util.concurrent.Executor JavaDoc;
15 import java.util.concurrent.Executors JavaDoc;
16 import java.lang.reflect.Method JavaDoc;
17
18 import is.Async;
19
20 public class AsyncAspect {
21
22 // @Before("staticinitialization(@is.Service)")
23
// public void before(JoinPoint jp) {
24
// System.out.println(jp.toString());
25
// System.out.println(jp.getSignature().toString());
26
// }
27

28     private Executor JavaDoc m_threadPool = Executors.newCachedThreadPool();
29
30     @Around("execution(@is.Async) && within(@is.Service)")
31     public Object JavaDoc async(final JoinPoint jp) throws Throwable JavaDoc {
32         // throwing a checked exception does not cause any problem
33
//if (true) throw new NoSuchMethodException("no way");
34
m_threadPool.execute(
35                 new Runnable JavaDoc() {
36                     public void run() {
37                         try {
38                             // proceed in a new thread
39

40                             // AOP code
41
Method JavaDoc currentMethod = ((MethodSignature)jp.getSignature()).getMethod();
42
43                             // plain Java code
44
Async theCurrentAnnotation = currentMethod.getAnnotation(Async.class);
45                             //System.out.println("AsyncAspect.run - timeout = " + theCurrentAnnotation.timeout());
46

47                             jp.proceed();
48                         } catch (Throwable JavaDoc t) {
49                             t.printStackTrace();
50                         }
51                     }
52                 }
53         );
54         return null;
55     }
56
57
58 }
Popular Tags