javasipsip-servlet

Stopwatch in Java with a resetting function


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
            }
        }
    }

Solution

  • 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:

    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.