View Javadoc

1   /*
2    * $Id: PoolingProfile.java 10529 2008-01-25 05:58:36Z dfeist $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.config;
12  
13  import java.util.Map;
14  
15  import org.apache.commons.collections.map.CaseInsensitiveMap;
16  
17  
18  /**
19   * <code>PoolingProfile</code> is a configuration object used to define the object
20   * pooling parameters for the service it is associated with.
21   */
22  
23  public class PoolingProfile
24  {
25  
26      /**
27       * Tells the object pool not to initialise any components on startup.
28       */
29      public static final int INITIALISE_NONE = 0;
30  
31      /**
32       * Tells the object pool only to initialise one service on startup.
33       */
34      public static final int INITIALISE_ONE = 1;
35  
36      /**
37       * Tells the object pool to initialise all components on startup.
38       */
39      public static final int INITIALISE_ALL = 2;
40  
41      // Constants used to determine the exhausted action of the pool
42      public static final int WHEN_EXHAUSTED_FAIL = 0;
43      /** @deprecated use WHEN_EXHAUSTED_WAIT instead */
44      public static final int WHEN_EXHAUSTED_BLOCK = 1;
45      public static final int WHEN_EXHAUSTED_WAIT = 1;
46      public static final int WHEN_EXHAUSTED_GROW = 2;
47  
48      /**
49       * Controls the maximum number of Mule UMOs that can be borrowed from a service
50       * pool at one time. When non-positive, there is no limit to the number of
51       * components that may be active at one time. When maxActive is exceeded, the
52       * pool is said to be exhausted. You can specify this value on the descriptor
53       * declaration. If none is set this value will be used.
54       */
55      public static final int DEFAULT_MAX_POOL_ACTIVE = 5;
56  
57      /**
58       * Controls the maximum number of Mule UMOs that can sit idle in the pool at any
59       * time. When non-positive, there is no limit to the number of Mule UMOs that may
60       * be idle at one time. You can specify this value on the descriptor declaration.
61       * If none is set this value will be used. If this value is not set then a system
62       * default of '5' will be used.
63       */
64      public static final int DEFAULT_MAX_POOL_IDLE = 5;
65  
66      /**
67       * When the threadPoolExhaustedAction is set to WHEN_EXHAUSTED_WAIT this can
68       * specify the maximum milliseconds the pool should block before throwing a
69       * NoSuchElementException
70       */
71      public static final long DEFAULT_MAX_POOL_WAIT = 4000;
72  
73      /**
74       * Specifies the behaviour of the Mule UMO pool when the pool is exhausted:
75       * <ul>
76       * <li>WHEN_EXHAUSTED_FAIL : will throw a NoSuchElementException</li>
77       * <li>WHEN_EXHAUSTED_WAIT : will block (invoke Object.wait(long) until a new or
78       * idle object is available.</li>
79       * <li>WHEN_EXHAUSTED_GROW : will create a new Mule and return it (essentially
80       * making maxActive meaningless).</li>
81       * </ul>
82       * If a positive maxWait value is supplied, it will block for at most that many
83       * milliseconds, after which a NoSuchElementException will be thrown. If maxWait
84       * is non-positive, it will block indefinitely.
85       */
86      public static final int DEFAULT_POOL_EXHAUSTED_ACTION = WHEN_EXHAUSTED_GROW;
87  
88      /**
89       * Determines how components in a pool should be initialised. The possible values
90       * are:
91       * <ul>
92       * <li>INITIALISE_NONE : Will not load any components in the pool on startup</li>
93       * <li>INITIALISE_ONE : Will load only the first service in the pool on
94       * startup</li>
95       * <li>INITIALISE_ALL : Will load all components in the pool on startup</li>
96       * </ul>
97       */
98      public static final int DEFAULT_POOL_INITIALISATION_POLICY = INITIALISE_ONE;
99  
100     // map pool exhaustion strings to their respective values
101     public static final Map POOL_EXHAUSTED_ACTIONS = new CaseInsensitiveMap()
102     {
103         private static final long serialVersionUID = 1L;
104 
105         // static initializer
106         {
107             // if the values were an actual enum in ObjectPool we could iterate
108             // properly.. :/
109 
110             Integer value = new Integer(WHEN_EXHAUSTED_WAIT);
111             this.put("WHEN_EXHAUSTED_WAIT", value);
112 
113             value = new Integer(WHEN_EXHAUSTED_FAIL);
114             this.put("WHEN_EXHAUSTED_FAIL", value);
115 
116             value = new Integer(WHEN_EXHAUSTED_GROW);
117             this.put("WHEN_EXHAUSTED_GROW", value);
118         }
119     };
120 
121     // map pool initialisation policy strings to their respective values
122     public static final Map POOL_INITIALISATION_POLICIES = new CaseInsensitiveMap()
123     {
124         private static final long serialVersionUID = 1L;
125 
126         // static initializer
127         {
128             Integer value = new Integer(INITIALISE_NONE);
129             this.put("INITIALISE_NONE", value);
130 
131             value = new Integer(INITIALISE_ONE);
132             this.put("INITIALISE_ONE", value);
133 
134             value = new Integer(INITIALISE_ALL);
135             this.put("INITIALISE_ALL", value);
136         }
137     };
138 
139     private int maxActive = DEFAULT_MAX_POOL_ACTIVE;
140 
141     private int maxIdle = DEFAULT_MAX_POOL_IDLE;
142 
143     private long maxWait = DEFAULT_MAX_POOL_WAIT;
144 
145     private int exhaustedAction = DEFAULT_POOL_EXHAUSTED_ACTION;
146 
147     private int initialisationPolicy = DEFAULT_POOL_INITIALISATION_POLICY;
148 
149 //    private UMOPoolFactory poolFactory = new CommonsPoolFactory();
150 
151     public PoolingProfile()
152     {
153         super();
154     }
155 
156     public PoolingProfile(PoolingProfile pp)
157     {
158         this.maxActive = pp.getMaxActive();
159         this.maxIdle = pp.getMaxIdle();
160         this.maxWait = pp.getMaxWait();
161         this.exhaustedAction = pp.getExhaustedAction();
162         this.initialisationPolicy = pp.getInitialisationPolicy();
163 //        if (pp.getPoolFactory() != null)
164 //        {
165 //            poolFactory = pp.getPoolFactory();
166 //        }
167     }
168 
169     public PoolingProfile(int maxActive,
170                           int maxIdle,
171                           long maxWait,
172                           int exhaustedAction,
173                           int initialisationPolicy)
174     {
175         this.maxActive = maxActive;
176         this.maxIdle = maxIdle;
177         this.maxWait = maxWait;
178         this.exhaustedAction = exhaustedAction;
179         this.initialisationPolicy = initialisationPolicy;
180     }
181 
182     /**
183      * @return max number of Mule UMOs that can be idle in a service
184      */
185     public int getMaxIdle()
186     {
187         return maxIdle;
188     }
189 
190     /**
191      * @return max number of Mule UMOs that can be active in a service
192      */
193     public int getMaxActive()
194     {
195         return maxActive;
196     }
197 
198     /**
199      * @return time in miilisconds to wait for a Mule UMO to be available in a
200      *         service when the pool of Mule UMOs is exhausted and the
201      *         PoolExhaustedAction is set to WHEN_EXHAUSTED_BLOCK
202      */
203     public long getMaxWait()
204     {
205         return maxWait;
206     }
207 
208     /**
209      * @return the action when the Mule UMO pool is exhaused for a service
210      */
211     public int getExhaustedAction()
212     {
213         return exhaustedAction;
214     }
215 
216     public int getInitialisationPolicy()
217     {
218         return initialisationPolicy;
219     }
220 
221     public void setInitialisationPolicy(int policy)
222     {
223         initialisationPolicy = policy;
224     }
225 
226     public void setMaxIdle(int maxIdle)
227     {
228         this.maxIdle = maxIdle;
229     }
230 
231     public void setMaxActive(int maxActive)
232     {
233         this.maxActive = maxActive;
234     }
235 
236     public void setMaxWait(long maxWait)
237     {
238         this.maxWait = maxWait;
239     }
240 
241     public void setExhaustedAction(int exhaustedAction)
242     {
243         this.exhaustedAction = exhaustedAction;
244     }
245 
246 //    public UMOPoolFactory getPoolFactory()
247 //    {
248 //        return poolFactory;
249 //    }
250 //
251 //    public void setPoolFactory(UMOPoolFactory poolFactory)
252 //    {
253 //        this.poolFactory = poolFactory;
254 //    }
255 
256 }