changeset 930:333e60e5cae9

new counter for open requests.
author robcast
date Tue, 20 Dec 2011 15:51:36 +0100
parents e881ab1c2f47
children 58343be11fa3 5f03f6937c32
files servlet3/src/main/java/digilib/servlet/DigilibServletConfiguration.java servlet3/src/main/java/digilib/servlet/Initialiser.java servlet3/src/main/java/digilib/servlet/Scaler.java webapp/src/main/webapp/server/dlConfig.jsp
diffstat 4 files changed, 112 insertions(+), 67 deletions(-) [+]
line wrap: on
line diff
--- a/servlet3/src/main/java/digilib/servlet/DigilibServletConfiguration.java	Tue Dec 20 14:40:48 2011 +0100
+++ b/servlet3/src/main/java/digilib/servlet/DigilibServletConfiguration.java	Tue Dec 20 15:51:36 2011 +0100
@@ -24,6 +24,7 @@
 import java.io.File;
 import java.util.Map;
 import java.util.Map.Entry;
+import java.util.concurrent.atomic.AtomicInteger;
 
 import javax.servlet.ServletContext;
 import javax.servlet.ServletException;
@@ -50,6 +51,9 @@
  */
 public class DigilibServletConfiguration extends DigilibConfiguration {
 
+    /** counter for open HttpRequests (mostly for debugging) */
+    public AtomicInteger openRequestCnt = new AtomicInteger(0);
+
     /**
      * Definition of parameters and default values.
      */
--- a/servlet3/src/main/java/digilib/servlet/Initialiser.java	Tue Dec 20 14:40:48 2011 +0100
+++ b/servlet3/src/main/java/digilib/servlet/Initialiser.java	Tue Dec 20 15:51:36 2011 +0100
@@ -23,11 +23,14 @@
 import java.io.File;
 import java.io.OutputStream;
 import java.util.List;
+import java.util.concurrent.atomic.AtomicInteger;
 
 import javax.imageio.ImageIO;
 import javax.servlet.ServletContext;
 import javax.servlet.ServletContextEvent;
 import javax.servlet.ServletContextListener;
+import javax.servlet.ServletRequestEvent;
+import javax.servlet.ServletRequestListener;
 import javax.servlet.annotation.WebListener;
 
 import org.apache.log4j.Logger;
@@ -48,7 +51,7 @@
  *  
  */
 @WebListener
-public class Initialiser implements ServletContextListener {
+public class Initialiser implements ServletContextListener, ServletRequestListener {
 
 
 	/** servlet version */
@@ -61,7 +64,7 @@
 	DocuDirCache dirCache;
 
 	/** DigilibConfiguration instance */
-	DigilibConfiguration dlConfig;
+	DigilibServletConfiguration dlConfig;
 
 	/** Executor for digilib image jobs (AsyncServletWorker doesn't return anything) */
 	DigilibJobCenter<DocuImage> imageEx;
@@ -201,4 +204,16 @@
         }
     }
 
+    @Override
+    public void requestDestroyed(ServletRequestEvent arg0) {
+        int i = dlConfig.openRequestCnt.decrementAndGet();
+        logger.debug("ServletRequest destroyed. (cnt="+i+")");
+    }
+
+    @Override
+    public void requestInitialized(ServletRequestEvent arg0) {
+        int i = dlConfig.openRequestCnt.incrementAndGet();
+        logger.debug("ServletRequest created. (cnt="+i+")");
+    }
+
 }
--- a/servlet3/src/main/java/digilib/servlet/Scaler.java	Tue Dec 20 14:40:48 2011 +0100
+++ b/servlet3/src/main/java/digilib/servlet/Scaler.java	Tue Dec 20 15:51:36 2011 +0100
@@ -5,8 +5,6 @@
 import java.util.List;
 
 import javax.servlet.AsyncContext;
-import javax.servlet.AsyncEvent;
-import javax.servlet.AsyncListener;
 import javax.servlet.ServletConfig;
 import javax.servlet.ServletContext;
 import javax.servlet.ServletException;
@@ -27,23 +25,27 @@
 import digilib.io.ImageInput;
 import digilib.util.DigilibJobCenter;
 
-@WebServlet(name="Scaler", urlPatterns={"/Scaler", "/servlet/Scaler/*"}, asyncSupported=true)
+@WebServlet(name = "Scaler", urlPatterns = { "/Scaler", "/servlet/Scaler/*" }, asyncSupported = true)
 public class Scaler extends HttpServlet {
 
     private static final long serialVersionUID = 5289386646192471549L;
 
     /** digilib servlet version (for all components) */
-    public static final String version = "2.0b2 async";
+    public static final String version = "2.0b3 async";
 
     /** servlet error codes */
-    public static enum Error {UNKNOWN, AUTH, FILE, IMAGE};
-    
+    public static enum Error {
+        UNKNOWN, AUTH, FILE, IMAGE
+    };
+
     /** type of error message */
-    public static enum ErrMsg {IMAGE, TEXT, CODE};
-    
+    public static enum ErrMsg {
+        IMAGE, TEXT, CODE
+    };
+
     /** default error message type */
     public static ErrMsg defaultErrMsgType = ErrMsg.IMAGE;
-    
+
     /** logger for accounting requests */
     protected static Logger accountlog = Logger.getLogger("account.request");
 
@@ -100,7 +102,8 @@
         // get our ServletContext
         ServletContext context = config.getServletContext();
         // see if there is a Configuration instance
-        dlConfig = (DigilibConfiguration) context.getAttribute("digilib.servlet.configuration");
+        dlConfig = (DigilibConfiguration) context
+                .getAttribute("digilib.servlet.configuration");
         if (dlConfig == null) {
             // no Configuration
             throw new ServletException("No Configuration!");
@@ -124,7 +127,8 @@
                 (File) dlConfig.getValue("notfound-image"), context);
         sendFileAllowed = dlConfig.getAsBoolean("sendfile-allowed");
         try {
-            defaultErrMsgType = ErrMsg.valueOf(dlConfig.getAsString("default-errmsg-type"));
+            defaultErrMsgType = ErrMsg.valueOf(dlConfig
+                    .getAsString("default-errmsg-type"));
         } catch (Exception e) {
             // nothing to do
         }
@@ -153,40 +157,50 @@
         return mtime;
     }
 
-    /* (non-Javadoc)
-     * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
+    /*
+     * (non-Javadoc)
+     * 
+     * @see
+     * javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest
+     * , javax.servlet.http.HttpServletResponse)
      */
-    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException {
+    public void doGet(HttpServletRequest request, HttpServletResponse response)
+            throws ServletException {
         accountlog.info("GET from " + request.getRemoteAddr());
         this.processRequest(request, response);
     }
 
-
-    /* (non-Javadoc)
-     * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
+    /*
+     * (non-Javadoc)
+     * 
+     * @see
+     * javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest
+     * , javax.servlet.http.HttpServletResponse)
      */
-    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException {
+    public void doPost(HttpServletRequest request, HttpServletResponse response)
+            throws ServletException {
         accountlog.info("POST from " + request.getRemoteAddr());
         this.processRequest(request, response);
     }
-    
 
-	protected void doHead(HttpServletRequest req, HttpServletResponse resp)
-			throws ServletException, IOException {
-		logger.debug("HEAD from "+req.getRemoteAddr());
-		super.doHead(req, resp);
-	}
+    protected void doHead(HttpServletRequest req, HttpServletResponse resp)
+            throws ServletException, IOException {
+        logger.debug("HEAD from " + req.getRemoteAddr());
+        super.doHead(req, resp);
+    }
 
-	protected void doOptions(HttpServletRequest req, HttpServletResponse resp)
-			throws ServletException, IOException {
-		logger.debug("OPTIONS from "+req.getRemoteAddr());
-		super.doOptions(req, resp);
-	}
+    protected void doOptions(HttpServletRequest req, HttpServletResponse resp)
+            throws ServletException, IOException {
+        logger.debug("OPTIONS from " + req.getRemoteAddr());
+        super.doOptions(req, resp);
+    }
 
-	/** Service this request using the response.
+    /**
+     * Service this request using the response.
+     * 
      * @param request
      * @param response
-     * @throws ServletException 
+     * @throws ServletException
      */
     public void processRequest(HttpServletRequest request,
             HttpServletResponse response) throws ServletException {
@@ -199,28 +213,30 @@
         accountlog.debug("request: " + request.getQueryString());
         logger.debug("request: " + request.getQueryString());
         logger.debug("headers: " + ServletOps.headersToString(request));
-        //logger.debug("response:"+ response + " committed=" + response.isCommitted());
+        // logger.debug("response:"+ response + " committed=" +
+        // response.isCommitted());
         final long startTime = System.currentTimeMillis();
 
         // parse request
         DigilibServletRequest dlRequest = new DigilibServletRequest(request);
         // extract the job information
-        final ImageJobDescription jobTicket = ImageJobDescription.getInstance(dlRequest, dlConfig);
+        final ImageJobDescription jobTicket = ImageJobDescription.getInstance(
+                dlRequest, dlConfig);
 
         // type of error reporting
         ErrMsg errMsgType = defaultErrMsgType;
         if (dlRequest.hasOption("errimg")) {
             errMsgType = ErrMsg.IMAGE;
         } else if (dlRequest.hasOption("errtxt")) {
-        	errMsgType = ErrMsg.TEXT;
+            errMsgType = ErrMsg.TEXT;
         } else if (dlRequest.hasOption("errcode")) {
-        	errMsgType = ErrMsg.CODE;
+            errMsgType = ErrMsg.CODE;
         }
-        
+
         try {
-        	/*
-        	 *  check if we can fast-track without scaling
-        	 */
+            /*
+             * check if we can fast-track without scaling
+             */
             ImageInput fileToLoad = (ImageInput) jobTicket.getInput();
 
             // check permissions
@@ -246,16 +262,21 @@
                     mt = "application/octet-stream";
                 }
                 logger.debug("Sending RAW File as is.");
-                ServletOps.sendFile(fileToLoad.getFile(), mt, null, response, logger);
-                logger.info("Done in " + (System.currentTimeMillis() - startTime) + "ms");
+                ServletOps.sendFile(fileToLoad.getFile(), mt, null, response,
+                        logger);
+                logger.info("Done in "
+                        + (System.currentTimeMillis() - startTime) + "ms");
                 return;
             }
 
-            // if possible, send the image without actually having to transform it
-            if (! jobTicket.isTransformRequired()) {
+            // if possible, send the image without actually having to transform
+            // it
+            if (!jobTicket.isTransformRequired()) {
                 logger.debug("Sending File as is.");
-                ServletOps.sendFile(fileToLoad.getFile(), null, null, response, logger);
-                logger.info("Done in " + (System.currentTimeMillis() - startTime) + "ms");
+                ServletOps.sendFile(fileToLoad.getFile(), null, null, response,
+                        logger);
+                logger.info("Done in "
+                        + (System.currentTimeMillis() - startTime) + "ms");
                 return;
             }
 
@@ -265,11 +286,12 @@
                 response.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE);
                 return;
             }
-            
+
             // worker job is done asynchronously
             AsyncContext asyncCtx = request.startAsync(request, response);
             // create job
-            AsyncServletWorker job = new AsyncServletWorker(dlConfig, jobTicket, asyncCtx, errMsgType, startTime);
+            AsyncServletWorker job = new AsyncServletWorker(dlConfig,
+                    jobTicket, asyncCtx, errMsgType, startTime);
             // AsyncServletWorker is its own AsyncListener
             asyncCtx.addListener(job);
             // submit job
@@ -288,7 +310,7 @@
         } catch (Exception e) {
             logger.error("Other Exception: ", e);
             // TODO: should we rethrow or swallow?
-            //throw new ServletException(e);
+            // throw new ServletException(e);
         }
     }
 
@@ -326,7 +348,7 @@
             }
             if (response.isCommitted()) {
                 // response already committed
-                logger.warn("Response committed for error "+msg);
+                logger.warn("Response committed for error " + msg);
             }
             if (type == ErrMsg.TEXT) {
                 ServletOps.htmlMessage(msg, response);
--- a/webapp/src/main/webapp/server/dlConfig.jsp	Tue Dec 20 14:40:48 2011 +0100
+++ b/webapp/src/main/webapp/server/dlConfig.jsp	Tue Dec 20 15:51:36 2011 +0100
@@ -20,7 +20,7 @@
 
 <%
 // get digilib config
-digilib.servlet.DigilibConfiguration dlConfig = docBean.getDlConfig();
+digilib.servlet.DigilibServletConfiguration dlConfig = docBean.getDlConfig();
 // parsing the query
 digilib.servlet.DigilibServletRequest dlRequest = new digilib.servlet.DigilibServletRequest(request);
 // add number of pages
@@ -46,27 +46,27 @@
     java.util.Arrays.sort(keys);
     int l = keys.length;
     for (int i = 0; i < l; i++) {
-	String key = (String) keys[i];
-	String val = dlConfig.getAsString(key);
-	if (key.equals("basedir-list")) {
-	    String[] bd = (String[]) dlConfig.getValue("basedir-list");
-	    val = "";
-	    if (bd != null) {
-		for (int j = 0; j < bd.length; j++) {
-		    val += bd[j] + "<br> ";
-		}
-	    }
-	}
-	if (val.length() == 0) {
-	    val = "(none)";
-	}
+        String key = (String) keys[i];
+       	String val = dlConfig.getAsString(key);
+        if (key.equals("basedir-list")) {
+            String[] bd = (String[]) dlConfig.getValue("basedir-list");
+            val = "";
+            if (bd != null) {
+                for (int j = 0; j < bd.length; j++) {
+                    val += bd[j] + "<br> ";
+                }
+            }
+        }
+        if (val.length() == 0) {
+            val = "(none)";
+        }
 %>
   <tr>
     <td valign="top"><%= key %></td><td><b><%= val %></b></td>
     <td></td>
   </tr>
 <%
-       }
+   }
 %>
 </table>
 
@@ -81,6 +81,10 @@
     <td>currently running</td><td><b><%= imageProcessor.getRunningJobs() %></b></td>
     <td></td>
   </tr>
+  <tr>
+    <td>currently open requests</td><td><b><%= dlConfig.openRequestCnt.get() %></b></td>
+    <td>(including this)</td>
+  </tr>
 </table>
 
 <h2>Directory cache</h2>