View Javadoc

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