• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Starting process threads when Tomcat starts

 
Ranch Hand
Posts: 323
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Implement ServletContextListener.
 
Bai Shen
Ranch Hand
Posts: 323
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ew. You guys are ugly with a capital UG. Here, maybe this tiny ad can help:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic