KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > impl > work > MuleWorkManager


1 /*
2  * $Id: MuleWorkManager.java 4259 2006-12-14 03:12:07Z aperepel $
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 MuleSource MPL
7  * license, a copy of which has been included with this distribution in the
8  * LICENSE.txt file.
9  */

10
11 /**
12  *
13  * Copyright 2004 The Apache Software Foundation
14  *
15  * Licensed under the Apache License, Version 2.0 (the "License");
16  * you may not use this file except in compliance with the License.
17  * You may obtain a copy of the License at
18  *
19  * http://www.apache.org/licenses/LICENSE-2.0
20  *
21  * Unless required by applicable law or agreed to in writing, software
22  * distributed under the License is distributed on an "AS IS" BASIS,
23  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24  * See the License for the specific language governing permissions and
25  * limitations under the License.
26  */

27
28 package org.mule.impl.work;
29
30 import edu.emory.mathcs.backport.java.util.concurrent.Executor;
31
32 import org.apache.commons.logging.Log;
33 import org.apache.commons.logging.LogFactory;
34 import org.mule.MuleManager;
35 import org.mule.config.ThreadingProfile;
36 import org.mule.umo.UMOException;
37 import org.mule.umo.manager.UMOWorkManager;
38
39 import javax.resource.spi.XATerminator JavaDoc;
40 import javax.resource.spi.work.ExecutionContext JavaDoc;
41 import javax.resource.spi.work.Work JavaDoc;
42 import javax.resource.spi.work.WorkCompletedException JavaDoc;
43 import javax.resource.spi.work.WorkException JavaDoc;
44 import javax.resource.spi.work.WorkListener JavaDoc;
45
46 /**
47  * <code>MuleWorkManager</code> is a JCA Work manager implementation used to manage
48  * thread allocation for Mule components and connectors This code has been adapted
49  * from the Geronimo implementation, and the different pool executors have be used
50  * directly from Geronimo.
51  */

52 public class MuleWorkManager implements UMOWorkManager
53 {
54     /**
55      * logger used by this class
56      */

57     protected static final Log logger = LogFactory.getLog(MuleWorkManager.class);
58
59     /**
60      * Pool of threads used by this MuleWorkManager in order to process the Work
61      * instances submitted via the doWork methods.
62      */

63     private WorkExecutorPool syncWorkExecutorPool;
64
65     /**
66      * Pool of threads used by this MuleWorkManager in order to process the Work
67      * instances submitted via the startWork methods.
68      */

69     private WorkExecutorPool startWorkExecutorPool;
70
71     /**
72      * Pool of threads used by this MuleWorkManager in order to process the Work
73      * instances submitted via the scheduleWork methods.
74      */

75     private WorkExecutorPool scheduledWorkExecutorPool;
76
77     private final WorkExecutor scheduleWorkExecutor = new ScheduleWorkExecutor();
78     private final WorkExecutor startWorkExecutor = new StartWorkExecutor();
79     private final WorkExecutor syncWorkExecutor = new SyncWorkExecutor();
80
81     /**
82      * Create a MuleWorkManager.
83      */

84     public MuleWorkManager()
85     {
86         this(MuleManager.getConfiguration().getDefaultThreadingProfile(), null);
87     }
88
89     public MuleWorkManager(ThreadingProfile profile, String JavaDoc name)
90     {
91         if (name == null)
92         {
93             name = "WorkManager#" + hashCode();
94         }
95         syncWorkExecutorPool = new NullWorkExecutorPool(profile, name);
96         startWorkExecutorPool = new NullWorkExecutorPool(profile, name);
97         scheduledWorkExecutorPool = new NullWorkExecutorPool(profile, name);
98     }
99
100     public void start() throws UMOException
101     {
102         syncWorkExecutorPool = syncWorkExecutorPool.start();
103         startWorkExecutorPool = startWorkExecutorPool.start();
104         scheduledWorkExecutorPool = scheduledWorkExecutorPool.start();
105     }
106
107     public void stop() throws UMOException
108     {
109         syncWorkExecutorPool = syncWorkExecutorPool.stop();
110         startWorkExecutorPool = startWorkExecutorPool.stop();
111         scheduledWorkExecutorPool = scheduledWorkExecutorPool.stop();
112     }
113
114     public void dispose()
115     {
116         try
117         {
118             stop();
119         }
120         catch (UMOException e)
121         {
122             logger.warn("Error while disposing Work Manager: " + e.getMessage(), e);
123
124         }
125     }
126
127     // TODO
128
public XATerminator JavaDoc getXATerminator()
129     {
130         return null;
131     }
132
133     public int getSyncThreadCount()
134     {
135         return syncWorkExecutorPool.getPoolSize();
136     }
137
138     public int getSyncMaximumPoolSize()
139     {
140         return syncWorkExecutorPool.getMaximumPoolSize();
141     }
142
143     public void setSyncMaximumPoolSize(int maxSize)
144     {
145         syncWorkExecutorPool.setMaximumPoolSize(maxSize);
146     }
147
148     public int getStartThreadCount()
149     {
150         return startWorkExecutorPool.getPoolSize();
151     }
152
153     public int getStartMaximumPoolSize()
154     {
155         return startWorkExecutorPool.getMaximumPoolSize();
156     }
157
158     public void setStartMaximumPoolSize(int maxSize)
159     {
160         startWorkExecutorPool.setMaximumPoolSize(maxSize);
161     }
162
163     public int getScheduledThreadCount()
164     {
165         return scheduledWorkExecutorPool.getPoolSize();
166     }
167
168     public int getScheduledMaximumPoolSize()
169     {
170         return scheduledWorkExecutorPool.getMaximumPoolSize();
171     }
172
173     public void setScheduledMaximumPoolSize(int maxSize)
174     {
175         scheduledWorkExecutorPool.setMaximumPoolSize(maxSize);
176     }
177
178     /*
179      * (non-Javadoc)
180      *
181      * @see javax.resource.spi.work.MuleWorkManager#doWork(javax.resource.spi.work.Work)
182      */

183     public void doWork(Work JavaDoc work) throws WorkException JavaDoc
184     {
185         executeWork(new WorkerContext(work), syncWorkExecutor, syncWorkExecutorPool);
186     }
187
188     /*
189      * (non-Javadoc)
190      *
191      * @see javax.resource.spi.work.MuleWorkManager#doWork(javax.resource.spi.work.Work,
192      * long, javax.resource.spi.work.ExecutionContext,
193      * javax.resource.spi.work.WorkListener)
194      */

195     public void doWork(Work JavaDoc work, long startTimeout, ExecutionContext JavaDoc execContext, WorkListener JavaDoc workListener)
196         throws WorkException JavaDoc
197     {
198         WorkerContext workWrapper = new WorkerContext(work, startTimeout, execContext, workListener);
199         workWrapper.setThreadPriority(Thread.currentThread().getPriority());
200         executeWork(workWrapper, syncWorkExecutor, syncWorkExecutorPool);
201     }
202
203     /*
204      * (non-Javadoc)
205      *
206      * @see javax.resource.spi.work.MuleWorkManager#startWork(javax.resource.spi.work.Work)
207      */

208     public long startWork(Work JavaDoc work) throws WorkException JavaDoc
209     {
210         WorkerContext workWrapper = new WorkerContext(work);
211         workWrapper.setThreadPriority(Thread.currentThread().getPriority());
212         executeWork(workWrapper, startWorkExecutor, startWorkExecutorPool);
213         return System.currentTimeMillis() - workWrapper.getAcceptedTime();
214     }
215
216     /*
217      * (non-Javadoc)
218      *
219      * @see javax.resource.spi.work.MuleWorkManager#startWork(javax.resource.spi.work.Work,
220      * long, javax.resource.spi.work.ExecutionContext,
221      * javax.resource.spi.work.WorkListener)
222      */

223     public long startWork(Work JavaDoc work,
224                           long startTimeout,
225                           ExecutionContext JavaDoc execContext,
226                           WorkListener JavaDoc workListener) throws WorkException JavaDoc
227     {
228         WorkerContext workWrapper = new WorkerContext(work, startTimeout, execContext, workListener);
229         workWrapper.setThreadPriority(Thread.currentThread().getPriority());
230         executeWork(workWrapper, startWorkExecutor, startWorkExecutorPool);
231         return System.currentTimeMillis() - workWrapper.getAcceptedTime();
232     }
233
234     /*
235      * (non-Javadoc)
236      *
237      * @see javax.resource.spi.work.MuleWorkManager#scheduleWork(javax.resource.spi.work.Work)
238      */

239     public void scheduleWork(Work JavaDoc work) throws WorkException JavaDoc
240     {
241         WorkerContext workWrapper = new WorkerContext(work);
242         workWrapper.setThreadPriority(Thread.currentThread().getPriority());
243         executeWork(workWrapper, scheduleWorkExecutor, scheduledWorkExecutorPool);
244     }
245
246     /*
247      * (non-Javadoc)
248      *
249      * @see javax.resource.spi.work.MuleWorkManager#scheduleWork(javax.resource.spi.work.Work,
250      * long, javax.resource.spi.work.ExecutionContext,
251      * javax.resource.spi.work.WorkListener)
252      */

253     public void scheduleWork(Work JavaDoc work,
254                              long startTimeout,
255                              ExecutionContext JavaDoc execContext,
256                              WorkListener JavaDoc workListener) throws WorkException JavaDoc
257     {
258         WorkerContext workWrapper = new WorkerContext(work, startTimeout, execContext, workListener);
259         workWrapper.setThreadPriority(Thread.currentThread().getPriority());
260         executeWork(workWrapper, scheduleWorkExecutor, scheduledWorkExecutorPool);
261     }
262
263     /**
264      * Execute the specified Work.
265      *
266      * @param work Work to be executed.
267      * @exception WorkException Indicates that the Work execution has been
268      * unsuccessful.
269      */

270     private void executeWork(WorkerContext work, WorkExecutor workExecutor, Executor pooledExecutor)
271         throws WorkException JavaDoc
272     {
273         work.workAccepted(this);
274         try
275         {
276             workExecutor.doExecute(work, pooledExecutor);
277             WorkException JavaDoc exception = work.getWorkException();
278             if (null != exception)
279             {
280                 throw exception;
281             }
282         }
283         catch (InterruptedException JavaDoc e)
284         {
285             WorkCompletedException JavaDoc wcj = new WorkCompletedException JavaDoc("The execution has been interrupted.", e);
286             wcj.setErrorCode(WorkException.INTERNAL);
287             throw wcj;
288         }
289     }
290 }
291
Popular Tags