View Javadoc

1   /*
2    * Created on May 12, 2004
3    *
4    */
5   package org.neo.swarm.interceptor.asyncQueue;
6   
7   import org.aopalliance.intercept.MethodInvocation;
8   import org.neo.swarm.core.aop.silc.comp.MethodInterceptor;
9   
10  import EDU.oswego.cs.dl.util.concurrent.BoundedLinkedQueue;
11  
12  /***
13   * This object does..
14   * 
15   * @author navery
16   */
17  public class AsyncInterceptor implements MethodInterceptor {
18  
19  	BoundedLinkedQueue eventQueue;
20  
21  	AsyncInterceptor(int queueSize) {
22  		eventQueue = new BoundedLinkedQueue(queueSize);
23  		Thread eventQueue = new EventFeed();
24  		eventQueue.start();
25  	}
26  
27  	/***
28  	 * @return Returns the queueSize.
29  	 */
30  	public int getQueueSize() {
31  		return eventQueue.capacity();
32  	}
33  
34  	/***
35  	 *   The queueSize to set.
36  	 * @param queueSize
37  	 */
38  	public void setQueueSize(int queueSize) {
39  		// change the size of the queue, doesnt dump existing entries, but will force new puts to wait until capacity is sufficient
40  		eventQueue.setCapacity(queueSize);
41  	}
42  
43  	public Object invoke(MethodInvocation invocation) throws Throwable {
44  		// raise warning log if event queue is full..
45  		if (eventQueue.size() == eventQueue.capacity()) {
46  			//LOG.info("event queue full:" + eventQueue.capacity());
47  			Thread.yield(); // attempt to allow queue depletion
48  		}
49  		eventQueue.put(invocation);
50  		return null;
51  	}
52  
53  	/***
54  	 * Feed thread that extracts events from the eventQueue and invokes the
55  	 * recipient object.
56  	 */
57  	class EventFeed extends Thread {
58  
59  		public void run() {
60  			try {
61  				while (true) {
62  					try {
63  						MethodInvocation invocation = (MethodInvocation) eventQueue.take();
64  						invocation.proceed();
65  					} catch (Throwable e) {
66  						e.printStackTrace();
67  					}
68  				}
69  			} catch (Exception e) {
70  				e.printStackTrace();
71  				//				LOG.info("eventThread exiting..." + this);
72  			}
73  		}
74  	}
75  }