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.config;
8   
9   import org.mule.api.MuleContext;
10  import org.mule.api.config.ThreadingProfile;
11  import org.mule.api.context.MuleContextAware;
12  import org.mule.api.context.WorkManager;
13  import org.mule.config.pool.ThreadPoolFactory;
14  
15  import edu.emory.mathcs.backport.java.util.concurrent.ExecutorService;
16  import edu.emory.mathcs.backport.java.util.concurrent.RejectedExecutionHandler;
17  import edu.emory.mathcs.backport.java.util.concurrent.ThreadFactory;
18  
19  /**
20   * This was written (perhaps too far in advance) with an eye to how we will manage default values
21   * in a dynamic environment.  Since very little has been decided in that direction the correct
22   * behaviour is unclear - changing the default value of "dynamic"
23   * {@link org.mule.config.ChainedThreadingProfile#ChainedThreadingProfile(ThreadingProfile)}
24   * will switch behaviour between dynamic and static chaining.
25   *
26   * <p>Note that within Spring, as far as I understand things, object creation is always ordered
27   * so that dependencies are correctly resolved.  In that case, in a static scenario (or in a
28   * dynamic one that rebuilds the instances) dynamic and static behaviour should be identical.</p>
29   *
30   * <p>Also, the "lazy" chaining is an optimisation - all hierarchies should be grounded in a final
31   * default which is {@link ImmutableThreadingProfile} and, as such, return reliable
32   * values (lazy would be necessary if this is not the case, since we must avoid evaluating
33   * incomplete delegates).</p>
34   */
35  public class ChainedThreadingProfile implements ThreadingProfile
36  {
37  
38      private Integer maxThreadsActive;
39      private Integer maxThreadsIdle;
40      private Integer maxBufferSize;
41      private Long threadTTL;
42      private Long threadWaitTimeout;
43      private Integer poolExhaustedAction;
44      private Boolean doThreading;
45  
46      private ThreadPoolFactory poolFactory = ThreadPoolFactory.newInstance();
47      private WorkManagerFactory workManagerFactory = new ImmutableThreadingProfile.DefaultWorkManagerFactory();
48      private RejectedExecutionHandler rejectedExecutionHandler;
49      private ThreadFactory threadFactory;
50  
51      private ThreadingProfile delegate;
52  
53      private MuleContext muleContext;
54  
55      /**
56       * Generate a mutable threading profile with fixed default values taken from
57       * {@link #DEFAULT_THREADING_PROFILE}
58       */
59      public ChainedThreadingProfile()
60      {
61          // the default is itself constant, so dynanmic=true irrelevant
62          this(DEFAULT_THREADING_PROFILE);
63      }
64  
65      /**
66       * Generate a mutable threading profile with dynamic default values taken from the
67       * given delegate.
68       *
69       */
70      public ChainedThreadingProfile(ThreadingProfile delegate)
71      {
72          this(delegate, true);
73      }
74  
75      /**
76       * Generate a mutable threading profile.  Default values are taken from the "delegate"
77       * argument.  If dynamic is true then changes in the delegate instance are reflected in
78       * this instance.
79       * 
80       * @param delegate Source of default values.
81       * @param dynamic If true, changes in delegate are reflected in this instance
82       */
83      public ChainedThreadingProfile(ThreadingProfile delegate, boolean dynamic)
84      {
85          if (!dynamic)
86          {
87              // for static dependencies, we delegate to a fixed copy
88              delegate = new ImmutableThreadingProfile(delegate);
89          }
90          this.delegate = delegate;
91      }
92  
93      public int getMaxThreadsActive()
94      {
95          return null != maxThreadsActive ? maxThreadsActive : delegate.getMaxThreadsActive();
96      }
97  
98      public int getMaxThreadsIdle()
99      {
100         return null != maxThreadsIdle ? maxThreadsIdle : delegate.getMaxThreadsIdle();
101     }
102 
103     public long getThreadTTL()
104     {
105         return null != threadTTL ? threadTTL : delegate.getThreadTTL();
106     }
107 
108     public long getThreadWaitTimeout()
109     {
110         return null != threadWaitTimeout ? threadWaitTimeout : delegate.getThreadWaitTimeout();
111     }
112 
113     public int getPoolExhaustedAction()
114     {
115         return null != poolExhaustedAction ? poolExhaustedAction : delegate.getPoolExhaustedAction();
116     }
117 
118     public RejectedExecutionHandler getRejectedExecutionHandler()
119     {
120         return rejectedExecutionHandler;
121     }
122 
123     public ThreadFactory getThreadFactory()
124     {
125         return threadFactory;
126     }
127 
128     public void setMaxThreadsActive(int maxThreadsActive)
129     {
130         this.maxThreadsActive = maxThreadsActive;
131     }
132 
133     public void setMaxThreadsIdle(int maxThreadsIdle)
134     {
135         this.maxThreadsIdle = maxThreadsIdle;
136     }
137 
138     public void setThreadTTL(long threadTTL)
139     {
140         this.threadTTL = threadTTL;
141     }
142 
143     public void setThreadWaitTimeout(long threadWaitTimeout)
144     {
145         this.threadWaitTimeout = threadWaitTimeout;
146     }
147 
148     public void setPoolExhaustedAction(int poolExhaustPolicy)
149     {
150         this.poolExhaustedAction = poolExhaustPolicy;
151     }
152 
153     public void setRejectedExecutionHandler(RejectedExecutionHandler rejectedExecutionHandler)
154     {
155         this.rejectedExecutionHandler = rejectedExecutionHandler;
156     }
157 
158     public void setThreadFactory(ThreadFactory threadFactory)
159     {
160         this.threadFactory = threadFactory;
161     }
162 
163     public int getMaxBufferSize()
164     {
165         return null != maxBufferSize ? maxBufferSize : delegate.getMaxBufferSize();
166     }
167 
168     public void setMaxBufferSize(int maxBufferSize)
169     {
170         this.maxBufferSize = maxBufferSize;
171     }
172 
173     public WorkManagerFactory getWorkManagerFactory()
174     {
175         return workManagerFactory;
176     }
177 
178     public void setWorkManagerFactory(WorkManagerFactory workManagerFactory)
179     {
180         this.workManagerFactory = workManagerFactory;
181     }
182 
183     public WorkManager createWorkManager(String name, int shutdownTimeout)
184     {
185         // we deliberately don't instantiate the chained profile as we just want a cloned copy, not recursion
186         return workManagerFactory.createWorkManager(new ImmutableThreadingProfile(this), name, shutdownTimeout);
187     }
188 
189     public ExecutorService createPool()
190     {
191         return createPool(null);
192     }
193 
194     public ExecutorService createPool(String name)
195     {
196         // we deliberately don't instantiate the chained profile as we just want a cloned copy, not recursion
197         return poolFactory.createPool(name, new ImmutableThreadingProfile(this));
198     }
199 
200     public boolean isDoThreading()
201     {
202         return null != doThreading ? doThreading : delegate.isDoThreading();
203     }
204 
205     public void setDoThreading(boolean doThreading)
206     {
207         this.doThreading = doThreading;
208     }
209 
210     public ThreadPoolFactory getPoolFactory()
211     {
212         return poolFactory;
213     }
214 
215     public MuleContext getMuleContext()
216     {
217         return muleContext;
218     }
219 
220     public void setMuleContext(MuleContext muleContext)
221     {
222         this.muleContext = muleContext;
223 
224         // propagate mule context
225         if (this.workManagerFactory instanceof MuleContextAware)
226         {
227             ((MuleContextAware) workManagerFactory).setMuleContext(muleContext);
228         }
229 
230         poolFactory.setMuleContext(muleContext);
231     }
232 
233     public String toString()
234     {
235         return "ThreadingProfile{" + "maxThreadsActive=" + maxThreadsActive + ", maxThreadsIdle="
236                         + maxThreadsIdle + ", maxBufferSize=" + maxBufferSize + ", threadTTL=" + threadTTL
237                         + ", poolExhaustedAction=" + poolExhaustedAction + ", threadWaitTimeout="
238                         + threadWaitTimeout + ", doThreading=" + doThreading + ", workManagerFactory="
239                         + workManagerFactory + ", rejectedExecutionHandler=" + rejectedExecutionHandler
240                         + ", threadFactory=" + threadFactory + "}";
241     }
242 
243 }