Class TimerServiceImpl
- java.lang.Object
-
- org.jboss.as.ejb3.timerservice.TimerServiceImpl
-
- All Implemented Interfaces:
javax.ejb.TimerService,org.jboss.msc.Service,org.jboss.msc.service.Service<javax.ejb.TimerService>,org.jboss.msc.value.Value<javax.ejb.TimerService>
public class TimerServiceImpl extends Object implements javax.ejb.TimerService, org.jboss.msc.service.Service<javax.ejb.TimerService>
MK2 implementation of Enterprise Beans 3.1TimerService- Version:
- $Revision: $
- Author:
- Carlo de Wolf
-
-
Field Summary
Fields Modifier and Type Field Description static org.jboss.msc.service.ServiceNameSERVICE_NAME
-
Constructor Summary
Constructors Constructor Description TimerServiceImpl(Map<Method,List<AutoTimer>> autoTimers, org.jboss.msc.service.ServiceName serviceName, TimerServiceRegistry registry)Creates aTimerServiceImpl
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidactivate()protected voidcancelTimeout(TimerImpl timer)Cancels any scheduledFuturecorresponding to the passedtimervoidcancelTimer(TimerImpl timer)javax.ejb.TimercreateCalendarTimer(javax.ejb.ScheduleExpression schedule)javax.ejb.TimercreateCalendarTimer(javax.ejb.ScheduleExpression schedule, javax.ejb.TimerConfig timerConfig)javax.ejb.TimercreateIntervalTimer(long initialDuration, long intervalDuration, javax.ejb.TimerConfig timerConfig)javax.ejb.TimercreateIntervalTimer(Date initialExpiration, long intervalDuration, javax.ejb.TimerConfig timerConfig)javax.ejb.TimercreateSingleActionTimer(long duration, javax.ejb.TimerConfig timerConfig)javax.ejb.TimercreateSingleActionTimer(Date expiration, javax.ejb.TimerConfig timerConfig)javax.ejb.TimercreateTimer(long initialDuration, long intervalDuration, Serializable info)javax.ejb.TimercreateTimer(long duration, Serializable info)javax.ejb.TimercreateTimer(Date initialExpiration, long intervalDuration, Serializable info)javax.ejb.TimercreateTimer(Date expiration, Serializable info)voiddeactivate()voidexpireTimer(TimerImpl timer)Collection<javax.ejb.Timer>getAllTimers()org.jboss.msc.value.InjectedValue<EJBComponent>getEjbComponentInjectedValue()org.jboss.msc.value.InjectedValue<ExecutorService>getExecutorServiceInjectedValue()TimedObjectInvokergetInvoker()Returns theTimedObjectInvokerto which this timer service belongsSerializablegetPersistedTimerInfo(TimerImpl timer)Retrieves the timer info from the timer database.TimerServiceResourcegetResource()org.jboss.msc.service.ServiceNamegetServiceName()org.jboss.msc.value.InjectedValue<TimedObjectInvoker>getTimedObjectInvoker()TimerImplgetTimer(String timerId)TimerImplgetTimer(String timerId, String timedObjectId)Returns the timer corresponding to the passed timer id and timed object id.org.jboss.msc.value.InjectedValue<Timer>getTimerInjectedValue()org.jboss.msc.value.InjectedValue<TimerPersistence>getTimerPersistence()Collection<javax.ejb.Timer>getTimers()protected javax.transaction.TransactiongetTransaction()javax.ejb.TimerServicegetValue()protected booleanisLifecycleCallbackInvocation()Returns true if theCurrentInvocationContextrepresents a lifecycle callback invocation.booleanisScheduled(String tid)booleanisStarted()TimerImplloadAutoTimer(javax.ejb.ScheduleExpression schedule, javax.ejb.TimerConfig timerConfig, Method timeoutMethod)voidpersistTimer(TimerImpl timer, boolean newTimer)Persists the passedtimer.voidrestoreTimers(List<AutoTimer> newAutoTimers)Restores persisted timers, corresponding to this timerservice, which are eligible for any new timeouts.static voidsafeClose(AutoCloseable resource)Safely closes some resource without throwing an exception.protected voidscheduleTimeout(TimerImpl timer, boolean newTimer)Creates and schedules aTimerTaskfor the next timeout of the passedtimerbooleanshouldRun(TimerImpl timer)Check if a persistent timer is already executed from a different instance or should be executed.voidstart(org.jboss.msc.service.StartContext context)protected voidstartTimer(TimerImpl timer)Registers a timer with a transaction (if any in progress) and then moves the timer to an active state, so that it becomes eligible for timeoutsvoidstop(org.jboss.msc.service.StopContext context)voidsuspendTimers()Suspends any currently scheduled tasks forTimers
-
-
-
Constructor Detail
-
TimerServiceImpl
public TimerServiceImpl(Map<Method,List<AutoTimer>> autoTimers, org.jboss.msc.service.ServiceName serviceName, TimerServiceRegistry registry)
Creates aTimerServiceImpl- Parameters:
autoTimers- The auto timers associated with this timer serviceserviceName- The service name of this timer serviceregistry- TheTimerServiceRegistrywhich has the knowledge of other timer services belonging to the EJB module to which this timer service belongs.
-
-
Method Detail
-
start
public void start(org.jboss.msc.service.StartContext context) throws org.jboss.msc.service.StartException- Specified by:
startin interfaceorg.jboss.msc.Service- Specified by:
startin interfaceorg.jboss.msc.service.Service<javax.ejb.TimerService>- Throws:
org.jboss.msc.service.StartException
-
stop
public void stop(org.jboss.msc.service.StopContext context)
- Specified by:
stopin interfaceorg.jboss.msc.Service- Specified by:
stopin interfaceorg.jboss.msc.service.Service<javax.ejb.TimerService>
-
activate
public void activate()
-
deactivate
public void deactivate()
-
getValue
public javax.ejb.TimerService getValue() throws IllegalStateException, IllegalArgumentException- Specified by:
getValuein interfaceorg.jboss.msc.value.Value<javax.ejb.TimerService>- Throws:
IllegalStateExceptionIllegalArgumentException
-
createCalendarTimer
public javax.ejb.Timer createCalendarTimer(javax.ejb.ScheduleExpression schedule) throws IllegalArgumentException, IllegalStateException, javax.ejb.EJBException- Specified by:
createCalendarTimerin interfacejavax.ejb.TimerService- Throws:
IllegalArgumentExceptionIllegalStateExceptionjavax.ejb.EJBException
-
createCalendarTimer
public javax.ejb.Timer createCalendarTimer(javax.ejb.ScheduleExpression schedule, javax.ejb.TimerConfig timerConfig) throws IllegalArgumentException, IllegalStateException, javax.ejb.EJBException- Specified by:
createCalendarTimerin interfacejavax.ejb.TimerService- Throws:
IllegalArgumentExceptionIllegalStateExceptionjavax.ejb.EJBException
-
createIntervalTimer
public javax.ejb.Timer createIntervalTimer(Date initialExpiration, long intervalDuration, javax.ejb.TimerConfig timerConfig) throws IllegalArgumentException, IllegalStateException, javax.ejb.EJBException
- Specified by:
createIntervalTimerin interfacejavax.ejb.TimerService- Throws:
IllegalArgumentExceptionIllegalStateExceptionjavax.ejb.EJBException
-
createIntervalTimer
public javax.ejb.Timer createIntervalTimer(long initialDuration, long intervalDuration, javax.ejb.TimerConfig timerConfig) throws IllegalArgumentException, IllegalStateException, javax.ejb.EJBException- Specified by:
createIntervalTimerin interfacejavax.ejb.TimerService- Throws:
IllegalArgumentExceptionIllegalStateExceptionjavax.ejb.EJBException
-
createSingleActionTimer
public javax.ejb.Timer createSingleActionTimer(Date expiration, javax.ejb.TimerConfig timerConfig) throws IllegalArgumentException, IllegalStateException, javax.ejb.EJBException
- Specified by:
createSingleActionTimerin interfacejavax.ejb.TimerService- Throws:
IllegalArgumentExceptionIllegalStateExceptionjavax.ejb.EJBException
-
createSingleActionTimer
public javax.ejb.Timer createSingleActionTimer(long duration, javax.ejb.TimerConfig timerConfig) throws IllegalArgumentException, IllegalStateException, javax.ejb.EJBException- Specified by:
createSingleActionTimerin interfacejavax.ejb.TimerService- Throws:
IllegalArgumentExceptionIllegalStateExceptionjavax.ejb.EJBException
-
createTimer
public javax.ejb.Timer createTimer(long duration, Serializable info) throws IllegalArgumentException, IllegalStateException, javax.ejb.EJBException- Specified by:
createTimerin interfacejavax.ejb.TimerService- Throws:
IllegalArgumentExceptionIllegalStateExceptionjavax.ejb.EJBException
-
createTimer
public javax.ejb.Timer createTimer(Date expiration, Serializable info) throws IllegalArgumentException, IllegalStateException, javax.ejb.EJBException
- Specified by:
createTimerin interfacejavax.ejb.TimerService- Throws:
IllegalArgumentExceptionIllegalStateExceptionjavax.ejb.EJBException
-
createTimer
public javax.ejb.Timer createTimer(long initialDuration, long intervalDuration, Serializable info) throws IllegalArgumentException, IllegalStateException, javax.ejb.EJBException- Specified by:
createTimerin interfacejavax.ejb.TimerService- Throws:
IllegalArgumentExceptionIllegalStateExceptionjavax.ejb.EJBException
-
createTimer
public javax.ejb.Timer createTimer(Date initialExpiration, long intervalDuration, Serializable info) throws IllegalArgumentException, IllegalStateException, javax.ejb.EJBException
- Specified by:
createTimerin interfacejavax.ejb.TimerService- Throws:
IllegalArgumentExceptionIllegalStateExceptionjavax.ejb.EJBException
-
loadAutoTimer
public TimerImpl loadAutoTimer(javax.ejb.ScheduleExpression schedule, javax.ejb.TimerConfig timerConfig, Method timeoutMethod)
-
getTimers
public Collection<javax.ejb.Timer> getTimers() throws IllegalStateException, javax.ejb.EJBException
- Specified by:
getTimersin interfacejavax.ejb.TimerService- Throws:
IllegalStateExceptionjavax.ejb.EJBException
-
getAllTimers
public Collection<javax.ejb.Timer> getAllTimers() throws IllegalStateException, javax.ejb.EJBException
When
PROGRAMMATIC_TIMER_REFRESH_ENABLEDis set to true, this method programmatically refreshes from the database timer persistence before returning all timers.- Specified by:
getAllTimersin interfacejavax.ejb.TimerService- Throws:
IllegalStateExceptionjavax.ejb.EJBException
-
getPersistedTimerInfo
public Serializable getPersistedTimerInfo(TimerImpl timer)
Retrieves the timer info from the timer database.- Parameters:
timer- the timer whose info to be retrieved- Returns:
- the timer info from database; cached timer info if the timer persistence store is not database
-
getInvoker
public TimedObjectInvoker getInvoker()
Returns theTimedObjectInvokerto which this timer service belongs- Returns:
-
getTimer
public TimerImpl getTimer(String timerId, String timedObjectId)
Returns the timer corresponding to the passed timer id and timed object id.- Parameters:
timerId- timer idtimedObjectId- timed object id- Returns:
- the
TimerImplcorresponding to the passed timer id and timed object id
-
getTransaction
protected javax.transaction.Transaction getTransaction()
- Returns:
- Returns the current transaction, if any. Else returns null.
- Throws:
javax.ejb.EJBException- If there is any system level exception
-
persistTimer
public void persistTimer(TimerImpl timer, boolean newTimer)
Persists the passedtimer.If the passed timer is null or is non-persistent (i.e.
Timer.isPersistent()returns false), then this method acts as a no-op- Parameters:
timer-
-
cancelTimer
public void cancelTimer(TimerImpl timer) throws InterruptedException
- Throws:
InterruptedException
-
expireTimer
public void expireTimer(TimerImpl timer)
-
suspendTimers
public void suspendTimers()
Suspends any currently scheduled tasks forTimersNote 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.
-
restoreTimers
public void restoreTimers(List<AutoTimer> newAutoTimers)
Restores persisted timers, corresponding to this timerservice, which are eligible for any new timeouts.This includes timers whose
TimerStateis neither of the following:All such restored timers will be schedule for their next timeouts.
- Parameters:
newAutoTimers-
-
startTimer
protected void startTimer(TimerImpl timer)
Registers a timer with a transaction (if any in progress) and then moves the timer to an active state, so that it becomes eligible for timeouts
-
isLifecycleCallbackInvocation
protected boolean isLifecycleCallbackInvocation()
Returns true if theCurrentInvocationContextrepresents a lifecycle callback invocation. Else returns false.This method internally relies on
CurrentInvocationContext.get()to obtain the current invocation context.- If the context is available then it looks for the method that was invoked. The absence of a method indicates a lifecycle callback.
- If the context is not available, then this method returns false (i.e.
it doesn't consider the current invocation as a lifecycle callback). This is
for convenience, to allow the invocation of
TimerServicemethods in the absence ofCurrentInvocationContext
- Returns:
-
scheduleTimeout
protected void scheduleTimeout(TimerImpl timer, boolean newTimer)
Creates and schedules aTimerTaskfor the next timeout of the passedtimer
-
cancelTimeout
protected void cancelTimeout(TimerImpl timer)
Cancels any scheduledFuturecorresponding to the passedtimer- Parameters:
timer- the timer to cancel
-
isScheduled
public boolean isScheduled(String tid)
-
getEjbComponentInjectedValue
public org.jboss.msc.value.InjectedValue<EJBComponent> getEjbComponentInjectedValue()
-
getExecutorServiceInjectedValue
public org.jboss.msc.value.InjectedValue<ExecutorService> getExecutorServiceInjectedValue()
-
getTimerInjectedValue
public org.jboss.msc.value.InjectedValue<Timer> getTimerInjectedValue()
-
getTimerPersistence
public org.jboss.msc.value.InjectedValue<TimerPersistence> getTimerPersistence()
-
getServiceName
public org.jboss.msc.service.ServiceName getServiceName()
-
isStarted
public boolean isStarted()
-
getTimedObjectInvoker
public org.jboss.msc.value.InjectedValue<TimedObjectInvoker> getTimedObjectInvoker()
-
getResource
public TimerServiceResource getResource()
-
shouldRun
public boolean shouldRun(TimerImpl timer)
Check if a persistent timer is already executed from a different instance or should be executed. For non-persistent timer it always returntrue.- Parameters:
timer- the timer which should be checked- Returns:
trueif the timer is not persistent or the persistent timer should start
-
safeClose
public static void safeClose(AutoCloseable resource)
Safely closes some resource without throwing an exception. Any exception will be logged at TRACE level.- Parameters:
resource- the resource to close
-
-