Java, Programming

Tracking Service Requests

Previous : Finalizing a Servlet

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

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

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

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

Previous : Notifying Methods to Shut Down in Servlet Tutorial

Tagged , , , , , , , , ,