KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb3 > embedded > resource > 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.ejb3.embedded.resource;
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.logging.Logger;
33 import org.jboss.tm.JBossXATerminator;
34 import org.jboss.util.threadpool.Task;
35 import org.jboss.util.threadpool.ThreadPool;
36
37 /**
38  * @version <tt>$Revision: 45712 $</tt>
39  * @author <a HREF="mailto:bdecoste@jboss.com">William DeCoste</a>
40  */

41 public class JBossWorkManager implements WorkManager JavaDoc
42 {
43    private static final Logger log = Logger.getLogger(Ejb3DeploymentInfo.class);
44    
45    private ThreadPool threadPool = new org.jboss.util.threadpool.BasicThreadPool();
46
47    /** The xa terminator */
48    private JBossXATerminator xaTerminator;
49
50    /** The xa terminator name */
51    private ObjectName JavaDoc xaTerminatorName;
52
53    public ObjectName JavaDoc getXATerminatorName()
54    {
55       return xaTerminatorName;
56    }
57
58    public void setXATerminatorName(ObjectName JavaDoc xaTerminatorName)
59    {
60       this.xaTerminatorName = xaTerminatorName;
61    }
62
63    public WorkManager JavaDoc getInstance()
64    {
65       return this;
66    }
67
68    public void doWork(Work JavaDoc work, long startTimeout, ExecutionContext JavaDoc ctx, WorkListener JavaDoc listener) throws WorkException JavaDoc
69    {
70       if (ctx == null)
71          ctx = new ExecutionContext JavaDoc();
72       WorkWrapper wrapper = new WorkWrapper(this, work, Task.WAIT_FOR_COMPLETE, startTimeout, ctx, listener);
73       importWork(wrapper);
74       executeWork(wrapper);
75       if (wrapper.getWorkException() != null)
76          throw wrapper.getWorkException();
77    }
78
79    public void doWork(Work JavaDoc work) throws WorkException JavaDoc
80    {
81       doWork(work, WorkManager.INDEFINITE, null, null);
82    }
83
84    public long startWork(Work JavaDoc work, long startTimeout, ExecutionContext JavaDoc ctx, WorkListener JavaDoc listener) throws WorkException JavaDoc
85    {
86       if (ctx == null)
87          ctx = new ExecutionContext JavaDoc();
88       WorkWrapper wrapper = new WorkWrapper(this, work, Task.WAIT_FOR_START, startTimeout, ctx, listener);
89       importWork(wrapper);
90       executeWork(wrapper);
91       if (wrapper.getWorkException() != null)
92          throw wrapper.getWorkException();
93       return wrapper.getBlockedElapsed();
94    }
95
96    public long startWork(Work JavaDoc work) throws WorkException JavaDoc
97    {
98       return startWork(work, WorkManager.INDEFINITE, null, null);
99    }
100
101    public void scheduleWork(Work JavaDoc work, long startTimeout, ExecutionContext JavaDoc ctx, WorkListener JavaDoc listener) throws WorkException JavaDoc
102    {
103       if (ctx == null)
104          ctx = new ExecutionContext JavaDoc();
105       WorkWrapper wrapper = new WorkWrapper(this, work, Task.WAIT_NONE, startTimeout, ctx, listener);
106       importWork(wrapper);
107       executeWork(wrapper);
108       if (wrapper.getWorkException() != null)
109          throw wrapper.getWorkException();
110    }
111
112    public void scheduleWork(Work JavaDoc work) throws WorkException JavaDoc
113    {
114       scheduleWork(work, WorkManager.INDEFINITE, null, null);
115    }
116
117    /**
118     * Import any work
119     *
120     * @param wrapper the work wrapper
121     * @throws WorkException for any error
122     */

123    protected void importWork(WorkWrapper wrapper) throws WorkException JavaDoc
124    {
125       ExecutionContext JavaDoc ctx = wrapper.getExecutionContext();
126       if (ctx != null)
127       {
128          Xid JavaDoc xid = ctx.getXid();
129          if (xid != null)
130          {
131             long timeout = ctx.getTransactionTimeout();
132             xaTerminator.registerWork(wrapper.getWork(), xid, timeout);
133          }
134       }
135    }
136    
137    /**
138     * Execute the work
139     *
140     * @param wrapper the work wrapper
141     * @throws WorkException for any error
142     */

143    protected void executeWork(WorkWrapper wrapper) throws WorkException JavaDoc
144    {
145       threadPool.runTaskWrapper(wrapper);
146    }
147
148    /**
149     * Start work
150     *
151     * @param wrapper the work wrapper
152     * @throws WorkException for any error
153     */

154    protected void startWork(WorkWrapper wrapper) throws WorkException JavaDoc
155    {
156       ExecutionContext JavaDoc ctx = wrapper.getExecutionContext();
157       if (ctx != null)
158       {
159          Xid JavaDoc xid = ctx.getXid();
160          if (xid != null)
161          {
162             xaTerminator.startWork(wrapper.getWork(), xid);
163          }
164       }
165    }
166
167    /**
168     * End work
169     *
170     * @param wrapper the work wrapper
171     * @throws WorkException for any error
172     */

173    protected void endWork(WorkWrapper wrapper)
174    {
175       ExecutionContext JavaDoc ctx = wrapper.getExecutionContext();
176       if (ctx != null)
177       {
178          Xid JavaDoc xid = ctx.getXid();
179          if (xid != null)
180          {
181             xaTerminator.endWork(wrapper.getWork(), xid);
182          }
183       }
184    }
185
186    /**
187     * Cancel work
188     *
189     * @param wrapper the work wrapper
190     * @throws WorkException for any error
191     */

192    protected void cancelWork(WorkWrapper wrapper)
193    {
194       ExecutionContext JavaDoc ctx = wrapper.getExecutionContext();
195       if (ctx != null)
196       {
197          Xid JavaDoc xid = ctx.getXid();
198          if (xid != null)
199          {
200             xaTerminator.cancelWork(wrapper.getWork(), xid);
201          }
202       }
203    }
204 }
205
Popular Tags