View Javadoc

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