View Javadoc

1   /*
2    * $Id: MutableThreadingProfile.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.WorkManager;
16  import org.mule.config.pool.ThreadPoolFactory;
17  
18  import edu.emory.mathcs.backport.java.util.concurrent.ExecutorService;
19  import edu.emory.mathcs.backport.java.util.concurrent.RejectedExecutionHandler;
20  import edu.emory.mathcs.backport.java.util.concurrent.ThreadFactory;
21  
22  /**
23   * Partial mutability for a threading profile. A typical use case for this class is to
24   * copy an existing immutable threading profile via a copying constructor, modify values and
25   * reconfigure a thread pool (if the latter supports it).<p/>
26   * <p/>The following parameters are copied locally and can be modified directly:
27   * <ul>
28   *  <li>{@link #maxThreadsActive}
29   *  <li>{@link #maxThreadsIdle}
30   *  <li>{@link #maxBufferSize}
31   *  <li>{@link #threadTTL}
32   *  <li>{@link #threadWaitTimeout}
33   *  <li>{@link #poolExhaustedAction}
34   *  <li>{@link #doThreading}
35   * </ul>
36   * <p/>The following parameters re-use the same object reference as the original threading
37   * profile and <strong>are not deep clones</strong> of those:
38   * <ul>
39   *  <li>{@link #poolFactory}
40   *  <li>{@link #workManagerFactory}
41   *  <li>{@link #rejectedExecutionHandler}
42   *  <li>{@link #threadFactory}
43   * </ul>
44   */
45  public class MutableThreadingProfile implements ThreadingProfile
46  {
47  
48      private int maxThreadsActive;
49      private int maxThreadsIdle;
50      private int maxBufferSize;
51      private long threadTTL;
52      private long threadWaitTimeout;
53      private int poolExhaustedAction;
54      private boolean doThreading;
55  
56      private ThreadPoolFactory poolFactory;
57      private WorkManagerFactory workManagerFactory;
58      private RejectedExecutionHandler rejectedExecutionHandler;
59      private ThreadFactory threadFactory;
60      private MuleContext muleContext;
61  
62      public MutableThreadingProfile(ThreadingProfile tp)
63      {
64          this.maxThreadsActive = tp.getMaxThreadsActive();
65          this.maxThreadsIdle = tp.getMaxThreadsIdle();
66          this.maxBufferSize = tp.getMaxBufferSize();
67          this.threadTTL = tp.getThreadTTL();
68          this.threadWaitTimeout = tp.getThreadWaitTimeout();
69          this.poolExhaustedAction = tp.getPoolExhaustedAction();
70          this.doThreading = tp.isDoThreading();
71          this.rejectedExecutionHandler = tp.getRejectedExecutionHandler();
72          this.threadFactory = tp.getThreadFactory();
73          this.workManagerFactory = tp.getWorkManagerFactory();
74          this.poolFactory = tp.getPoolFactory();
75      }
76  
77      public ExecutorService createPool()
78      {
79          return createPool(null);
80      }
81  
82      public int getMaxThreadsActive()
83      {
84          return maxThreadsActive;
85      }
86  
87      public int getMaxThreadsIdle()
88      {
89          return maxThreadsIdle;
90      }
91  
92      public long getThreadTTL()
93      {
94          return threadTTL;
95      }
96  
97      public long getThreadWaitTimeout()
98      {
99          return threadWaitTimeout;
100     }
101 
102     public int getPoolExhaustedAction()
103     {
104         return poolExhaustedAction;
105     }
106 
107     public RejectedExecutionHandler getRejectedExecutionHandler()
108     {
109         return rejectedExecutionHandler;
110     }
111 
112     public ThreadFactory getThreadFactory()
113     {
114         return threadFactory;
115     }
116 
117     public void setMaxThreadsActive(int maxThreadsActive)
118     {
119         this.maxThreadsActive = maxThreadsActive;
120     }
121 
122     public void setMaxThreadsIdle(int maxThreadsIdle)
123     {
124         this.maxThreadsIdle = maxThreadsIdle;
125     }
126 
127     public void setThreadTTL(long threadTTL)
128     {
129         this.threadTTL = threadTTL;
130     }
131 
132     public void setThreadWaitTimeout(long threadWaitTimeout)
133     {
134         this.threadWaitTimeout = threadWaitTimeout;
135     }
136 
137     public void setPoolExhaustedAction(int poolExhaustedAction)
138     {
139         this.poolExhaustedAction = poolExhaustedAction;
140     }
141 
142     public void setRejectedExecutionHandler(RejectedExecutionHandler rejectedExecutionHandler)
143     {
144         this.rejectedExecutionHandler = rejectedExecutionHandler;
145     }
146 
147     public void setThreadFactory(ThreadFactory threadFactory)
148     {
149         this.threadFactory = threadFactory;
150     }
151 
152     public int getMaxBufferSize()
153     {
154         return maxBufferSize;
155     }
156 
157     public void setMaxBufferSize(int maxBufferSize)
158     {
159         this.maxBufferSize = maxBufferSize;
160     }
161 
162     public WorkManagerFactory getWorkManagerFactory()
163     {
164         return workManagerFactory;
165     }
166 
167     public void setWorkManagerFactory(WorkManagerFactory workManagerFactory)
168     {
169         this.workManagerFactory = workManagerFactory;
170     }
171 
172     public WorkManager createWorkManager(String name, int shutdownTimeout)
173     {
174         return workManagerFactory.createWorkManager(new ImmutableThreadingProfile(this), name, shutdownTimeout);
175     }
176 
177     public ExecutorService createPool(String name)
178     {
179         return poolFactory.createPool(name, new ImmutableThreadingProfile(this));
180     }
181 
182     public boolean isDoThreading()
183     {
184         return doThreading;
185     }
186 
187     public void setDoThreading(boolean doThreading)
188     {
189         this.doThreading = doThreading;
190     }
191 
192     public ThreadPoolFactory getPoolFactory()
193     {
194         return poolFactory;
195     }
196 
197     public void setMuleContext(MuleContext muleContext)
198     {
199         this.muleContext = muleContext;
200     }
201 
202     public MuleContext getMuleContext()
203     {
204         return muleContext;
205     }
206 }