File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Tomcat and the fly likes Starting process threads when Tomcat starts Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Products » Tomcat
Bookmark "Starting process threads when Tomcat starts" Watch "Starting process threads when Tomcat starts" New topic
Author

Starting process threads when Tomcat starts

Bai Shen
Ranch Hand

Joined: Sep 24, 2008
Posts: 323
I have a servlet that I've created. It takes some information that the user input on a form and places it in a database.

I have a program that spawns a thread which periodically checks the database and acts on the data there. I want this program/thread to run while tomcat is running. I could start it from the servlet in the doPost method, but then it would only run once someone actually hits the servlet. I want it to run as soon as I start tomcat.

How would I go about doing this?
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
Implement ServletContextListener.
Bai Shen
Ranch Hand

Joined: Sep 24, 2008
Posts: 323
Bauke Scholtz wrote:Implement ServletContextListener.


Can you give me some more details? I did some googleing, but I'm not quite sure how this would work for my setup.

I see that it has a contextInitilized method and a contextDestroyed method. Do those get called when the server is started and stopped?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18650
    
    8

Basically, yes. Actually they get called when the application is started and stopped (the "servlet context" corresponds to your web application).
Bai Shen
Ranch Hand

Joined: Sep 24, 2008
Posts: 323
Paul Clapham wrote:Basically, yes. Actually they get called when the application is started and stopped (the "servlet context" corresponds to your web application).


I just implemented ServletContextListener and neither of the methods got called.

And I don't want my process to be called when the application starts. That I can do just by putting a thread start call in the servlet class. I'm looking for a way to make this process thread start as soon as tomcat starts.

All I did was create a class that implemented ServletContextListener and added it to the src folder in my Eclipse Servlet project. Is there more that I need to do in order for it to be called?

EDIT: NM, I figured it out. I didn't realize I had to add the listener piece to my web.xml. Thanks.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Bai Shen wrote:

And I don't want my process to be called when the application starts. That I can do just by putting a thread start call in the servlet class. I'm looking for a way to make this process thread start as soon as tomcat starts.


Why?
Is there any reason that you would want this process to start before your app was up and running?


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Bai Shen
Ranch Hand

Joined: Sep 24, 2008
Posts: 323
Ben Souther wrote:
Bai Shen wrote:

And I don't want my process to be called when the application starts. That I can do just by putting a thread start call in the servlet class. I'm looking for a way to make this process thread start as soon as tomcat starts.


Why?
Is there any reason that you would want this process to start before your app was up and running?


Yup. As I said, my servlet enters data into a database. This other process takes the data from the database and uses it. So I don't need to wait for the servlet to be up and running before I get the data from the database.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

If that's the case, then it might make sense to separate it from your web app and Tomcat altogether.

Have you considered writing a command line interface to it and, simply calling it from a cron job or Windows scheduled task?
Bai Shen
Ranch Hand

Joined: Sep 24, 2008
Posts: 323
Ben Souther wrote:If that's the case, then it might make sense to separate it from your web app and Tomcat altogether.

Have you considered writing a command line interface to it and, simply calling it from a cron job or Windows scheduled task?


Yup. I know it's weird, but it works.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

So...

Have you considered writing a command line interface to it and, simply calling it from a cron job or Windows scheduled task?
Bai Shen
Ranch Hand

Joined: Sep 24, 2008
Posts: 323
Ben Souther wrote:So...

Have you considered writing a command line interface to it and, simply calling it from a cron job or Windows scheduled task?


I answered you. I said "Yup."

As I mentioned, I looked at those options, but for my needs, what I've done works the way I need it to. I don't need the job running all the time. Just when the web server is up.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Starting process threads when Tomcat starts