• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Starting process threads when Tomcat starts

 
Bai Shen
Ranch Hand
Posts: 323
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Implement ServletContextListener.
 
Bai Shen
Ranch Hand
Posts: 323
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 323
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Bai Shen
Ranch Hand
Posts: 323
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 323
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 323
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic