View Javadoc

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