KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > resource > work > JBossWorkManager


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.resource.work;
23
24 import javax.management.ObjectName JavaDoc;
25 import javax.resource.spi.work.ExecutionContext JavaDoc;
26 import javax.resource.spi.work.Work JavaDoc;
27 import javax.resource.spi.work.WorkException JavaDoc;
28 import javax.resource.spi.work.WorkListener JavaDoc;
29 import javax.resource.spi.work.WorkManager JavaDoc;
30 import javax.transaction.xa.Xid JavaDoc;
31
32 import org.jboss.system.ServiceMBeanSupport;
33 import org.jboss.tm.JBossXATerminator;
34 import org.jboss.util.threadpool.Task;
35 import org.jboss.util.threadpool.ThreadPool;
36
37 /**
38  * The work manager implementation
39  *
40  * @author <a HREF="mailto:adrian@jboss.org">Adrian Brock</a>
41  * @version $Revision: 38343 $
42  */

43 public class JBossWorkManager extends ServiceMBeanSupport implements WorkManager JavaDoc, JBossWorkManagerMBean
44 {
45    /** Whether trace is enabled */
46    private boolean trace = log.isTraceEnabled();
47    
48    /** The thread pool */
49    private ThreadPool threadPool;
50
51    /** The thread pool name */
52    private ObjectName JavaDoc threadPoolName;
53
54    /** The xa terminator */
55    private JBossXATerminator xaTerminator;
56
57    /** The xa terminator name */
58    private ObjectName JavaDoc xaTerminatorName;
59
60    /**
61     * Retrieve the thread pool
62     *
63     * @return the thread pool
64     */

65    public ThreadPool getThreadPool()
66    {
67       return threadPool;
68    }
69
70    /**
71     * Set the thread pool
72     *
73     * @param threadPool the thread pool
74     */

75    public void setThreadPool(ThreadPool threadPool)
76    {
77       this.threadPool = threadPool;
78    }
79
80    public ObjectName JavaDoc getThreadPoolName()
81    {
82       return threadPoolName;
83    }
84
85    public void setThreadPoolName(ObjectName JavaDoc threadPoolName)
86    {
87       this.threadPoolName = threadPoolName;
88    }
89
90    public ObjectName JavaDoc getXATerminatorName()
91    {
92       return xaTerminatorName;
93    }
94
95    public void setXATerminatorName(ObjectName JavaDoc xaTerminatorName)
96    {
97       this.xaTerminatorName = xaTerminatorName;
98    }
99
100    public WorkManager JavaDoc getInstance()
101    {
102       return this;
103    }
104
105    public void doWork(Work JavaDoc work, long startTimeout, ExecutionContext JavaDoc ctx, WorkListener JavaDoc listener) throws WorkException JavaDoc
106    {
107       if (ctx == null)
108          ctx = new ExecutionContext JavaDoc();
109       WorkWrapper wrapper = new WorkWrapper(this, work, Task.WAIT_FOR_COMPLETE, startTimeout, ctx, listener);
110       importWork(wrapper);
111       executeWork(wrapper);
112       if (wrapper.getWorkException() != null)
113          throw wrapper.getWorkException();
114    }
115
116    public void doWork(Work JavaDoc work) throws WorkException JavaDoc
117    {
118       doWork(work, WorkManager.INDEFINITE, null, null);
119    }
120
121    public long startWork(Work JavaDoc work, long startTimeout, ExecutionContext JavaDoc ctx, WorkListener JavaDoc listener) throws WorkException JavaDoc
122    {
123       if (ctx == null)
124          ctx = new ExecutionContext JavaDoc();
125       WorkWrapper wrapper = new WorkWrapper(this, work, Task.WAIT_FOR_START, startTimeout, ctx, listener);
126       importWork(wrapper);
127       executeWork(wrapper);
128       if (wrapper.getWorkException() != null)
129          throw wrapper.getWorkException();
130       return wrapper.getBlockedElapsed();
131    }
132
133    public long startWork(Work JavaDoc work) throws WorkException JavaDoc
134    {
135       return startWork(work, WorkManager.INDEFINITE, null, null);
136    }
137
138    public void scheduleWork(Work JavaDoc work, long startTimeout, ExecutionContext JavaDoc ctx, WorkListener JavaDoc listener) throws WorkException JavaDoc
139    {
140       if (ctx == null)
141          ctx = new ExecutionContext JavaDoc();
142       WorkWrapper wrapper = new WorkWrapper(this, work, Task.WAIT_NONE, startTimeout, ctx, listener);
143       importWork(wrapper);
144       executeWork(wrapper);
145       if (wrapper.getWorkException() != null)
146          throw wrapper.getWorkException();
147    }
148
149    public void scheduleWork(Work JavaDoc work) throws WorkException JavaDoc
150    {
151       scheduleWork(work, WorkManager.INDEFINITE, null, null);
152    }
153
154    protected void startService() throws Exception JavaDoc
155    {
156       if (threadPoolName == null)
157          throw new IllegalStateException JavaDoc("No thread pool name");
158
159       threadPool = (ThreadPool) server.getAttribute(threadPoolName, "Instance");
160
161       if (xaTerminatorName == null)
162          throw new IllegalStateException JavaDoc("No xa terminator name");
163
164       xaTerminator = (JBossXATerminator) server.getAttribute(xaTerminatorName, "XATerminator");
165    }
166
167    /**
168     * Import any work
169     *
170     * @param wrapper the work wrapper
171     * @throws WorkException for any error
172     */

173    protected void importWork(WorkWrapper wrapper) throws WorkException JavaDoc
174    {
175       trace = log.isTraceEnabled();
176       if (trace)
177          log.trace("Importing work " + wrapper);
178       
179       ExecutionContext JavaDoc ctx = wrapper.getExecutionContext();
180       if (ctx != null)
181       {
182          Xid JavaDoc xid = ctx.getXid();
183          if (xid != null)
184          {
185             long timeout = ctx.getTransactionTimeout();
186             xaTerminator.registerWork(wrapper.getWork(), xid, timeout);
187          }
188       }
189       if (trace)
190          log.trace("Imported work " + wrapper);
191    }
192    
193    /**
194     * Execute the work
195     *
196     * @param wrapper the work wrapper
197     * @throws WorkException for any error
198     */

199    protected void executeWork(WorkWrapper wrapper) throws WorkException JavaDoc
200    {
201       if (trace)
202          log.trace("Submitting work to thread pool " + wrapper);
203
204       threadPool.runTaskWrapper(wrapper);
205
206       if (trace)
207          log.trace("Submitted work to thread pool " + wrapper);
208    }
209
210    /**
211     * Start work
212     *
213     * @param wrapper the work wrapper
214     * @throws WorkException for any error
215     */

216    protected void startWork(WorkWrapper wrapper) throws WorkException JavaDoc
217    {
218       if (trace)
219          log.trace("Starting work " + wrapper);
220
221       ExecutionContext JavaDoc ctx = wrapper.getExecutionContext();
222       if (ctx != null)
223       {
224          Xid JavaDoc xid = ctx.getXid();
225          if (xid != null)
226          {
227             xaTerminator.startWork(wrapper.getWork(), xid);
228          }
229       }
230       if (trace)
231          log.trace("Started work " + wrapper);
232    }
233
234    /**
235     * End work
236     *
237     * @param wrapper the work wrapper
238     * @throws WorkException for any error
239     */

240    protected void endWork(WorkWrapper wrapper)
241    {
242       if (trace)
243          log.trace("Ending work " + wrapper);
244
245       ExecutionContext JavaDoc ctx = wrapper.getExecutionContext();
246       if (ctx != null)
247       {
248          Xid JavaDoc xid = ctx.getXid();
249          if (xid != null)
250          {
251             xaTerminator.endWork(wrapper.getWork(), xid);
252          }
253       }
254       if (trace)
255          log.trace("Ended work " + wrapper);
256    }
257
258    /**
259     * Cancel work
260     *
261     * @param wrapper the work wrapper
262     * @throws WorkException for any error
263     */

264    protected void cancelWork(WorkWrapper wrapper)
265    {
266       if (trace)
267          log.trace("Cancel work " + wrapper);
268
269       ExecutionContext JavaDoc ctx = wrapper.getExecutionContext();
270       if (ctx != null)
271       {
272          Xid JavaDoc xid = ctx.getXid();
273          if (xid != null)
274          {
275             xaTerminator.cancelWork(wrapper.getWork(), xid);
276          }
277       }
278       if (trace)
279          log.trace("Canceled work " + wrapper);
280    }
281 }
282
Popular Tags