Package org.jboss.as.ejb3.timerservice
Class TimerImpl
- java.lang.Object
-
- org.jboss.as.ejb3.timerservice.TimerImpl
-
- All Implemented Interfaces:
javax.ejb.Timer
- Direct Known Subclasses:
CalendarTimer
public class TimerImpl extends Object implements javax.ejb.Timer
Implementation of Enterprise Beans 3.1Timer- Version:
- $Revision: $
- Author:
- Carlo de Wolf
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classTimerImpl.Builder
-
Field Summary
Fields Modifier and Type Field Description protected StringidUnique id for this timer instanceprotected SerializableinfoThe info which was passed while creating the timer.protected DateinitialExpirationThe initial (first) expiry date of this timerprotected longintervalDurationThe duration in milli sec.protected DatenextExpirationNext expiry date of this timerprotected booleanpersistentIndicates whether the timer is persistentprotected DatepreviousRunThe date of the previous run of this timerprotected TimedObjectInvokertimedObjectInvokerTheTimedObjectInvokerto which this timer correspondsprotected TimerServiceImpltimerServiceTheTimerServicethrough which this timer was createdprotected TimerStatetimerStateThe timer state
-
Constructor Summary
Constructors Modifier Constructor Description protectedTimerImpl(TimerImpl.Builder builder, TimerServiceImpl service)Creates aTimerImpl
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected voidassertTimerState()Asserts that the timer is not in any of the following states:TimerState.CANCELEDTimerState.EXPIREDstatic TimerImpl.Builderbuilder()voidcancel()booleanequals(Object o)SerializablegetCachedTimerInfo()Obtains the timer info cached in memory, without checking the persistent store.protected ThreadgetExecutingThread()Returns the executing thread which is processing the timeout taskjavax.ejb.TimerHandlegetHandle()StringgetId()Returns the id of this timerSerializablegetInfo()DategetInitialExpiration()Returns the initial (first) timeout date of this timerlonggetInterval()Returns the interval (in milliseconds), between timeouts, of this timer.DategetNextExpiration()This method is similar togetNextTimeout(), except that this method does not check the timer state and hence does not throw eitherIllegalStateExceptionorNoSuchObjectLocalExceptionorEJBException.DategetNextTimeout()DategetPreviousRun()Returns theDateof the previous timeout of this timerjavax.ejb.ScheduleExpressiongetSchedule()TimerStategetState()Returns the current state of this timerStringgetTimedObjectId()Returns the timed object id to which this timer belongslonggetTimeRemaining()SerializablegetTimerInfo()This method is similar togetInfo(), except that this method does not check the timer state and hence does not throw eitherIllegalStateExceptionorNoSuchObjectLocalExceptionorEJBException.TimerServiceImplgetTimerService()Returns the timer service through which this timer was createdprotected TimerTaskgetTimerTask()Returns the task which handles the timeouts of thisTimerImplinthashCode()voidinvokeOneOff()Triggers timer, outside of normal expiration.booleanisActive()Returns true if this timer is active.booleanisAutoTimer()booleanisCalendarTimer()booleanisCanceled()Returns true if this timer is inTimerState.CANCELEDstate.booleanisExpired()Returns true if this timer is inTimerState.EXPIREDstate.booleanisInRetry()Returns true if this timer is inTimerState.RETRY_TIMEOUT.booleanisPersistent()voidlock()voidscheduleTimeout(boolean newTimer)Creates and schedules aTimerTaskfor the next timeout of this timervoidsetCachedTimerInfo(Serializable newInfo)Sets the timer info to a new value.voidsetNextTimeout(Date next)Sets the next timeout of this timervoidsetPreviousRun(Date previousRun)Sets theDateof the previous timeout of this timervoidsetTimerState(TimerState state, Thread thread)Sets the state and timer task executing thread of this timervoidsuspend()Suspends any currently scheduled task for this timerStringtoString()A nice formatted string output for this timervoidunlock()
-
-
-
Field Detail
-
id
protected final String id
Unique id for this timer instance
-
timerState
protected volatile TimerState timerState
The timer state
-
timerService
protected final TimerServiceImpl timerService
TheTimerServicethrough which this timer was created
-
timedObjectInvoker
protected final TimedObjectInvoker timedObjectInvoker
TheTimedObjectInvokerto which this timer corresponds
-
info
protected Serializable info
The info which was passed while creating the timer.
-
persistent
protected final boolean persistent
Indicates whether the timer is persistent
-
initialExpiration
protected final Date initialExpiration
The initial (first) expiry date of this timer
-
intervalDuration
protected final long intervalDuration
The duration in milli sec. between timeouts
-
nextExpiration
protected volatile Date nextExpiration
Next expiry date of this timer
-
previousRun
protected volatile Date previousRun
The date of the previous run of this timer
-
-
Constructor Detail
-
TimerImpl
protected TimerImpl(TimerImpl.Builder builder, TimerServiceImpl service)
Creates aTimerImpl- Parameters:
builder- The builder with the timer informationservice- The timer service through which this timer was created
-
-
Method Detail
-
cancel
public void cancel() throws IllegalStateException, javax.ejb.EJBException- Specified by:
cancelin interfacejavax.ejb.Timer- Throws:
IllegalStateExceptionjavax.ejb.EJBException
-
getId
public String getId()
Returns the id of this timer- Returns:
-
isCalendarTimer
public boolean isCalendarTimer() throws IllegalStateException, javax.ejb.EJBException- Specified by:
isCalendarTimerin interfacejavax.ejb.Timer- Throws:
IllegalStateExceptionjavax.ejb.EJBException
-
getHandle
public javax.ejb.TimerHandle getHandle() throws IllegalStateException, javax.ejb.EJBException- Specified by:
getHandlein interfacejavax.ejb.Timer- Throws:
IllegalStateExceptionjavax.ejb.EJBException
-
isPersistent
public boolean isPersistent() throws IllegalStateException, javax.ejb.EJBException- Specified by:
isPersistentin interfacejavax.ejb.Timer- Throws:
IllegalStateExceptionjavax.ejb.EJBException
-
getInfo
public Serializable getInfo() throws IllegalStateException, javax.ejb.EJBException
- Specified by:
getInfoin interfacejavax.ejb.Timer- Throws:
IllegalStateExceptionjavax.ejb.EJBException- See Also:
getTimerInfo()
-
getTimerInfo
public Serializable getTimerInfo()
This method is similar togetInfo(), except that this method does not check the timer state and hence does not throw eitherIllegalStateExceptionorNoSuchObjectLocalExceptionorEJBException.- Returns:
- the timer info; if not available in-memory, retrieve it from persistence
-
getCachedTimerInfo
public Serializable getCachedTimerInfo()
Obtains the timer info cached in memory, without checking the persistent store.- Returns:
- the cached timer info
-
setCachedTimerInfo
public void setCachedTimerInfo(Serializable newInfo)
Sets the timer info to a new value. The purpose of this method is forDatabaseTimerPersistenceto reset the cached timer info. It should not be used for other purposes.- Parameters:
newInfo- the new timer info, typically null or an empty holder value
-
getNextTimeout
public Date getNextTimeout() throws IllegalStateException, javax.ejb.EJBException
- Specified by:
getNextTimeoutin interfacejavax.ejb.Timer- Throws:
IllegalStateExceptionjavax.ejb.EJBException- See Also:
getNextExpiration()
-
getNextExpiration
public Date getNextExpiration()
This method is similar togetNextTimeout(), except that this method does not check the timer state and hence does not throw eitherIllegalStateExceptionorNoSuchObjectLocalExceptionorEJBException.- Returns:
-
setNextTimeout
public void setNextTimeout(Date next)
Sets the next timeout of this timer- Parameters:
next- The next scheduled timeout of this timer
-
getSchedule
public javax.ejb.ScheduleExpression getSchedule() throws IllegalStateException, javax.ejb.EJBException- Specified by:
getSchedulein interfacejavax.ejb.Timer- Throws:
IllegalStateExceptionjavax.ejb.EJBException
-
getTimeRemaining
public long getTimeRemaining() throws IllegalStateException, javax.ejb.EJBException- Specified by:
getTimeRemainingin interfacejavax.ejb.Timer- Throws:
IllegalStateExceptionjavax.ejb.EJBException
-
isAutoTimer
public boolean isAutoTimer()
-
getInitialExpiration
public Date getInitialExpiration()
Returns the initial (first) timeout date of this timer- Returns:
-
getInterval
public long getInterval()
Returns the interval (in milliseconds), between timeouts, of this timer.- Returns:
-
getTimedObjectId
public String getTimedObjectId()
Returns the timed object id to which this timer belongs- Returns:
-
getTimerService
public TimerServiceImpl getTimerService()
Returns the timer service through which this timer was created- Returns:
-
isActive
public boolean isActive()
Returns true if this timer is active. Else returns false.A timer is considered to be "active", if its
TimerStateis neither of the following:TimerState.CANCELEDTimerState.EXPIRED- has not been suspended
- Returns:
-
isCanceled
public boolean isCanceled()
Returns true if this timer is inTimerState.CANCELEDstate. Else returns false.- Returns:
-
isExpired
public boolean isExpired()
Returns true if this timer is inTimerState.EXPIREDstate. Else returns false- Returns:
-
isInRetry
public boolean isInRetry()
Returns true if this timer is inTimerState.RETRY_TIMEOUT. Else returns false.- Returns:
-
getPreviousRun
public Date getPreviousRun()
Returns theDateof the previous timeout of this timer- Returns:
-
setPreviousRun
public void setPreviousRun(Date previousRun)
Sets theDateof the previous timeout of this timer- Parameters:
previousRun-
-
getState
public TimerState getState()
Returns the current state of this timer- Returns:
-
getExecutingThread
protected Thread getExecutingThread()
Returns the executing thread which is processing the timeout task- Returns:
- the executingThread
-
assertTimerState
protected void assertTimerState()
Asserts that the timer is not in any of the following states:- Throws:
javax.ejb.NoSuchObjectLocalException- if the txtimer was canceled or has expired
-
setTimerState
public void setTimerState(TimerState state, Thread thread)
Sets the state and timer task executing thread of this timer- Parameters:
state- The state of this timerthread- The executing thread which is processing the timeout task
-
suspend
public void suspend()
Suspends any currently scheduled task for this timerNote that, suspend does not cancel the
Timer. Instead, it just cancels the next scheduled timeout. So once theTimeris restored (whenever that happens), theTimerwill continue to timeout at appropriate times.
-
invokeOneOff
public void invokeOneOff() throws ExceptionTriggers timer, outside of normal expiration. Only used when running an explicit management trigger operation. The tigger operation simply runs the callback, it does not modify the timer state in any way, and there is no protection against overlapping events when running it. This is the expected behaviour, as otherwise the semantics of dealing with concurrent execution is complex and kinda weird.- Throws:
Exception
-
scheduleTimeout
public void scheduleTimeout(boolean newTimer)
Creates and schedules aTimerTaskfor the next timeout of this timer- Parameters:
newTimer-trueif this is a new timer being scheduled, and not a re-schedule due to a timeout
-
getTimerTask
protected TimerTask getTimerTask()
Returns the task which handles the timeouts of thisTimerImpl- Returns:
- See Also:
TimerTask
-
lock
public void lock() throws InterruptedException- Throws:
InterruptedException
-
unlock
public void unlock()
-
toString
public String toString()
A nice formatted string output for this timer
-
builder
public static TimerImpl.Builder builder()
-
-