View Javadoc

1   /*
2    * $Id: ImmutableThreadingProfile.java 21939 2011-05-18 13:32:09Z aperepel $
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  import org.mule.work.MuleWorkManager;
19  
20  import java.util.concurrent.ExecutorService;
21  import java.util.concurrent.RejectedExecutionHandler;
22  import java.util.concurrent.ThreadFactory;
23  
24  
25  public class ImmutableThreadingProfile implements ThreadingProfile
26  {
27  
28      private int maxThreadsActive;
29      private int maxThreadsIdle;
30      private int maxBufferSize;
31      private long threadTTL;
32      private long threadWaitTimeout;
33      private int poolExhaustedAction;
34      private boolean doThreading;
35  
36      private ThreadPoolFactory poolFactory = ThreadPoolFactory.newInstance();
37      private WorkManagerFactory workManagerFactory = new DefaultWorkManagerFactory();
38      private RejectedExecutionHandler rejectedExecutionHandler;
39      private ThreadFactory threadFactory;
40      private MuleContext muleContext;
41  
42      public ImmutableThreadingProfile(int maxThreadsActive,
43                              int maxThreadsIdle,
44                              int maxBufferSize,
45                              long threadTTL,
46                              long threadWaitTimeout,
47                              int poolExhaustedAction,
48                              boolean doThreading,
49                              RejectedExecutionHandler rejectedExecutionHandler,
50                              ThreadFactory threadFactory)
51      {
52          this.maxThreadsActive = maxThreadsActive;
53          this.maxThreadsIdle = maxThreadsIdle;
54          this.maxBufferSize = maxBufferSize;
55          this.threadTTL = threadTTL;
56          this.threadWaitTimeout = threadWaitTimeout;
57          this.poolExhaustedAction = poolExhaustedAction;
58          this.doThreading = doThreading;
59          this.rejectedExecutionHandler = rejectedExecutionHandler;
60          this.threadFactory = threadFactory;
61      }
62  
63      public ImmutableThreadingProfile(ThreadingProfile tp)
64      {
65          this(tp.getMaxThreadsActive(),
66                  tp.getMaxThreadsIdle(),
67                  tp.getMaxBufferSize(),
68                  tp.getThreadTTL(),
69                  tp.getThreadWaitTimeout(),
70                  tp.getPoolExhaustedAction(),
71                  tp.isDoThreading(),
72                  tp.getRejectedExecutionHandler(),
73                  tp.getThreadFactory());
74      }
75  
76      public int getMaxThreadsActive()
77      {
78          return maxThreadsActive;
79      }
80  
81      public int getMaxThreadsIdle()
82      {
83          return maxThreadsIdle;
84      }
85  
86      public long getThreadTTL()
87      {
88          return threadTTL;
89      }
90  
91      public long getThreadWaitTimeout()
92      {
93          return threadWaitTimeout;
94      }
95  
96      public int getPoolExhaustedAction()
97      {
98          return poolExhaustedAction;
99      }
100 
101     public RejectedExecutionHandler getRejectedExecutionHandler()
102     {
103         return rejectedExecutionHandler;
104     }
105 
106     public ThreadFactory getThreadFactory()
107     {
108         return threadFactory;
109     }
110 
111     public void setMaxThreadsActive(int maxThreadsActive)
112     {
113         throw new UnsupportedOperationException(getClass().getName());
114     }
115 
116     public void setMaxThreadsIdle(int maxThreadsIdle)
117     {
118         throw new UnsupportedOperationException(getClass().getName());
119     }
120 
121     public void setThreadTTL(long threadTTL)
122     {
123         throw new UnsupportedOperationException(getClass().getName());
124     }
125 
126     public void setThreadWaitTimeout(long threadWaitTimeout)
127     {
128         throw new UnsupportedOperationException(getClass().getName());
129     }
130 
131     public void setPoolExhaustedAction(int poolExhaustPolicy)
132     {
133         throw new UnsupportedOperationException(getClass().getName());
134     }
135 
136     public void setRejectedExecutionHandler(RejectedExecutionHandler rejectedExecutionHandler)
137     {
138         throw new UnsupportedOperationException(getClass().getName());
139     }
140 
141     public void setThreadFactory(ThreadFactory threadFactory)
142     {
143         throw new UnsupportedOperationException(getClass().getName());
144     }
145 
146     public int getMaxBufferSize()
147     {
148         return maxBufferSize;
149     }
150 
151     public void setMaxBufferSize(int maxBufferSize)
152     {
153         throw new UnsupportedOperationException(getClass().getName());
154     }
155 
156     public WorkManagerFactory getWorkManagerFactory()
157     {
158         return workManagerFactory;
159     }
160 
161     public void setWorkManagerFactory(WorkManagerFactory workManagerFactory)
162     {
163         throw new UnsupportedOperationException(getClass().getName());
164     }
165 
166     public WorkManager createWorkManager(String name, int shutdownTimeout)
167     {
168         return workManagerFactory.createWorkManager(new ImmutableThreadingProfile(this), name, shutdownTimeout);
169     }
170 
171     public ExecutorService createPool()
172     {
173         return createPool(null);
174     }
175 
176     public ExecutorService createPool(String name)
177     {
178         return poolFactory.createPool(name, new ImmutableThreadingProfile(this));
179     }
180 
181     public boolean isDoThreading()
182     {
183         return doThreading;
184     }
185 
186     public void setDoThreading(boolean doThreading)
187     {
188         throw new UnsupportedOperationException(getClass().getName());
189     }
190 
191     public ThreadPoolFactory getPoolFactory()
192     {
193         return poolFactory;
194     }
195 
196     public void setMuleContext(MuleContext context)
197     {
198         this.muleContext = context;
199 
200         // propagate mule context
201         if (this.workManagerFactory instanceof MuleContextAware)
202         {
203             ((MuleContextAware) workManagerFactory).setMuleContext(muleContext);
204         }
205 
206         poolFactory.setMuleContext(muleContext);
207     }
208 
209     public MuleContext getMuleContext()
210     {
211         return muleContext;
212     }
213 
214     public String toString()
215     {
216         return "ThreadingProfile{" + "maxThreadsActive=" + maxThreadsActive + ", maxThreadsIdle="
217                         + maxThreadsIdle + ", maxBufferSize=" + maxBufferSize + ", threadTTL=" + threadTTL
218                         + ", poolExhaustedAction=" + poolExhaustedAction + ", threadWaitTimeout="
219                         + threadWaitTimeout + ", doThreading=" + doThreading + ", workManagerFactory="
220                         + workManagerFactory + ", rejectedExecutionHandler=" + rejectedExecutionHandler
221                         + ", threadFactory=" + threadFactory + "}";
222     }
223 
224     public static class DefaultWorkManagerFactory implements WorkManagerFactory, MuleContextAware
225     {
226 
227         protected MuleContext muleContext;
228 
229         public WorkManager createWorkManager(ThreadingProfile profile, String name, int shutdownTimeout)
230         {
231             final WorkManager workManager = new MuleWorkManager(profile, name, shutdownTimeout);
232             if (muleContext != null)
233             {
234                 MuleContextAware contextAware = (MuleContextAware) workManager;
235                 contextAware.setMuleContext(muleContext);
236             }
237 
238             return workManager;
239         }
240 
241         public void setMuleContext(MuleContext context)
242         {
243             this.muleContext = context;
244         }
245     }
246 
247 }