Class 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.1 Timer
    Version:
    $Revision: $
    Author:
    Carlo de Wolf
    • 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
        The TimerService through which this timer was created
      • 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 a TimerImpl
        Parameters:
        builder - The builder with the timer information
        service - The timer service through which this timer was created
    • Method Detail

      • getId

        public String getId()
        Returns the id of this timer
        Returns:
      • isCalendarTimer

        public boolean isCalendarTimer()
                                throws IllegalStateException,
                                       javax.ejb.EJBException
        Specified by:
        isCalendarTimer in interface javax.ejb.Timer
        Throws:
        IllegalStateException
        javax.ejb.EJBException
      • getHandle

        public javax.ejb.TimerHandle getHandle()
                                        throws IllegalStateException,
                                               javax.ejb.EJBException
        Specified by:
        getHandle in interface javax.ejb.Timer
        Throws:
        IllegalStateException
        javax.ejb.EJBException
      • isPersistent

        public boolean isPersistent()
                             throws IllegalStateException,
                                    javax.ejb.EJBException
        Specified by:
        isPersistent in interface javax.ejb.Timer
        Throws:
        IllegalStateException
        javax.ejb.EJBException
      • getTimerInfo

        public Serializable getTimerInfo()
        This method is similar to getInfo(), except that this method does not check the timer state and hence does not throw either IllegalStateException or NoSuchObjectLocalException or EJBException.
        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 for DatabaseTimerPersistence to 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
      • getNextExpiration

        public Date getNextExpiration()
        This method is similar to getNextTimeout(), except that this method does not check the timer state and hence does not throw either IllegalStateException or NoSuchObjectLocalException or EJBException.
        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:
        getSchedule in interface javax.ejb.Timer
        Throws:
        IllegalStateException
        javax.ejb.EJBException
      • getTimeRemaining

        public long getTimeRemaining()
                              throws IllegalStateException,
                                     javax.ejb.EJBException
        Specified by:
        getTimeRemaining in interface javax.ejb.Timer
        Throws:
        IllegalStateException
        javax.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 TimerState is neither of the following:

        And if the corresponding timer service is still up

        Returns:
      • isCanceled

        public boolean isCanceled()
        Returns true if this timer is in TimerState.CANCELED state. Else returns false.
        Returns:
      • isExpired

        public boolean isExpired()
        Returns true if this timer is in TimerState.EXPIRED state. Else returns false
        Returns:
      • isInRetry

        public boolean isInRetry()
        Returns true if this timer is in TimerState.RETRY_TIMEOUT. Else returns false.
        Returns:
      • getPreviousRun

        public Date getPreviousRun()
        Returns the Date of the previous timeout of this timer
        Returns:
      • setPreviousRun

        public void setPreviousRun​(Date previousRun)
        Sets the Date of 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 timer
        thread - The executing thread which is processing the timeout task
      • suspend

        public void suspend()
        Suspends any currently scheduled task for this timer

        Note that, suspend does not cancel the Timer. Instead, it just cancels the next scheduled timeout. So once the Timer is restored (whenever that happens), the Timer will continue to timeout at appropriate times.

      • invokeOneOff

        public void invokeOneOff()
                          throws Exception
        Triggers 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 a TimerTask for the next timeout of this timer
        Parameters:
        newTimer - true if 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 this TimerImpl
        Returns:
        See Also:
        TimerTask
      • unlock

        public void unlock()
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class Object
      • toString

        public String toString()
        A nice formatted string output for this timer
        Overrides:
        toString in class Object