View Javadoc

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