Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2005

Tip: Looking for answers? Try searching our database.

Launch a class on tomcat startup

Thread view: 
ramprasad.madathil@gmail.com - 07 Nov 2005 12:42 GMT
Hi,

    I was wondering if there is any means by which I could launch my
own class when tomcat starts (in the same jvm).

    This class would start a new thread that, in a loop, polls some
log files for updates and take appropriate action.

    I know that I can possibly use a servlet with a load-on-startup
value greater than 1 and code my requirement into the init method.
However, using a servlet for a functionality different from servicing
http requests has me worried. (valid ?)

   I googled hard and found that I could probably use a lifecycle
listener - but couldnt find any documentation on the how-to 's. Any
pointers would help.

   I could also possibly use a Context Listener - will that help -
will I run into problems creating my own threads from within container
managed classes ?

  Thanks in advance for any help.

Ram.
Bryce - 07 Nov 2005 14:24 GMT
>Hi,
>
[quoted text clipped - 16 lines]
>will I run into problems creating my own threads from within container
>managed classes ?

Definitely, use a Context Listener, specifically, the
javax.Servlet.ServletContextListener

See here:
http://www.onjava.com/pub/a/onjava/2001/04/12/listeners.html

>   Thanks in advance for any help.
>
>Ram.

--
now with more cowbell
Ram - 08 Nov 2005 06:32 GMT
Thanks for the reply, Bryce.

I guess I have to use a listener after all - either the
ServletContextListener or Tomcat specific Lifecycle Listener, though
the later, as I said binds me to Tomcat (which is ok for me).

Are there any other specific (dis)advantages of using either especially
when I have to create threads from within them (on context startup)?
Other than memory leaks, killing the thread objects & associated
resources on context shutdown, is there anything else that I need to
watch out for while using threads.

I need to instantiate a Thread/TimerTask because the requirement is
such that the application would have continous data drops (in the form
of logs) which has to be picked up, parsed and pushed into a db.
Another part of the application reads the data from the db on requests
(http) and displays it over the web. The thread has to poll for arrival
of new logs and then hand it over to a framework for parsing and
inserting the parsed data to a db.

Thanks,
Ram.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.