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