An implementation of an EJB Timer.
Internally it uses a java.util.Timer and maintains its state in
a Tx manner.
| Method from org.jboss.ejb.txtimer.TimerImpl Detail: |
public void afterCompletion(int status) {
if (status == Status.STATUS_COMMITTED)
{
log.debug("commit: " + this);
switch (timerState)
{
case STARTED_IN_TX:
scheduleTimeout();
setTimerState(ACTIVE);
break;
case CANCELED_IN_TX:
cancelTimer();
break;
case IN_TIMEOUT:
case RETRY_TIMEOUT:
if(periode == 0)
{
setTimerState(EXPIRED);
cancelTimer();
}
else
{
setTimerState(ACTIVE);
}
break;
}
}
else if (status == Status.STATUS_ROLLEDBACK)
{
log.debug("rollback: " + this);
switch (timerState)
{
case STARTED_IN_TX:
cancelTimer();
break;
case CANCELED_IN_TX:
setTimerState(ACTIVE);
break;
case IN_TIMEOUT:
setTimerState(RETRY_TIMEOUT);
log.debug("retry: " + this);
timerService.retryTimeout(this);
break;
case RETRY_TIMEOUT:
if (periode == 0)
{
setTimerState(EXPIRED);
cancelTimer();
}
else
{
setTimerState(ACTIVE);
}
break;
}
}
}
This method is invoked after the transaction has committed or
rolled back. |
public void beforeCompletion() {
switch(timerState)
{
case CANCELED_IN_TX:
timerService.removeTimer(this);
break;
case IN_TIMEOUT:
case RETRY_TIMEOUT:
if(periode == 0)
{
timerService.removeTimer(this);
}
break;
}
}
This method is invoked before the start of the commit or rollback
process. The method invocation is done in the context of the
transaction that is about to be committed or rolled back. |
public void cancel() throws NoSuchObjectLocalException, EJBException, IllegalStateException {
assertTimedOut();
assertAllowedOperation("Timer.cancel");
registerTimerWithTx();
cancelInTx();
}
Cause the txtimer and all its associated expiration notifications to be cancelled. |
public boolean equals(Object obj) {
if (obj == this) return true;
if (obj instanceof TimerImpl)
{
TimerImpl other = (TimerImpl)obj;
return hashCode() == other.hashCode();
}
return false;
}
Return true if objectId, createDate, periode are equal |
public Date getFirstTime() {
return firstTime;
}
|
public TimerHandle getHandle() throws NoSuchObjectLocalException, EJBException, IllegalStateException {
assertTimedOut();
assertAllowedOperation("Timer.getHandle");
return new TimerHandleImpl(this);
}
Get a serializable handle to the txtimer. This handle can be used at a later time to
re-obtain the txtimer reference. |
public Serializable getInfo() throws NoSuchObjectLocalException, EJBException, IllegalStateException {
assertTimedOut();
assertAllowedOperation("Timer.getInfo");
return info;
}
Get the information associated with the txtimer at the time of creation. |
public Serializable getInfoInternal() {
return info;
}
|
public long getNextExpire() {
return nextExpire;
}
|
public Date getNextTimeout() throws NoSuchObjectLocalException, EJBException, IllegalStateException {
assertTimedOut();
assertAllowedOperation("Timer.getNextTimeout");
return new Date(nextExpire);
}
Get the point in time at which the next txtimer expiration is scheduled to occur. |
public long getPeriode() {
return periode;
}
|
public long getTimeRemaining() throws NoSuchObjectLocalException, EJBException, IllegalStateException {
assertTimedOut();
assertAllowedOperation("Timer.getTimeRemaining");
return nextExpire - System.currentTimeMillis();
}
Get the number of milliseconds that will elapse before the next scheduled txtimer expiration. |
public TimedObjectId getTimedObjectId() {
return timedObjectId;
}
|
public String getTimerId() {
return timerId;
}
|
public int hashCode() {
if (hashCode == 0)
{
String hash = "[" + timerId + "," + timedObjectId + "," + firstTime + "," + periode + "]";
hashCode = hash.hashCode();
}
return hashCode;
}
Hash code based on the Timers invariant properties |
public boolean isActive() {
return !isCanceled() && !isExpired();
}
|
public boolean isCanceled() {
return timerState == CANCELED_IN_TX || timerState == CANCELED;
}
|
public boolean isExpired() {
return timerState == EXPIRED;
}
|
public boolean isInRetry() {
return timerState == RETRY_TIMEOUT;
}
|
public void killTimer() {
log.debug("killTimer: " + this);
if (timerState != EXPIRED)
setTimerState(CANCELED);
timerService.removeTimer(this);
utilTimer.cancel();
}
Kill the timer, and remove it from the timer service |
void startTimer(Date firstTime,
long periode) {
this.firstTime = firstTime;
this.nextExpire = firstTime.getTime();
this.periode = periode;
timerService.addTimer(this);
registerTimerWithTx();
// the timer will actually go ACTIVE on tx commit
startInTx();
}
|
public void stopTimer() {
log.debug("stopTimer: " + this);
if (timerState != EXPIRED)
setTimerState(CANCELED);
utilTimer.cancel();
}
Kill the timer, do not remove from timer service |
public String toString() {
long remaining = nextExpire - System.currentTimeMillis();
String retStr = "[id=" + timerId + ",target=" + timedObjectId + ",remaining=" + remaining + ",periode=" + periode +
"," + TIMER_STATES[timerState] + "]";
return retStr;
}
Returns a string representation of the object. |