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.
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
posted
0
Paul Clapham wrote:Basically, yes. Actually they get called when the application is started and stopped (the "servlet context" corresponds to your web application).
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.
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?
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.
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
posted
0
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.