View Javadoc

1   /*
2    * $Id: ThreadingProfile.java 12269 2008-07-10 04:19:03Z 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.api.config;
12  
13  import org.mule.api.context.WorkManager;
14  import org.mule.config.ImmutableThreadingProfile;
15  
16  import java.util.Map;
17  
18  import edu.emory.mathcs.backport.java.util.concurrent.RejectedExecutionHandler;
19  import edu.emory.mathcs.backport.java.util.concurrent.ThreadFactory;
20  import edu.emory.mathcs.backport.java.util.concurrent.ThreadPoolExecutor;
21  
22  import org.apache.commons.collections.map.CaseInsensitiveMap;
23  
24  /**
25   * Mule uses a few different pools i.e. for service threads and message dispatchers. This interface
26   * makes it easier to configure the pool.  Pools are created via
27   * {@link ImmutableThreadingProfile#createPool(String, ThreadingProfile)} (which
28   * should really be a separate factory).
29   *
30   * <p>{@link ImmutableThreadingProfile} is a simple read-only implementation that
31   * makes a local copy of any {@link org.mule.api.config.ThreadingProfile} passed to a consructor.</p>
32   *
33   * <p>{@link org.mule.config.ChainedThreadingProfile} is a mutable implementation that can take
34   * default values from an existing {@link org.mule.api.config.ThreadingProfile}.  The default values
35   * can be either dynamic (read whenever the value is queried) or static (a local copy of the
36   * default is made when the profile is first constructed).</p>
37   */
38  public interface ThreadingProfile
39  {
40  
41      /**
42       * Default value for MAX_THREADS_ACTIVE
43       */
44      int DEFAULT_MAX_THREADS_ACTIVE = 16;
45  
46      /**
47       * Default value for MAX_THREADS_IDLE
48       */
49      int DEFAULT_MAX_THREADS_IDLE = 1;
50  
51      /**
52       * Default value for MAX_BUFFER_SIZE
53       */
54      int DEFAULT_MAX_BUFFER_SIZE = 0;
55  
56      /**
57       * Default value for MAX_THREAD_TTL
58       */
59      long DEFAULT_MAX_THREAD_TTL = 60000;
60  
61      /**
62       * Default value for DEFAULT_THREAD_WAIT_TIMEOUT
63       */
64      long DEFAULT_THREAD_WAIT_TIMEOUT = 30000L;
65  
66      /**
67       * Default value for do threading
68       */
69      boolean DEFAULT_DO_THREADING = true;
70  
71      /**
72       * Actions to perform on pool exhaustion
73       */
74      int WHEN_EXHAUSTED_WAIT = 0;
75      int WHEN_EXHAUSTED_DISCARD = 1;
76      int WHEN_EXHAUSTED_DISCARD_OLDEST = 2;
77      int WHEN_EXHAUSTED_ABORT = 3;
78      int WHEN_EXHAUSTED_RUN = 4;
79  
80      /**
81       * Default action to perform on pool exhaustion
82       */
83      int DEFAULT_POOL_EXHAUST_ACTION = WHEN_EXHAUSTED_RUN;
84  
85      // map pool exhaustion strings to their respective values
86      Map POOL_EXHAUSTED_ACTIONS = new CaseInsensitiveMap()
87      {
88          private static final long serialVersionUID = 1L;
89  
90          // initializer
91          {
92              Integer value = new Integer(WHEN_EXHAUSTED_WAIT);
93              this.put("WHEN_EXHAUSTED_WAIT", value);
94              this.put("WAIT", value);
95  
96              value = new Integer(WHEN_EXHAUSTED_DISCARD);
97              this.put("WHEN_EXHAUSTED_DISCARD", value);
98              this.put("DISCARD", value);
99  
100             value = new Integer(WHEN_EXHAUSTED_DISCARD_OLDEST);
101             this.put("WHEN_EXHAUSTED_DISCARD_OLDEST", value);
102             this.put("DISCARD_OLDEST", value);
103 
104             value = new Integer(WHEN_EXHAUSTED_ABORT);
105             this.put("WHEN_EXHAUSTED_ABORT", value);
106             this.put("ABORT", value);
107 
108             value = new Integer(WHEN_EXHAUSTED_RUN);
109             this.put("WHEN_EXHAUSTED_RUN", value);
110             this.put("RUN", value);
111         }
112     };
113 
114     ThreadingProfile DEFAULT_THREADING_PROFILE =
115             new ImmutableThreadingProfile(
116                     DEFAULT_MAX_THREADS_ACTIVE,
117                     DEFAULT_MAX_THREADS_IDLE,
118                     DEFAULT_MAX_BUFFER_SIZE,
119                     DEFAULT_MAX_THREAD_TTL,
120                     DEFAULT_THREAD_WAIT_TIMEOUT,
121                     DEFAULT_POOL_EXHAUST_ACTION,
122                     DEFAULT_DO_THREADING,
123                     null,
124                     null
125                     );
126 
127     int getMaxThreadsActive();
128 
129     int getMaxThreadsIdle();
130 
131     long getThreadTTL();
132 
133     long getThreadWaitTimeout();
134 
135     int getPoolExhaustedAction();
136 
137     RejectedExecutionHandler getRejectedExecutionHandler();
138 
139     ThreadFactory getThreadFactory();
140 
141     void setMaxThreadsActive(int maxThreadsActive);
142 
143     void setMaxThreadsIdle(int maxThreadsIdle);
144 
145     void setThreadTTL(long threadTTL);
146 
147     void setThreadWaitTimeout(long threadWaitTimeout);
148 
149     void setPoolExhaustedAction(int poolExhaustPolicy);
150 
151     void setRejectedExecutionHandler(RejectedExecutionHandler rejectedExecutionHandler);
152 
153     void setThreadFactory(ThreadFactory threadFactory);
154 
155     int getMaxBufferSize();
156 
157     void setMaxBufferSize(int maxBufferSize);
158 
159     WorkManagerFactory getWorkManagerFactory();
160 
161     void setWorkManagerFactory(WorkManagerFactory workManagerFactory);
162 
163     WorkManager createWorkManager(String name);
164 
165     ThreadPoolExecutor createPool();
166 
167     ThreadPoolExecutor createPool(String name);
168 
169     boolean isDoThreading();
170 
171     void setDoThreading(boolean doThreading);
172 
173     interface WorkManagerFactory
174     {
175         WorkManager createWorkManager(ThreadingProfile profile, String name);
176     }
177 
178 }