You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Anagha Mudigonda <an...@india.hp.com> on 2001/09/11 06:52:12 UTC

on startup

hi all,
I have a java class that needs to be loaded at the startup of the tomcat

server. And I want it to persist as long as tomcat is alive. Is it
possible ?
If so where do i put the class ?
Can i do it by changing something in any of the config files ?


help please.

regards
anagha.

--
Where the mind is without fear and the head is held high;
Where knowledge is free;
Where the world has not been broken up into fragments by narrow domestic
walls;
Where words come out from the depths of truth;
...............
Where the mind is led by thee into ever-widening thought and action;
Into that heaven of freedom,my Father,let my country awake.



Re: on startup

Posted by "Craig R. McClanahan" <cr...@apache.org>.

On Tue, 11 Sep 2001, simon wrote:

> Date: Tue, 11 Sep 2001 13:53:42 +0900
> From: simon <si...@lexues.co.jp>
> Reply-To: tomcat-user@jakarta.apache.org
> To: tomcat-user@jakarta.apache.org
> Subject: Re: on startup
>
>
> ----- Original Message -----
> From: "Kamran Mazandrani" <ka...@netropolis.net>
> To: <to...@jakarta.apache.org>
> Sent: Tuesday, September 11, 2001 1:54 PM
> Subject: Re: on startup
>
>
> > Date: Fri, 7 Sep 2001 11:11:10 -0700 (PDT)
> > From: "Craig R. McClanahan" <cr...@apache.org>
> > Sender: <cr...@localhost>
> > To: <to...@jakarta.apache.org>
> > Subject: RE: How can I have a class run on start-up?
> >
> > For a portable solution to the "run my class at startup" problem, Servlet
> > 2.3 (and therefore Tomcat 4.0) supports a new API called
> > javax.servlet.ServletContextListener. If you register such a listener in
> > your web.xml file, the container will call the contextInitialized() method
> > when the web application starts up, and contextDestroyed() when the web
> > application is shut down.
> > This is safer than the typical approach (use the init() method of a
> > load-on-startup servlet), because the servlet specification does *not*
> > guarantee to keep any particular servlet instance in memory for the life
> > of the application (although Tomcat actually does so).
> > Craig
>
> Is this true?

See the Servlet Specification, either 2.2 or 2.3, the chapter on Servlets.
In 2.3, it's in Section 2.3.4, the very first sentence.  You can download
the spec from:

  http://java.sun.com/products/servlet/download.html

I'm continually surprised by how many developers don't know about the
servlet specs.  The rules defined here are *just as important* as the
rules stated in the Javadocs (in fact, the 2.3 spec *includes* the
Javadocs :-) at defining what a servlet container does and doesn't do for
you.

Long time readers of TOMCAT-USER will note that this has been my favorite
URL reference lately -- there is a message here :-) :-)

>  Under what circumstances would a servlet be removed from memory?
>

Remember that containers often run for a very long time, against a request
processing load that varies dynamically.  If the container detects that it
is close to running out of memory, it makes a lot of sense to consider
deactivating servlet instances that haven't been called for a while, in
order to reuse the memory space occupied by those servlets.  They will be
restarted automatically if they are requested again, so there's no
difference to the programming model -- except that you need to be aware
that servlet instances are not permanent.

> simon
>
>
>

Craig



RE: on startup

Posted by Peter Romianowski <pe...@antaramusic.de>.
> > This is safer than the typical approach (use the init() method of a
> > load-on-startup servlet), because the servlet specification does *not*
> > guarantee to keep any particular servlet instance in memory for the life
> > of the application (although Tomcat actually does so).
> > Craig

> Is this true?  Under what circumstances would a servlet be removed from
memory?

the specification does not guarantee that a servlet instance will be kept
all over the lifetime of the application. so it is implementation-dependent.
thus you can never be sure, that your servlet will be alive. i.e. a servlet
engine could remove a servlet if it has not been used for some amount of
time and recreate it when accessed again.
i dont know how other servlet engines handle this, but Craig is right when
he says that tomcat actually keeps the instance of a servlet but you
shouldn't rely on that (at least for the sake of portability)... and using
tomcat 4 his aproach is the by far the cleanest...

sincerly,
pero


Re: on startup

Posted by simon <si...@lexues.co.jp>.
----- Original Message ----- 
From: "Kamran Mazandrani" <ka...@netropolis.net>
To: <to...@jakarta.apache.org>
Sent: Tuesday, September 11, 2001 1:54 PM
Subject: Re: on startup


> Date: Fri, 7 Sep 2001 11:11:10 -0700 (PDT)
> From: "Craig R. McClanahan" <cr...@apache.org>
> Sender: <cr...@localhost>
> To: <to...@jakarta.apache.org>
> Subject: RE: How can I have a class run on start-up?
> 
> For a portable solution to the "run my class at startup" problem, Servlet
> 2.3 (and therefore Tomcat 4.0) supports a new API called
> javax.servlet.ServletContextListener. If you register such a listener in
> your web.xml file, the container will call the contextInitialized() method
> when the web application starts up, and contextDestroyed() when the web
> application is shut down.
> This is safer than the typical approach (use the init() method of a
> load-on-startup servlet), because the servlet specification does *not*
> guarantee to keep any particular servlet instance in memory for the life
> of the application (although Tomcat actually does so).
> Craig

Is this true?  Under what circumstances would a servlet be removed from memory?  

simon



Re: on startup

Posted by Kamran Mazandrani <ka...@netropolis.net>.
Mailing-List: contact tomcat-user-help@jakarta.apache.org; run by ezmlm
Reply-To: tomcat-user@jakarta.apache.org
list-help: <ma...@jakarta.apache.org>
list-unsubscribe: <ma...@jakarta.apache.org>
list-post: <ma...@jakarta.apache.org>
Delivered-To: mailing list tomcat-user@jakarta.apache.org
Date: Fri, 7 Sep 2001 11:11:10 -0700 (PDT)
From: "Craig R. McClanahan" <cr...@apache.org>
Sender: <cr...@localhost>
To: <to...@jakarta.apache.org>
Subject: RE: How can I have a class run on start-up?
X-Spam-Rating: localhost 1.6.2 0/1000/N
X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N
X-RCPT-TO: <ka...@netropolis.net>

For a portable solution to the "run my class at startup" problem, Servlet
2.3 (and therefore Tomcat 4.0) supports a new API called
javax.servlet.ServletContextListener. If you register such a listener in
your web.xml file, the container will call the contextInitialized() method
when the web application starts up, and contextDestroyed() when the web
application is shut down.
This is safer than the typical approach (use the init() method of a
load-on-startup servlet), because the servlet specification does *not*
guarantee to keep any particular servlet instance in memory for the life
of the application (although Tomcat actually does so).
Craig

On Fri, 7 Sep 2001, Peter Romianowski wrote:
 > Date: Fri, 7 Sep 2001 19:35:32 +0200
 > From: Peter Romianowski <an...@gmx.de>
 > Reply-To: tomcat-user@jakarta.apache.org
 > To: tomcat-user@jakarta.apache.org
 > Subject: RE: How can I have a class run on start-up?
 >
 > you could implement a org.apache.catalina.LifecycleListener and attach 
it to
 > the context you are using like this:
 >
 > <Context path="/myapp" ...>
 > ...
 > <Listener className="com.mycompany.MyAppListener"/>
 > ...
 > </Context>
 >
 > look into the docs for details.
 > but this is a tomcat-specific thing (and thus not portable).
 >
 > anyway there's a second approach that comes in mind (but it is not as
 > "clean" as the first one)
 >
 > you simply could write a servlet where you put all code which should be
 > executed during webapp-initialization into the init-method.
 > in web.xml you configure the servlet to load on startup. it goes like this:
 >
 > <servlet>
 > <servlet-name>somename</servlet-name>
 > <servlet-class>someclass</servlet-class>
 > <load-on-startup>a value greater 0</load-on-startup>
 > </servlet>
 >
 > then the servlet will be called upon webapp-initialization.
 >
 > dont know if there's is a better (cleaner and portable) solution, but these
 > two do the job.
 >
 > greets,
 > pero
 >
 > -----Original Message-----
 > From: Jonathan Eric Miller [mailto:jemiller@uchicago.edu]
 > Sent: Friday, September 07, 2001 7:15 PM
 > To: tomcat-user@jakarta.apache.org
 > Subject: Re: How can I have a class run on start-up?
 >
 >
 > I don't know the answer to your question, but, I'm wondering if the
 > application actually has to run in Tomcat. It sounds like you might want to
 > just create a standalone application that listens on a port.
 >
 > Jon
 >
 > ----- Original Message -----
 > From: "Alex Colic" <al...@pop-ware.com>
 > To: "Tomcat-User" <to...@jakarta.apache.org>
 > Sent: Friday, September 07, 2001 8:36 AM
 > Subject: How can I have a class run on start-up?
 >
 >
 > > Hi,
 > >
 > > hopefully someone can help me with this. I need some type of a class to
 > > start when the web server starts. This class is going to bind itself to a
 > > port and listen to commands from a VB app. Other classes in other web 
apps
 > > will register themselves with this class to receive these commands.
 > >
 > > My questions are:
 > >
 > > 1: how can you have a class start when the web server starts? This needs
 > to
 > > work with all web servers.
 > > 2: how can you have a class in a web app register itself with the class
 > > listening on the port?
 > >
 > > Any suggestions are appreciated.
 > >
 > > Regards
 > >
 > > Alex Colic
 > >
 >
 >




At 10:22 AM 9/11/2001 +0530, you wrote:
>hi all,
>I have a java class that needs to be loaded at the startup of the tomcat
>
>server. And I want it to persist as long as tomcat is alive. Is it
>possible ?
>If so where do i put the class ?
>Can i do it by changing something in any of the config files ?
>
>
>help please.
>
>regards
>anagha.
>
>--
>Where the mind is without fear and the head is held high;
>Where knowledge is free;
>Where the world has not been broken up into fragments by narrow domestic
>walls;
>Where words come out from the depths of truth;
>...............
>Where the mind is led by thee into ever-widening thought and action;
>Into that heaven of freedom,my Father,let my country awake.