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