Timers

From Wesip

From Wesip

Timers are used by an application tu schedule a notification at a certain moment or with a certain frequency. Applications retrieve an implementation of the TimerService interface as a ServletContext attribute stored with under the key "javax.servlet.sip.TimerService".

The TimerService instance creates a ServletTimer via one of the two methods

ServletTimer createTimer(SipApplicationSession appSession, 
long delay, 
boolean isPersistent, 
java.io.Serializable info); 

to schedule a one shot Timer or

ServletTimer createTimer(SipApplicationSession appSession, 
                                  long delay, 
                                  long period, 
                                  boolean fixedDelay, 
                                  boolean isPersistent, 
                                  java.io.Serializable info); 

to schedule a recurrent Timer.

  • - Timing is configured with the delay, period and fixedDelay parameters.
  • - A SipApplicationSession is required for instantiation. The ServletTimer is bound to that application session so when the timer fires it can be used to retrieve attributes previously stored.
  • - If the isPersistent parameter is set to true the timer is saved along with the application session so it can survive across reboots.
  • - Another parameter, info, is used to describe the nature of the timer. Any object will be valid provided that it can be serialized. The information is retrieved on expiration.

Every time a timer fires the TimerListener of the application is notified. Applications declare a class implementing the TimerListener interface in the deployment descriptor.

<listener> 
  <listener-class>MyTimerListener</listener-class> 
</listener> 

The callback method invoked is void timeout(ServletTimer timer) which receives the expired ServletTimer. The ServletTimer interface provides several methods useful for processing

  • - getApplicationSession(): Returns the bound SipApplicationSession
  • - getInfo(): Returns the information set on creation.
  • - scheduledExecutionTime(): Returns the scheduled expiration time of the most recent actual expiration of this timer.
  • - cancel(): To prevent the timer from expiring again.

<< Listeners