You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Daniel Drasin <dr...@appliedreasoning.com> on 2002/02/08 14:36:47 UTC

Tomcat + Apache lifecycle question

I've set up Tomcat 4.x to work with Apache 1.3.x using the
Warp connector stuff.  I followed the standard instructions for
how to modify the httpd.conf file of apache, etc.  (All pretty 
straightforward...)  My problem is that i need to have a certain
activity occur when Tomcat starts up.  This activity is to initialize
a certain set of java objects and make them available to servlets.
Because of classpath issues (i.e. classloaders) i was unable to use
the lifecycle hooks of the contexts and had to settle for the 
(hackish) method of making a dummy servlet that only exists to
have its <load-on-startup> value set (excerpt from the web.xml is
below).  This servlet has an init() definition that initializes the 
objects in question (example definition below.)

The issue is that i need to have this activity occur once and only
once - so i add a static flag into the init method to see if it's
already run.  However, this doesn't work.  When i start tomcat,
the initialization code runs and when i start apache, it runs 
again - despite the flag.  (Output included below).

My guess is that the flag doesn't help because these init() calls
are occuring in two different object spaces (class loaders) so the
flag really is false both times.

So my question is - what's going on here.  Fundamentally, how can
i achieve my goal outlined in paragraph 1.  At a deeper level, my
question is - why do i get two calls to startup?  Is tomcat starting
a new context for apache?  If so, why doesn't it use the one that 
it's already started?  Is there a way to surpress the starting of
one of the contexts?  

Any help is appreciated

Dan
-------------------------CODE AND OUTPUT---------------------------

...
    <servlet>
      <servlet-name>
          ServletTest
      </servlet-name>
      <servlet-class>
          ServletTest
      </servlet-class>
      <load-on-startup>0</load-on-startup>
...

...
public class ServletTest extends HttpServlet {
    static int i = 0;
    static boolean flag = false;
    static {
	i++;
	if(!flag) {
         flag = true;
         System.out.println("***********GOT " + i + " **************");
      }
    }
...

...
Starting service Tomcat-Standalon
Apache Tomcat/4.0.1
***********GOT 1 **************
Starting service Tomcat-Apache
Apache Tomcat/4.0.1
***********GOT 1 **************
...

-- 
	Daniel Drasin				Applied Reasoning
	drasin@appliedreasoning.com		8612 Long Meadow Ct.
	(410)-461-6168				Columbia, MD, 21045
	http://www.appliedreasoning.com

--
To unsubscribe:   <ma...@jakarta.apache.org>
For additional commands: <ma...@jakarta.apache.org>
Troubles with the list: <ma...@jakarta.apache.org>


Re: Tomcat + Apache lifecycle question

Posted by Julien OIX <ju...@presidence.univ-nantes.fr>.
Daniel Drasin a écrit :
> 
> I've set up Tomcat 4.x to work with Apache 1.3.x using the
> Warp connector stuff.  I followed the standard instructions for
> how to modify the httpd.conf file of apache, etc.  (All pretty
> straightforward...)  My problem is that i need to have a certain
> activity occur when Tomcat starts up.  This activity is to initialize
> a certain set of java objects and make them available to servlets.
> Because of classpath issues (i.e. classloaders) i was unable to use
> the lifecycle hooks of the contexts and had to settle for the
> (hackish) method of making a dummy servlet that only exists to
> have its <load-on-startup> value set (excerpt from the web.xml is
> below).  This servlet has an init() definition that initializes the
> objects in question (example definition below.)
> 
> The issue is that i need to have this activity occur once and only
> once - so i add a static flag into the init method to see if it's
> already run.  However, this doesn't work.  When i start tomcat,
> the initialization code runs and when i start apache, it runs
> again - despite the flag.  (Output included below).
> 
> My guess is that the flag doesn't help because these init() calls
> are occuring in two different object spaces (class loaders) so the
> flag really is false both times.
> 
> So my question is - what's going on here.  Fundamentally, how can
> i achieve my goal outlined in paragraph 1.  At a deeper level, my
> question is - why do i get two calls to startup?  Is tomcat starting
> a new context for apache?  If so, why doesn't it use the one that
> it's already started?  Is there a way to surpress the starting of
> one of the contexts?
> 
> Any help is appreciated
> 
> Dan
> -------------------------CODE AND OUTPUT---------------------------
> 
> ...
>     <servlet>
>       <servlet-name>
>           ServletTest
>       </servlet-name>
>       <servlet-class>
>           ServletTest
>       </servlet-class>
>       <load-on-startup>0</load-on-startup>
> ...
> 
> ...
> public class ServletTest extends HttpServlet {
>     static int i = 0;
>     static boolean flag = false;
>     static {
>         i++;
>         if(!flag) {
>          flag = true;
>          System.out.println("***********GOT " + i + " **************");
>       }
>     }
> ...
> 
> ...
> Starting service Tomcat-Standalon
> Apache Tomcat/4.0.1
> ***********GOT 1 **************

delete this service ( everything in the right <Service> section ) from
server.xml

hope this helps 


> Starting service Tomcat-Apache
> Apache Tomcat/4.0.1
> ***********GOT 1 **************
> ...
> 
> --
>         Daniel Drasin                           Applied Reasoning
>         drasin@appliedreasoning.com             8612 Long Meadow Ct.
>         (410)-461-6168                          Columbia, MD, 21045
>         http://www.appliedreasoning.com
> 
> --
> To unsubscribe:   <ma...@jakarta.apache.org>
> For additional commands: <ma...@jakarta.apache.org>
> Troubles with the list: <ma...@jakarta.apache.org>

-- 
Julien OIX
Service Informatique de Gestion
Tél: 02 40 99 83 65
mail: julien.oix@presidence.univ-nantes.fr

--
To unsubscribe:   <ma...@jakarta.apache.org>
For additional commands: <ma...@jakarta.apache.org>
Troubles with the list: <ma...@jakarta.apache.org>