Launch a class on tomcat startup

  • Thread starter ramprasad.madathil
  • Start date
R

ramprasad.madathil

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.
 
B

Bryce

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 ?

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

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

Ram

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,276
Latest member
Sawatmakal

Latest Threads

Top