Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Launch a class on application startup

 
ramprasad madathil
Ranch Hand
Posts: 489
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was wondering if there is any means by which I could launch my own class when tomcat starts (in the same jvm).

I have to create new Threads on startup - threads that run in a loop and poll some events. I was thinking of coding this functionality into a static method in a class and fire it off when tomcat starts.

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 ?)

1. I googled hard and found that I could probably use a context lifecycle listener.
2. I could also possibly use the servlet api - the ServletContextListener.

I know that using tomcat specific methodolgy would bind me to the server - but other than that are there any advantages/disadvantages between the two above ?
Is it safe to create threads from within the 2 Listeners listed above ?

Thanks in advance for any help.
Ram.
 
Mark Spritzler
ranger
Sheriff
Posts: 17290
9
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, what type of events are you polling, and can you just use a Listener instead of creating a thread?

Mark
 
ramprasad madathil
Ranch Hand
Posts: 489
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply, Mark.

I cant do away with 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.

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 wonder is there is there a plain startup hook available in tomcat (rather than listeners) - from where I can launch a class that starts a thread in the same jvm as tomcat's ?

Thanks,
Ram.
 
Rajesh Agarwal
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could probably write a TimerTask. It goes this way.

Write the class
================
class StandAloneTimer extends TimerTask
{
public void run()
{
>>>implementation goes here<<<
}
}


In your servlet's init method you can schedule this timer
=========================================================
Timer timer = new Timer(true);
StandAloneTImer standAloneTimer = new StandAloneTimer();
timer.schedule(standAloneTimer, 0, 30000);

And configure the server to initialize on start up.
 
ramprasad madathil
Ranch Hand
Posts: 489
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply, Rajesh.
However its not the the code for the Thread or a TimerTask that has me worried - its where I would create them - as you mentioned having a servlet that loads on startup is one option (and which I mentioned in my first post and towards which Iam not too inclined). The other is the use of Listeners. What I wish for is some insight into the relative advantages/disadvantages of the use of one over the other. Any pointers on the pitfalls of creating threads that run in a loop (see my previous post), as long as the context is alive would also help.

Thanks again,
Ram.
 
ramprasad madathil
Ranch Hand
Posts: 489
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anybody? Apologies for bumping up.

Thanks,
Ram.
 
RohitNik Sharma
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I am also facing the same problem as you had faced regarding creating threads in tomcat.

My requirement is that i have already created such thread but now i want to increase the number of threads . Can you please tell me will id affect the performance of the application.

Rohit
 
RohitNik Sharma
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry forgot to write my email id

please send mail to me if possible

rohit.sharma@india.birlasoft.com
 
Frank Carver
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by ramprasad madathil:
What I wish for is some insight into the relative advantages/disadvantages of the use of one over the other. Any pointers on the pitfalls of creating threads that run in a loop (see my previous post), as long as the context is alive would also help.


I can't really answer your question right now, but a little more information might help. In order to sensibly decide which of the two options you suggest to use, I guess we really need to know a bit more about why you want to run this thread in the web application server at all.

One of the reasons that both of your choices seem a bit uncomfortable is that this is not really what a web server is designed to do. Is there any strong reason why you can't just run a separate application that polls your log drop and inserts new data into the database? Can you explain a bit about your choice to "piggy-back" this function onto a web server in the first place?
 
ramprasad madathil
Ranch Hand
Posts: 489
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Apologies for the late reply.


Originally quoted by Frank...

One of the reasons that both of your choices seem a bit uncomfortable is that this is not really what a web server is designed to do.


Yes, I know. I too, havent done such stuff before and that's why I am a bit uneasy about this. Since I posted this a while back, I have run some tests and found that having a thread (& an associated set of worker threads - I used the new java.util.Concurrent.Executors for this) doesnt slow down the web application in any significant manner. Fingers crossed, on how the app would perform if left running for several days on a stretch.


Is there any strong reason why you can't just run a separate application that polls your log drop and inserts new data into the database? Can you explain a bit about your choice to "piggy-back" this function onto a web server in the first place?


Yes. The first is that the application needs to be run from within a single jvm, not multiple ones. In other words, the whole application has to be packaged as one single, tightly couple unit. I know that, that may not be reason enough.
The biggest reason, though, is for the two systems (the one which drops data and the other, the web application, that consumes it) to work under a single transaction.
Thirdly and this is a small point, we can use the container provided utilities like connection pooling etc.

I have searched long and hard for help/experience on this kind of stuff in the hope that surely there should be applications (admittedly few) that works in such fashion. Not much luck though

Thanks,
Ram.
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by ramprasad madathil:
I was wondering if there is any means by which I could launch my own class when tomcat starts (in the same jvm).

I have to create new Threads on startup - threads that run in a loop and poll some events. I was thinking of coding this functionality into a static method in a class and fire it off when tomcat starts.

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 ?)

1. I googled hard and found that I could probably use a context lifecycle listener.
2. I could also possibly use the servlet api - the ServletContextListener.

I know that using tomcat specific methodolgy would bind me to the server - but other than that are there any advantages/disadvantages between the two above ?
Is it safe to create threads from within the 2 Listeners listed above ?

Thanks in advance for any help.
Ram.



what you can do is write a servlet which loads itself on start up..then you can have a inner class that should extend java.lang.Thread .so as soon as the application is loaded your servlets init() would be called whihc in turn would swamp the Thread created from the inner class..

that should be enough to help you in all the pooling related activities..

cheers!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!