FileDocCategorySizeDatePackage
WorkWrapper.javaAPI DocJBoss 4.2.19139Fri Jul 13 20:53:50 BST 2007org.jboss.ejb3.embedded.resource

WorkWrapper

public class WorkWrapper extends org.jboss.util.threadpool.BasicTaskWrapper implements org.jboss.util.threadpool.Task
Wraps the resource adapter's work.
author
Adrian Brock
version
$Revision: 60233 $

Fields Summary
private static final Logger
log
The log
private boolean
trace
Whether we are tracing
private javax.resource.spi.work.Work
work
The work
private javax.resource.spi.work.ExecutionContext
executionContext
The execution context
private javax.resource.spi.work.WorkListener
workListener
the work listener
private long
startTimeout
The start timeout
private JBossWorkManager
workManager
The work manager
private int
waitType
The wait type
private long
blockedTime
The blocked time
private javax.resource.spi.work.WorkException
exception
Any exception
Constructors Summary
public WorkWrapper(JBossWorkManager workManager, javax.resource.spi.work.Work work, int waitType, long startTimeout, javax.resource.spi.work.ExecutionContext executionContext, javax.resource.spi.work.WorkListener workListener)
Create a new WorkWrapper

param
workManager the work manager
param
work the work
param
waitType the waitType
param
executionContext the execution context
param
workListener the WorkListener
throws
IllegalArgumentException for null work, execution context or a negative start timeout


                                             
               
   
      super();

      if (work == null)
         throw new IllegalArgumentException("Null work");
      if (executionContext == null)
         throw new IllegalArgumentException("Null execution context");
      if (startTimeout < 0)
         throw new IllegalArgumentException("Illegal start timeout: " + startTimeout);

      this.workManager = workManager;
      this.work = work;
      this.waitType = waitType;
      this.startTimeout = startTimeout;
      this.executionContext = executionContext;
      this.workListener = workListener;

      setTask(this);
   
Methods Summary
public voidaccepted(long time)

      blockedTime = time;

      if (trace)
         log.trace("Accepted work " + this);

      if (workListener != null)
      {
         WorkEvent event = new WorkEvent(workManager, WorkEvent.WORK_ACCEPTED, work, null);
         workListener.workAccepted(event);
      }
   
public voidcompleted(long time, java.lang.Throwable throwable)

 
      if (waitType == WAIT_FOR_COMPLETE)
         blockedTime = time;

      if (throwable != null)
         exception = new WorkCompletedException(throwable);

      if (trace)
         log.trace("Completed work " + this);

      if (workListener != null)
      {
         WorkEvent event = new WorkEvent(workManager, WorkEvent.WORK_COMPLETED, work, exception);
         workListener.workCompleted(event);
      }
   
public voidexecute()

  
      if (trace)
         log.trace("Executing work " + this);
      try
      {
         workManager.startWork(this);
      }
      catch (WorkException e)
      {
         e.printStackTrace();
         taskRejected(new NestedRuntimeException(e));
         return;
      }
      try
      {
         work.run();
      }
      finally
      {
         workManager.endWork(this);
      }
      if (trace)
         log.trace("Executed work " + this);
   
public longgetBlockedElapsed()
Retrieve the time blocked

return
the blocked time

      return blockedTime;
   
public longgetCompletionTimeout()

      return executionContext.getTransactionTimeout();
   
public javax.resource.spi.work.ExecutionContextgetExecutionContext()
Retrieve the exection context

return
the execution context

      return executionContext;
   
public intgetPriority()

      return Thread.NORM_PRIORITY;
   
public longgetStartTimeout()

      return startTimeout;
   
public intgetWaitType()

      return waitType;
   
public javax.resource.spi.work.WorkgetWork()
Retrieve the work

return
the work

      return work;
   
public javax.resource.spi.work.WorkExceptiongetWorkException()
Get any exception

return
the exception or null if there is none

      return exception;
   
public javax.resource.spi.work.WorkListenergetWorkListener()
Retrieve the work listener

return
the WorkListener

      return workListener;
   
public JBossWorkManagergetWorkManager()
Get the work manager

return
the work manager

      return workManager;
   
public voidrejected(long time, java.lang.Throwable throwable)

      blockedTime = time;

      if (trace)
      {
         if (throwable != null)
            log.trace("Rejecting work " + this, throwable);
         else
            log.trace("Rejecting work " + this);
      }

      if (throwable != null)
      {
         exception = new WorkRejectedException(throwable);
         if (throwable instanceof StartTimeoutException)
            exception.setErrorCode(WorkRejectedException.START_TIMED_OUT);
      }
      
      workManager.cancelWork(this);
      
      if (workListener != null)
      {
         WorkEvent event = new WorkEvent(workManager, WorkEvent.WORK_ACCEPTED, work, exception);
         workListener.workRejected(event);
      }
   
public voidstarted(long time)

      if (waitType != WAIT_NONE)
         blockedTime = time;

      if (workListener != null)
      {
         WorkEvent event = new WorkEvent(workManager, WorkEvent.WORK_STARTED, work, null);
         workListener.workStarted(event);
      }
   
public voidstop()

      if (trace)
         log.trace("Stopping work " + this);

      work.release();
   
public java.lang.StringtoString()

      JBossStringBuilder buffer = new JBossStringBuilder(100);
      buffer.append("WorkWrapper@").append(Integer.toHexString(System.identityHashCode(this)));
      buffer.append("[workManger=").append(workManager);
      buffer.append(" work=").append(work);
      buffer.append(" state=").append(getStateString());
      if (executionContext != null && executionContext.getXid() != null)
      {
         buffer.append(" xid=").append(executionContext.getXid());
         buffer.append(" txTimeout=").append(executionContext.getTransactionTimeout());
      }
      buffer.append(" waitType=");
      switch (waitType)
      {
         case WAIT_NONE:
         {
            buffer.append("WAIT_NONE");
            break;
         }
         case WAIT_FOR_START:
         {
            buffer.append("WAIT_FOR_START");
            break;
         }
         case WAIT_FOR_COMPLETE:
         {
            buffer.append("WAIT_FOR_COMPLETE");
            break;
         }
         default:
            buffer.append("???");
      }
      if (startTimeout != WorkManager.INDEFINITE)
         buffer.append(" startTimeout=").append(startTimeout);
      long completionTimeout = getCompletionTimeout();
      if (completionTimeout != -1)
         buffer.append(" completionTimeout=").append(completionTimeout);
      if (blockedTime != 0)
         buffer.append(" blockTime=").append(blockedTime);
      buffer.append(" elapsedTime=").append(getElapsedTime());
      if (workListener != null)
         buffer.append(" workListener=").append(workListener);
      if (exception != null)
         buffer.append(" exception=").append(exception);
      buffer.append("]");
      return buffer.toString();