Previous Page Next Page Start of Tutorial > Start of Trail > Start of Lesson Lesson Contents

Trail:
Servlets
Lesson:
The Life Cycle of a Servlet



Help on searching
is available.

Search technology
by Excite, Inc.

Handling Service Threads at Servlet Termination

All of a servlet's service methods should be complete when a servlet is removed. The server tries to ensure this by calling the destroy method only after all service requests have returned, or after a server-specific grace period, whichever comes first. If your servlet has operations that take a long time to run (that is, operations that may run longer than the server's grace period), the operations could still be running when destroy is called. You must make sure that any threads still handling client requests complete; the remainder of this section describes a technique for doing this.

If your servlet has potentially long-running service requests, use the techniques in this lesson to:

  • Keep track of how many threads are currently running the service method.
     
  • Provide a clean shutdown by having the destroy method notify long-running threads of the shutdown and wait for them to complete
     
  • Have the long-running methods poll periodically to check for shutdown and, if necessary, stop working, clean up and return.
     

Tracking Service Requests

To track service requests, include a field in your servlet class that counts the number of service methods that are running. The field should have access methods to increment, decrement, and return its value. For example:

public ShutdownExample extends HttpServlet {
    private int serviceCounter = 0;
    ...
    //Access methods for serviceCounter
    protected synchronized void enteringServiceMethod() {
	serviceCounter++;
    }
    protected synchronized void leavingServiceMethod() {
        serviceCounter--;
    }
    protected synchronized int numServices() {
	return serviceCounter;
    }
}

The service method should increment the service counter each time the method is entered and decrement the counter each time the method returns. This is one of the few times that your HttpServlet subclass should override the service method. The new method should call super.service to preserve all the original HttpServlet.service method's functionality.

    protected void service(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException
    {
	enteringServiceMethod();
	try {
            super.service(req, resp);
        } finally {
            leavingServiceMethod();
        }
    }

 

Providing a Clean Shutdown

To provide a clean shutdown, your destroy method should not destroy any shared resources until all the service requests have completed. One part of doing this is to check the service counter. Another part is to notify the long-running methods that it is time to shut down. For this, another field is required along with the usual access methods. For example:

public ShutdownExample extends HttpServlet {
    private boolean shuttingDown;
    ...
    //Access methods for shuttingDown
    protected setShuttingDown(boolean flag) {
	shuttingDown = flag;
    }
    protected boolean isShuttingDown() {
	return shuttingDown;
    }
}

An example of the destroy method using these fields to provide a clean shutdown is shown below:

    public void destroy() {

        /* Check to see whether there are still service methods running,
	 * and if there are, tell them to stop. */
	if (numServices() > 0) {
	    setShuttingDown(true);
        }

	/* Wait for the service methods to stop.  */
	while(numServices() > 0) {
            try {
                Thread.sleep(interval);
            } catch (InterruptedException e) {
            }
        }
    }

 

Creating Polite Long-running Methods

The final step in providing a clean shutdown is to make any long-running methods behave politely. Methods that might run for a long time should check the value of the field that notifies them of shut downs, and interrupt their work if neceesary. For example:
    public void doPost(...) {
        ...
	for(i = 0; ((i < lotsOfStuffToDo) && !isShuttingDown()); i++) {
	    try {
		partOfLongRunningOperation(i);
	    } catch (InterruptedException e) {
            }
        }
    }
 
Previous Page Next Page Start of Tutorial > Start of Trail > Start of Lesson Lesson Contents