1 22 package org.jboss.ejb3.embedded.resource; 23 24 import javax.resource.spi.work.ExecutionContext ; 25 import javax.resource.spi.work.Work ; 26 import javax.resource.spi.work.WorkCompletedException ; 27 import javax.resource.spi.work.WorkEvent ; 28 import javax.resource.spi.work.WorkException ; 29 import javax.resource.spi.work.WorkListener ; 30 import javax.resource.spi.work.WorkManager ; 31 import javax.resource.spi.work.WorkRejectedException ; 32 33 import org.jboss.logging.Logger; 34 import org.jboss.util.JBossStringBuilder; 35 import org.jboss.util.NestedRuntimeException; 36 import org.jboss.util.threadpool.BasicTaskWrapper; 37 import org.jboss.util.threadpool.StartTimeoutException; 38 import org.jboss.util.threadpool.Task; 39 40 46 public class WorkWrapper extends BasicTaskWrapper implements Task 47 { 48 49 private static final Logger log = Logger.getLogger(WorkWrapper.class); 50 51 52 private boolean trace = log.isTraceEnabled(); 53 54 55 private Work work; 56 57 58 private ExecutionContext executionContext; 59 60 61 private WorkListener workListener; 62 63 64 private long startTimeout; 65 66 67 private JBossWorkManager workManager; 68 69 70 private int waitType; 71 72 73 private long blockedTime; 74 75 76 private WorkException exception; 77 78 88 public WorkWrapper(JBossWorkManager workManager, Work work, int waitType, long startTimeout, ExecutionContext executionContext, WorkListener workListener) 89 { 90 super(); 91 92 if (work == null) 93 throw new IllegalArgumentException ("Null work"); 94 if (executionContext == null) 95 throw new IllegalArgumentException ("Null execution context"); 96 if (startTimeout < 0) 97 throw new IllegalArgumentException ("Illegal start timeout: " + startTimeout); 98 99 this.workManager = workManager; 100 this.work = work; 101 this.waitType = waitType; 102 this.startTimeout = startTimeout; 103 this.executionContext = executionContext; 104 this.workListener = workListener; 105 106 setTask(this); 107 } 108 109 114 public JBossWorkManager getWorkManager() 115 { 116 return workManager; 117 } 118 119 124 public Work getWork() 125 { 126 return work; 127 } 128 129 134 public WorkListener getWorkListener() 135 { 136 return workListener; 137 } 138 139 144 public ExecutionContext getExecutionContext() 145 { 146 return executionContext; 147 } 148 149 154 public long getBlockedElapsed() 155 { 156 return blockedTime; 157 } 158 159 164 public WorkException getWorkException() 165 { 166 return exception; 167 } 168 169 public int getWaitType() 170 { 171 return waitType; 172 } 173 174 public int getPriority() 175 { 176 return Thread.NORM_PRIORITY; 177 } 178 179 public long getStartTimeout() 180 { 181 return startTimeout; 182 } 183 184 public long getCompletionTimeout() 185 { 186 return executionContext.getTransactionTimeout(); 187 } 188 189 public void execute() 190 { 191 if (trace) 192 log.trace("Executing work " + this); 193 try 194 { 195 workManager.startWork(this); 196 } 197 catch (WorkException e) 198 { 199 e.printStackTrace(); 200 taskRejected(new NestedRuntimeException(e)); 201 return; 202 } 203 try 204 { 205 work.run(); 206 } 207 finally 208 { 209 workManager.endWork(this); 210 } 211 if (trace) 212 log.trace("Executed work " + this); 213 } 214 215 public void stop() 216 { 217 if (trace) 218 log.trace("Stopping work " + this); 219 220 work.release(); 221 } 222 223 public void accepted(long time) 224 { 225 blockedTime = time; 226 227 if (trace) 228 log.trace("Accepted work " + this); 229 230 if (workListener != null) 231 { 232 WorkEvent event = new WorkEvent (workManager, WorkEvent.WORK_ACCEPTED, work, null); 233 workListener.workAccepted(event); 234 } 235 } 236 237 public void rejected(long time, Throwable throwable) 238 { 239 blockedTime = time; 240 241 if (trace) 242 { 243 if (throwable != null) 244 log.trace("Rejecting work " + this, throwable); 245 else 246 log.trace("Rejecting work " + this); 247 } 248 249 if (throwable != null) 250 { 251 exception = new WorkRejectedException (throwable); 252 if (throwable instanceof StartTimeoutException) 253 exception.setErrorCode(WorkRejectedException.START_TIMED_OUT); 254 } 255 256 workManager.cancelWork(this); 257 258 if (workListener != null) 259 { 260 WorkEvent event = new WorkEvent (workManager, WorkEvent.WORK_ACCEPTED, work, exception); 261 workListener.workRejected(event); 262 } 263 } 264 265 public void started(long time) 266 { 267 if (waitType != WAIT_NONE) 268 blockedTime = time; 269 270 if (workListener != null) 271 { 272 WorkEvent event = new WorkEvent (workManager, WorkEvent.WORK_STARTED, work, null); 273 workListener.workStarted(event); 274 } 275 } 276 277 public void completed(long time, Throwable throwable) 278 { 279 if (waitType == WAIT_FOR_COMPLETE) 280 blockedTime = time; 281 282 if (throwable != null) 283 exception = new WorkCompletedException (throwable); 284 285 if (trace) 286 log.trace("Completed work " + this); 287 288 if (workListener != null) 289 { 290 WorkEvent event = new WorkEvent (workManager, WorkEvent.WORK_COMPLETED, work, exception); 291 workListener.workCompleted(event); 292 } 293 } 294 295 public String toString() 296 { 297 JBossStringBuilder buffer = new JBossStringBuilder(100); 298 buffer.append("WorkWrapper@").append(Integer.toHexString(System.identityHashCode(this))); 299 buffer.append("[workManger=").append(workManager); 300 buffer.append(" work=").append(work); 301 buffer.append(" state=").append(getStateString()); 302 if (executionContext != null && executionContext.getXid() != null) 303 { 304 buffer.append(" xid=").append(executionContext.getXid()); 305 buffer.append(" txTimeout=").append(executionContext.getTransactionTimeout()); 306 } 307 buffer.append(" waitType="); 308 switch (waitType) 309 { 310 case WAIT_NONE: 311 { 312 buffer.append("WAIT_NONE"); 313 break; 314 } 315 case WAIT_FOR_START: 316 { 317 buffer.append("WAIT_FOR_START"); 318 break; 319 } 320 case WAIT_FOR_COMPLETE: 321 { 322 buffer.append("WAIT_FOR_COMPLETE"); 323 break; 324 } 325 default: 326 buffer.append("???"); 327 } 328 if (startTimeout != WorkManager.INDEFINITE) 329 buffer.append(" startTimeout=").append(startTimeout); 330 long completionTimeout = getCompletionTimeout(); 331 if (completionTimeout != -1) 332 buffer.append(" completionTimeout=").append(completionTimeout); 333 if (blockedTime != 0) 334 buffer.append(" blockTime=").append(blockedTime); 335 buffer.append(" elapsedTime=").append(getElapsedTime()); 336 if (workListener != null) 337 buffer.append(" workListener=").append(workListener); 338 if (exception != null) 339 buffer.append(" exception=").append(exception); 340 buffer.append("]"); 341 return buffer.toString(); 342 } 343 } 344 | Popular Tags |