View Javadoc

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