01: /**************************************************************************************
02: * Copyright (c) Jonas BonŽr, Alexandre Vasseur. All rights reserved. *
03: * http://aspectwerkz.codehaus.org *
04: * ---------------------------------------------------------------------------------- *
05: * The software in this package is published under the terms of the BSD-style license *
06: * a copy of which has been included with this distribution in the license.txt file. *
07: **************************************************************************************/package examples.async;
08:
09: import org.codehaus.aspectwerkz.joinpoint.JoinPoint;
10: import org.codehaus.aspectwerkz.joinpoint.MethodSignature;
11: import org.codehaus.aspectwerkz.annotation.*;
12:
13: import java.util.concurrent.Executor;
14: import java.util.concurrent.Executors;
15: import java.lang.reflect.Method;
16:
17: import is.Async;
18:
19: public class AsyncAspect {
20:
21: // @Before("staticinitialization(@is.Service)")
22: // public void before(JoinPoint jp) {
23: // System.out.println(jp.toString());
24: // System.out.println(jp.getSignature().toString());
25: // }
26:
27: private Executor m_threadPool = Executors.newCachedThreadPool();
28:
29: @Around("execution(@is.Async) && within(@is.Service)")
30: public Object async(final JoinPoint jp) throws Throwable {
31: // throwing a checked exception does not cause any problem
32: //if (true) throw new NoSuchMethodException("no way");
33: m_threadPool.execute(new Runnable() {
34: public void run() {
35: try {
36: // proceed in a new thread
37:
38: // AOP code
39: Method currentMethod = ((MethodSignature) jp
40: .getSignature()).getMethod();
41:
42: // plain Java code
43: Async theCurrentAnnotation = currentMethod
44: .getAnnotation(Async.class);
45: //System.out.println("AsyncAspect.run - timeout = " + theCurrentAnnotation.timeout());
46:
47: jp.proceed();
48: } catch (Throwable t) {
49: t.printStackTrace();
50: }
51: }
52: });
53: return null;
54: }
55:
56: }
|