View Javadoc

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