To execute some arbitrary code based on a timer, use the ServletTimer
class which can be created from the TimerService
. There are several parts to this:
- When you want to set a timer, obtain a reference to the
TimerService
. - Use the
TimerService
to create aServletTimer
with the required timeout period. - Store the ID of the
ServletTimer
somewhere (as an attribute in theSipSession
orSipApplicationSession
). - If the timer needs to be cancelled, retrieve the timer ID from the session, retrieve the timer using
sipApplicationSession.getTimer(id)
and callcancel()
on it. - You will need some class that implements the
TimerListener
interface. It can be your servlet class if you wish. Implement thetimeout
method with the logic that your application needs to perform when the timeout expires. As discussed in this link, declare the class to be alistener
in the SIP deployment descriptor. - Optional: when your call is over, call
sipApplicationSession.invalidate()
, which will cancel any outstanding timers.
A simple example is shown here. The example is flawed in that it stores the ServletTimer
as a field of the servlet class, so it will get overwritten as subsequent calls come in. Storing the ID as an attribute of the SipApplicationSession
will keep it from getting overwritten.