1 22 package org.jboss.ejb3.embedded.resource; 23 24 import javax.management.ObjectName ; 25 import javax.resource.spi.work.ExecutionContext ; 26 import javax.resource.spi.work.Work ; 27 import javax.resource.spi.work.WorkException ; 28 import javax.resource.spi.work.WorkListener ; 29 import javax.resource.spi.work.WorkManager ; 30 import javax.transaction.xa.Xid ; 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 41 public class JBossWorkManager implements WorkManager 42 { 43 private static final Logger log = Logger.getLogger(Ejb3DeploymentInfo.class); 44 45 private ThreadPool threadPool = new org.jboss.util.threadpool.BasicThreadPool(); 46 47 48 private JBossXATerminator xaTerminator; 49 50 51 private ObjectName xaTerminatorName; 52 53 public ObjectName getXATerminatorName() 54 { 55 return xaTerminatorName; 56 } 57 58 public void setXATerminatorName(ObjectName xaTerminatorName) 59 { 60 this.xaTerminatorName = xaTerminatorName; 61 } 62 63 public WorkManager getInstance() 64 { 65 return this; 66 } 67 68 public void doWork(Work work, long startTimeout, ExecutionContext ctx, WorkListener listener) throws WorkException 69 { 70 if (ctx == null) 71 ctx = new ExecutionContext (); 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 work) throws WorkException 80 { 81 doWork(work, WorkManager.INDEFINITE, null, null); 82 } 83 84 public long startWork(Work work, long startTimeout, ExecutionContext ctx, WorkListener listener) throws WorkException 85 { 86 if (ctx == null) 87 ctx = new ExecutionContext (); 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 work) throws WorkException 97 { 98 return startWork(work, WorkManager.INDEFINITE, null, null); 99 } 100 101 public void scheduleWork(Work work, long startTimeout, ExecutionContext ctx, WorkListener listener) throws WorkException 102 { 103 if (ctx == null) 104 ctx = new ExecutionContext (); 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 work) throws WorkException 113 { 114 scheduleWork(work, WorkManager.INDEFINITE, null, null); 115 } 116 117 123 protected void importWork(WorkWrapper wrapper) throws WorkException 124 { 125 ExecutionContext ctx = wrapper.getExecutionContext(); 126 if (ctx != null) 127 { 128 Xid 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 143 protected void executeWork(WorkWrapper wrapper) throws WorkException 144 { 145 threadPool.runTaskWrapper(wrapper); 146 } 147 148 154 protected void startWork(WorkWrapper wrapper) throws WorkException 155 { 156 ExecutionContext ctx = wrapper.getExecutionContext(); 157 if (ctx != null) 158 { 159 Xid xid = ctx.getXid(); 160 if (xid != null) 161 { 162 xaTerminator.startWork(wrapper.getWork(), xid); 163 } 164 } 165 } 166 167 173 protected void endWork(WorkWrapper wrapper) 174 { 175 ExecutionContext ctx = wrapper.getExecutionContext(); 176 if (ctx != null) 177 { 178 Xid xid = ctx.getXid(); 179 if (xid != null) 180 { 181 xaTerminator.endWork(wrapper.getWork(), xid); 182 } 183 } 184 } 185 186 192 protected void cancelWork(WorkWrapper wrapper) 193 { 194 ExecutionContext ctx = wrapper.getExecutionContext(); 195 if (ctx != null) 196 { 197 Xid xid = ctx.getXid(); 198 if (xid != null) 199 { 200 xaTerminator.cancelWork(wrapper.getWork(), xid); 201 } 202 } 203 } 204 } 205 | Popular Tags |