KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > ubik > rmi > server > transport > nio > tcp > RWPStrategy


1 package org.sapia.ubik.rmi.server.transport.nio.tcp;
2
3 import java.rmi.RemoteException JavaDoc;
4 import java.util.Properties JavaDoc;
5
6 import org.sapia.ubik.net.nio.Dispatcher;
7 import org.sapia.ubik.net.nio.dispatcher.RWPDispatcher;
8 import org.sapia.ubik.rmi.PropUtil;
9
10 import EDU.oswego.cs.dl.util.concurrent.BoundedBuffer;
11 import EDU.oswego.cs.dl.util.concurrent.Channel;
12 import EDU.oswego.cs.dl.util.concurrent.LinkedQueue;
13 import EDU.oswego.cs.dl.util.concurrent.PooledExecutor;
14
15 /**
16  * This implementation creates <code>RWPDispatcher</code> instances. An RWPDispatcher
17  * uses <code>PooledExecutor</code>s (from Doug Lea's concurrency library). A different
18  * executor is used for read, write and process operations, respectively.
19  * <p>
20  * Each executor maintains a pool of threads as well as a queue of incoming commands
21  * (i.e.: read, write or process commands). Both pooling and queueing behavior can be
22  * configured through the following properties:
23  *
24  * <ul>
25  * <li>ubik.rmi.transport.nio-tcp.rwp.<b>type</b>.max-queue-size
26  * <li>ubik.rmi.transport.nio-tcp.rwp.<b>type</b>.min-pool-size
27  * <li>ubik.rmi.transport.nio-tcp.rwp.<b>type</b>.max-pool-size
28  * <li>ubik.rmi.transport.nio-tcp.rwp.<b>type</b>.keep-alive
29  * <li>ubik.rmi.transport.nio-tcp.rwp.<b>type</b>.abort-full
30  * </ul>
31  * <p>
32  * Note that the <b>type</b> placeholder must in fact consist of one of the
33  * following character strings, depending on the executor it is intented to:
34  * <ul>
35  * <li><code>read</code>
36  * <li><code>write</code>
37  * <li><code>process</code>
38  * </ul>
39  * <p>
40  * The above properties are explained below:
41  * <ul>
42  * <li>max-queue-size: the maximum number of commands that will be queued (pending
43  * to be treated) until they are treated. If this property is not specified, then
44  * no maximum is set.
45  * <li>min-pool-size: the minimum number of threads that should sit in the pool -
46  * the default is no minimum.
47  * <li>max-pool-size: the minimum number of threads that will be pooled - the
48  * default is no maximum.
49  * <li>keep-alive: the maximum amount of time idle threads will be kept in the
50  * pool before being terminated - the default is no termination (idle threads
51  * are kept in the pool).
52  * <li>abort-full: indicates if attempts at inserting a command in a queue that
53  * has reached its maximum size will result in a RuntimeException or in the caller
54  * thread (in fact the selector's) waiting indefinetaly until the command can be accepted.
55  * The value of this property is either <code>true</code> if a RuntimeException
56  * should be thrown, or <code>false</code> if an indefinite wait should take place. This
57  * property defaults to <code>true</code>, and will be valid only if no
58  * <code>max-queue-size</code> has been specified.
59  * </ul>
60  *
61  * @author Yanick Duchesne
62  *
63  * <dl>
64  * <dt><b>Copyright: </b>
65  * <dd>Copyright &#169; 2002-2005 <a HREF="http://www.sapia-oss.org">Sapia Open
66  * Source Software </a>. All Rights Reserved.</dd>
67  * </dt>
68  * <dt><b>License: </b>
69  * <dd>Read the license.txt file of the jar or visit the <a
70  * HREF="http://www.sapia-oss.org/license.html">license page </a> at the Sapia
71  * OSS web site</dd>
72  * </dt>
73  * </dl>
74  */

75 public class RWPStrategy implements DispatcherStrategy {
76   
77   private static final String JavaDoc PROP_PREFIX = "ubik.rmi.transport.nio-tcp.rwp";
78   private static final String JavaDoc PROP_READ_SUFFIX = "read";
79   private static final String JavaDoc PROP_WRITE_SUFFIX = "write";
80   private static final String JavaDoc PROP_PROCESS_SUFFIX = "process";
81
82   /**
83    * Corresponds to the property that specifies the maximum number of commands
84    * that the corresponding queue can hold - until they are processed by a pooled
85    * thread (defaults to no maximum).
86    */

87   public static final String JavaDoc MAX_QUEUE_SIZE = "max-queue-size";
88   
89   /**
90    * Corresponds to the property that specifies the minimum number of threads
91    * in the corresponding pool (defaults to no minimum size).
92    */

93   public static final String JavaDoc MIN_POOL_SIZE = "min-pool-size";
94   
95   /**
96    * Corresponds to the property that specifies the maximum number of threads
97    * in the corresponding pool (defaults to no maximum size).
98    */

99   public static final String JavaDoc MAX_POOL_SIZE = "max-pool-size";
100   
101   /**
102    * Corresponds to the property that specifies the maximum number of milliseconds that
103    * idle threads are kept in the corresponding pool (this property defaults to no keep-alive,
104    * meaning that idle threads are not terminated).
105    */

106   public static final String JavaDoc KEEP_ALIVE = "keep-alive";
107   
108   /**
109    * Corresponds to the property that specifies if callers are aborted when all threads
110    * are busy and the command queue is empty (this property defaults to <code>true</code>).
111    */

112   public static final String JavaDoc ABORT_FULL = "abort-full";
113
114   /**
115    * Creates a new dispatcher and returns it. This method takes into account the
116    * given properties and will also lookup the system properties, if necessary.
117    *
118    * @see org.sapia.ubik.rmi.server.transport.nio.tcp.DispatcherStrategy#newDispatcher(java.util.Properties)
119    */

120   public Dispatcher newDispatcher(Properties JavaDoc props) throws RemoteException JavaDoc {
121     PropUtil pu = new PropUtil().addProperties(props);
122     pu.addProperties(System.getProperties());
123     
124     PooledExecutor read = createExecutor(PROP_READ_SUFFIX, pu);
125     PooledExecutor write = createExecutor(PROP_WRITE_SUFFIX, pu);
126     PooledExecutor process = createExecutor(PROP_PROCESS_SUFFIX, pu);
127     RWPDispatcher dispatcher = new RWPDispatcher(read, write, process);
128     return dispatcher;
129   }
130   
131   private PooledExecutor createExecutor(String JavaDoc suffix, PropUtil props){
132     int queueSize = props.getIntProperty(prop(suffix, MAX_QUEUE_SIZE), 0);
133     Channel queue;
134     if(queueSize <= 0){
135       queue = new LinkedQueue();
136     }
137     else{
138       queue = new BoundedBuffer(queueSize);
139     }
140     
141     PooledExecutor exec = new PooledExecutor(queue);
142     
143     int minPoolSize = props.getIntProperty(prop(suffix, MIN_POOL_SIZE), 0);
144     if(minPoolSize > 0){
145       exec.setMinimumPoolSize(minPoolSize);
146     }
147     
148     int maxPoolSize = props.getIntProperty(prop(suffix, MAX_POOL_SIZE), 0);
149     if(minPoolSize > 0){
150       exec.setMaximumPoolSize(maxPoolSize);
151     }
152     
153     long keepAlive = props.getLongProperty(prop(suffix, KEEP_ALIVE), -1);
154     exec.setKeepAliveTime(keepAlive);
155     
156     boolean abortFull = props.getBooleanProperty(prop(suffix, ABORT_FULL), true);
157     if(abortFull && queue instanceof BoundedBuffer){
158       exec.abortWhenBlocked();
159     }
160     return exec;
161   }
162   
163   private String JavaDoc prop(String JavaDoc suffix, String JavaDoc propType){
164     return new StringBuffer JavaDoc().append(PROP_PREFIX).append('.').append(suffix).append('.').append(propType).toString();
165   }
166
167 }
168
Popular Tags