1 10 11 27 28 package org.mule.impl.work; 29 30 import org.apache.commons.logging.Log; 31 import org.apache.commons.logging.LogFactory; 32 import org.mule.util.concurrent.Latch; 33 34 import javax.resource.spi.work.ExecutionContext ; 35 import javax.resource.spi.work.Work ; 36 import javax.resource.spi.work.WorkAdapter ; 37 import javax.resource.spi.work.WorkCompletedException ; 38 import javax.resource.spi.work.WorkEvent ; 39 import javax.resource.spi.work.WorkException ; 40 import javax.resource.spi.work.WorkListener ; 41 import javax.resource.spi.work.WorkRejectedException ; 42 43 46 public class WorkerContext implements Work 47 { 48 49 52 protected static final Log logger = LogFactory.getLog(WorkerContext.class); 53 54 57 private static final WorkListener NULL_WORK_LISTENER = new WorkAdapter () 58 { 59 public void workRejected(WorkEvent event) 60 { 61 if (event.getException() != null) 62 { 63 if (event.getException() instanceof WorkCompletedException 64 && event.getException().getCause() != null) 65 { 66 logger.error(event.getWork().toString(), event.getException().getCause()); 67 } 68 else 69 { 70 logger.error(event.getWork().toString(), event.getException()); 71 } 72 } 73 } 74 }; 75 76 79 private int threadPriority; 80 81 84 private Work worker; 85 86 89 private long acceptedTime; 90 91 94 private int retryCount; 95 96 100 private long startTimeOut; 101 102 105 private final ExecutionContext executionContext; 106 107 110 private WorkListener workListener = NULL_WORK_LISTENER; 111 112 115 private WorkException workException; 116 117 120 private final Latch startLatch = new Latch(); 121 122 125 private final Latch endLatch = new Latch(); 126 127 132 public WorkerContext(Work work) 133 { 134 worker = work; 135 executionContext = null; 136 } 137 138 149 public WorkerContext(Work aWork, 150 long aStartTimeout, 151 ExecutionContext execContext, 152 WorkListener workListener) 153 { 154 worker = aWork; 155 startTimeOut = aStartTimeout; 156 executionContext = execContext; 157 if (null != workListener) 158 { 159 this.workListener = workListener; 160 } 161 } 162 163 168 public void release() 169 { 170 worker.release(); 171 } 172 173 181 public void setThreadPriority(int aPriority) 182 { 183 threadPriority = aPriority; 184 } 185 186 194 public int getThreadPriority() 195 { 196 return threadPriority; 197 } 198 199 206 public synchronized void workAccepted(Object anObject) 207 { 208 acceptedTime = System.currentTimeMillis(); 210 workListener.workAccepted(new WorkEvent (anObject, WorkEvent.WORK_ACCEPTED, worker, null)); 211 } 212 213 219 public synchronized long getAcceptedTime() 220 { 221 return acceptedTime; 222 } 223 224 230 public long getStartTimeout() 231 { 232 return startTimeOut; 233 } 234 235 242 public synchronized boolean isTimedOut() 243 { 244 245 if (0 == startTimeOut || startTimeOut == MuleWorkManager.INDEFINITE) 248 { 249 return false; 250 } 251 boolean isTimeout = acceptedTime + startTimeOut > 0 252 && System.currentTimeMillis() > acceptedTime + startTimeOut; 253 if (logger.isDebugEnabled()) 254 { 255 logger.debug(this + " accepted at " + acceptedTime 256 + (isTimeout ? " has timed out." : " has not timed out. ") + retryCount 257 + " retries have been performed."); 258 } 259 if (isTimeout) 260 { 261 workException = new WorkRejectedException (this + " has timed out.", WorkException.START_TIMED_OUT); 262 workListener.workRejected(new WorkEvent (this, WorkEvent.WORK_REJECTED, worker, workException)); 263 return true; 264 } 265 retryCount++; 266 return isTimeout; 267 } 268 269 274 public synchronized WorkException getWorkException() 275 { 276 return workException; 277 } 278 279 284 public void run() 285 { 286 if (isTimedOut()) 287 { 288 startLatch.countDown(); 291 endLatch.countDown(); 292 return; 293 } 294 workListener.workStarted(new WorkEvent (this, WorkEvent.WORK_STARTED, worker, null)); 298 startLatch.countDown(); 299 try 303 { 304 if (executionContext == null || executionContext.getXid() == null) 305 { 306 try 309 { 310 worker.run(); 311 } 312 finally 313 { 314 } 321 } 324 else 325 { 326 try 351 { 352 worker.run(); 353 } 354 finally 355 { 356 } 358 359 } 360 workListener.workCompleted(new WorkEvent (this, WorkEvent.WORK_COMPLETED, worker, null)); 361 } 362 catch (Throwable e) 363 { 364 workException = (WorkException )(e instanceof WorkCompletedException 365 ? e : new WorkCompletedException ("Unknown error", 366 WorkCompletedException.UNDEFINED).initCause(e)); 367 workListener.workCompleted(new WorkEvent (this, WorkEvent.WORK_REJECTED, worker, workException)); 368 } 369 finally 370 { 371 endLatch.countDown(); 372 } 373 } 374 375 381 public Latch provideStartLatch() 382 { 383 return startLatch; 384 } 385 386 392 public Latch provideEndLatch() 393 { 394 return endLatch; 395 } 396 397 public String toString() 398 { 399 return "Work: " + worker; 400 } 401 } 402 | Popular Tags |