I have implemented a SIP Servlet, where I receive two types of messages from the clients. I can receive either a high priority message and low priority message, which I separate when I'm reading the URIs of the messages as shown in the code below. I have to implement a basic stopwatch, which increments the "count" integer declared in code below. How do I make such a stopwatch and resetting it ?.
protected void doRequest(SipServletRequest reqfromclient) throws javax.servlet.ServletException, java.io.IOException {
if( reqfromclient.getMethod().equals("MESSAGE") ) {
String MESSAGE = reqfromclient.getContent().toString();
System.out.println("The arrived message is " + MESSAGE);
// Assign the callee URI
String URICallee = reqfromclient.getTo().getURI().toString();
//Assign the caller URI
String URICaller = reqfromclient.getFrom().getURI().toString();
//DECLARE STOPWATCH
int count = 0;
// Now the Highprio and Lowprio alerts have to be separated
if(URICallee.endsWith("policeHigh.com")) {
// RESET STOPWATCH
//START THE STOPWATCH. INCREMENT COUNT EVERY SECOND
}
else if(URICallee.endsWith("policeLow.com")) {
if(count == 21) {
//something
}
}
}
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:
TimerService
.TimerService
to create a ServletTimer
with the required timeout period.ServletTimer
somewhere (as an attribute in the SipSession
or SipApplicationSession
).sipApplicationSession.getTimer(id)
and call cancel()
on it.TimerListener
interface. It can be your servlet class if you wish. Implement the timeout
method with the logic that your application needs to perform when the timeout expires. As discussed in this link, declare the class to be a listener
in the SIP deployment descriptor.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.