Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to call a class when werver starts  RSS feed

 
Cherry Mathew
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I need to call a method when the server starts to do all the initialization. Right now Iam using a servlet for that which is loaded automatically. But if i can call the method of a classs for this wont it be better. And i need to call another method of the same class to clean up the resources used.
Iam using tomcat is there any way to do this in tomcat or any server.
Thanks In advance
Cherry
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using the init method of a servlet to get some initialization done is perfectly alright. I don't understand what your problem is.
Bill

------------------
author of:
 
Anoop Krishnan
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hai Cherry,
If you are using Tomcat 4.I can give you some information
Tomcat 4 has a Servlet container called Catalina.You can use the LifecycleListener interface of Catalina to full fill your requirement
You have to do the following things
1) Write a class which implements org.apache.catalina.LifecycleListener and override the method
public void lifecycleEvent(LifecycleEvent event)(More more API info look at the API documentation)
create a jar file with this class and put it under
TOMCAT_HOME/server/lib
2)Configure the server.xml file to add a Listener for your Virtual host(for eg if you are using default virtual host)look for the following entry in your server.xml
<Host name="localhost" debug="0" appBase="webapps" unpackWARs="true">
....
....
</Host>
now change it like this by adding your listener
<Host name="localhost" debug="0" appBase="webapps" unpackWARs="true">
<Listener className="com.mycompany.mytools.mylistenercalss" />
....
</Host>
where com.mycompany.mytools.mylistenercalss is the class which implements org.apache.catalina.LifecycleListener interface
 
Cherry Mathew
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by William Brogden:
Using the init method of a servlet to get some initialization done is perfectly alright. I don't understand what your problem is.
Bill


Hi Bill,
Thank u for replying
I need to do some initialzation which is needed by all the servlets. I can do this using a servlet which will be loaded in startup. but i want to create a class with methods to do this instead of doing it using a InitializationServlet.
I think this is a genuine need in a web application. If u think otherwise please give me where im wrong and how to do it.
Thanks in advance
Cherry
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So go ahead and create the class - use the singleton pattern, and have every one of your servlets check to see if the instance you need has been created yet in the init method.
That way no matter which servlet is started first, you can be sure that your helper class object has been created. Something like:
static HelperClass helper ;
static synchronized HelperClass getHelper(){
if( helper == null ){ createHelper();
}
return helper ;
}
Personally I like to use a servlet devoted to initialization - if protected by a password, I can use that servlet to monitor system performance, memory use, etc. - your mileage may vary
Bill
 
john_guthrie
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
some app servers (weblogic comes to mind) have ways to specify start-up classes that get loaded by the JVM when it initializes. with weblogic 5.1, you add a line like this to your weblogic.properties file:
weblogic.system.startupClass.[virtualName]=[fullPackageName]
 
Cherry Mathew
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
Thank u john and william for the responses.
William,
I agree about the singleton class. but my real need comes not when the server startup but calling method of a class when the server shutsdown.
My servlets might be using many common resources and i may need to clean them up when the server shuts down.
I can put them in the destroy method of the servlet but wont it be better to do the clean up calling mehod when the server shutsdown.
Regards
Cherry
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!