You could launch each webapps initialization code in a separate thread.

Here is what we do. Our load is very heavy and our servers are responsive after restart much sooner using this technique.

UniverseLoader.java:
package com.example;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Date;

public class UniverseLoader extends HttpServlet {

    protected long timeLoaded;

    public void init() throws ServletException {
        new Thread(new LoadUniverseTask()).start();
    }

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.getWriter().println("Universe loaded: " + new Date(timeLoaded));
    }

    public void loadUniverse() throws Exception {
        long timeStart = System.currentTimeMillis();

        // Your initialization code

        long elapsed = (System.currentTimeMillis() - timeStart) / 1000;
        log("Universe loaded: " + elapsed + " seconds");
        timeLoaded = System.currentTimeMillis();
    }

    private class LoadUniverseTask implements Runnable {
        public void run(){
            try {
                loadUniverse();
            } catch(Exception e){
                StringWriter msg = new StringWriter();
                PrintWriter pout = new PrintWriter(msg);
                e.printStackTrace(pout);
            }
        }

    }
}

In WEB-INF/web.xml
    <servlet>
        <servlet-name>UniverseLoader</servlet-name>
        <servlet-class>com.example.UniverseLoader</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>UniverseLoader</servlet-name>
        <url-pattern>/loaduniverse</url-pattern>
    </servlet-mapping>

Regards,
Dave

On Dec 10, 2009, at 5:34 PM, sh...@radiantblue.com wrote:

Thank you Chuck. That is exactly what I was looking for.


-------- Original Message --------
Subject: RE: Context Chicken & Egg Problem
From: "Caldarale, Charles R" <chuck.caldar...@unisys.com>
Date: Thu, December 10, 2009 6:24 pm
To: Tomcat Users List <users@tomcat.apache.org>

From: sh...@radiantblue.com [mailto:sh...@radiantblue.com]
Subject: RE: Context Chicken & Egg Problem

just a curiosity as to what is happening under the Tomcat
covers during this scenario.

Tomcat initialization is single-thread, so if webapp A happens to get
initialized before B, and the initialization thread running inside A
makes an HTTP request that targets a yet-to-be initialized webapp in the
same Tomcat instance, you have a deadlock. Someday maybe, we'll see
multi-threaded initialization.

- Chuck


THIS COMMUNICATION MAY CONTAIN CONFIDENTIAL AND/OR OTHERWISE PROPRIETARY
MATERIAL and is thus for use only by the intended recipient. If you
received this in error, please contact the sender and delete the e- mail
and its attachments from all computers.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
For additional commands, e-mail: users-h...@tomcat.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
For additional commands, e-mail: users-h...@tomcat.apache.org

Reply via email to