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