Methods Summary |
---|
private boolean | checkStateBeforeLocking()It is possible that state is modified just before
the lock is obtained. So check it again.
Access the variable directly to avoid nested locking.
if (waitMode == WAIT_UNTIL_FINISH) {
return state < COMPLETED;
}
if (waitMode == WAIT_UNTIL_START) {
return state < STARTED;
}
return false;
|
public WorkException | getException()Retrieves the exception created during the work's execution.
return exception;
|
public synchronized int | getState()Retrieves the state of the work coordinator object.
return state;
|
private boolean | isTimedOut()
return getState() == TIMEDOUT;
|
public void | lock()Lock the thread upto the end of execution or start of work
execution.
if (!lockRequired()) {
return;
}
try {
synchronized (lock) {
if (checkStateBeforeLocking()) {
if (timeout != -1) {
lock.wait(timeout);
} else {
lock.wait();
}
}
}
if (getState() < STARTED) {
workTimedOut();
}
if (lockRequired()) {
synchronized (lock) {
if (checkStateBeforeLocking()) {
lock.wait();
}
}
}
} catch(Exception e) {
setException(e);
}
|
private boolean | lockRequired()
if (!proceed()) {
return false;
}
if (waitMode == NO_WAIT) {
return false;
}
if (waitMode == WAIT_UNTIL_FINISH) {
return getState() < COMPLETED;
}
if (waitMode == WAIT_UNTIL_START) {
return getState() < STARTED;
}
return false;
|
public void | postInvoke()Post-invoke operation. This does the following after the work is executed.
1. Releases the transaction with JTS.
2. Generates work completed event.
3. Clear the thread context.
boolean txImported = (ec != null && ec.getXid() != null);
try {
J2EETransactionManager tm = Switch.getSwitch().getTransactionManager();
if (txImported) {
tm.release(ec.getXid());
}
} catch (WorkException ex) {
setException(ex);
} finally {
try {
if(workStats != null) {
workStats.setActiveWorkCount
(--workStats.currentActiveWorkCount);
workStats.completedWorkCount++;
}
//If exception is not null, the work has already been rejected.
if (listener != null) {
if ((!isTimedOut()) && (exception == null)) {
listener.workCompleted(
new WorkEvent(this, WorkEvent.WORK_COMPLETED, work,
getException()));
}
}
//Also release the TX from the record of TX Optimizer
if (txImported) {
J2EETransactionManager tm =
Switch.getSwitch().getTransactionManager();
if (tm instanceof J2EETransactionManagerOpt) {
((J2EETransactionManagerOpt) tm).clearThreadTx();
}
}
} catch(Exception e) {
logger.log(Level.WARNING, e.getMessage());
}
}
setState(COMPLETED);
if (waitMode == WAIT_UNTIL_FINISH) {
unLock();
}
|
public void | preInvoke()Pre-invoke operation. This does the following
1. Notifies the WorkManager.startWork method.
2. Checks whether the wok has already been timed out.
3. Recreates the transaction with JTS.
// If the work is just scheduled, check whether it has timed out or not.
if (waitMode == NO_WAIT && timeout > -1) {
long elapsedTime = System.currentTimeMillis() - startTime ;
if(workStats != null) {
workStats.setWorkWaitTime(elapsedTime);
}
if (elapsedTime > timeout) {
workTimedOut();
}
}
// Change the status to started.
setState(STARTED);
if (waitMode == WAIT_UNTIL_START) {
unLock();
}
// If the work is timed out then return.
if (!proceed()) {
if (workStats != null) {
workStats.decrementWaitQueueLength();
}
return;
}
// All set to do start the work. So send the event.
if (listener != null) {
listener.workStarted(
new WorkEvent(this, WorkEvent.WORK_STARTED, work, null));
}
try {
J2EETransactionManager tm = Switch.getSwitch().getTransactionManager();
if (ec != null && ec.getXid() != null) {
tm.recreate(ec.getXid(), ec.getTransactionTimeout());
}
} catch(WorkException we) {
this.exception = we;
} catch(Exception e) {
setException(e);
}
if(workStats != null) {
workStats.setActiveWorkCount(++workStats.currentActiveWorkCount);
workStats.decrementWaitQueueLength();
}
|
public boolean | proceed()Checks the work is good to proceed with further processing.
return !isTimedOut() && exception == null;
|
public void | setException(java.lang.Throwable e)Accepts an exception object and converts to a
WorkException object.
if (getState() < STARTED ) {
if (e instanceof WorkRejectedException) {
exception = (WorkException) e;
} else if (e instanceof WorkException) {
WorkException we = (WorkException) e;
exception = new WorkRejectedException(we);
exception.setErrorCode(we.getErrorCode());
} else {
exception = new WorkRejectedException(e);
exception.setErrorCode(WorkException.UNDEFINED);
}
} else {
if (e instanceof WorkCompletedException) {
exception = (WorkException) e;
} else if (e instanceof WorkException) {
WorkException we = (WorkException) e;
exception = new WorkCompletedException(we);
exception.setErrorCode(we.getErrorCode());
} else {
exception = new WorkCompletedException(e);
exception.setErrorCode(WorkException.UNDEFINED);
}
}
|
public synchronized void | setState(int state)Sets the state of the work coordinator object
this.state = state;
|
public void | submitWork(int waitMode)Submits the work to the queue and generates a work accepted event.
this.waitMode = waitMode;
this.startTime = System.currentTimeMillis();
if (listener != null) {
listener.workAccepted(
new WorkEvent(this, WorkEvent.WORK_ACCEPTED, work, null));
}
if(workStats != null) {
workStats.submittedWorkCount++;
workStats.incrementWaitQueueLength();
}
queue.addWork( new OneWork(work, this));
|
public java.lang.String | toString()Returns the string representation of WorkCoordinator.
return id + ":" + work;
|
private void | unLock()Unlocks the thread.
try {
synchronized (lock) {
lock.notify();
}
} catch(Exception e) {
setException(e);
}
|
private void | workTimedOut()Times out the thread
setState(TIMEDOUT);
exception = new WorkRejectedException();
exception.setErrorCode(WorkException.START_TIMED_OUT);
if (listener != null) {
listener.workRejected(
new WorkEvent(this, WorkEvent.WORK_REJECTED, work, exception));
}
if(workStats != null) {
workStats.rejectedWorkCount++;
workStats.setActiveWorkCount(--workStats.currentActiveWorkCount);
}
|