To start a background thread in tomcat
Hi,
I want to know if its possible to run a thread in background as tomcat is started.
The objective is to check my database & particularly some fields of the database to make immediate changes in its values.
I want the thread to run till i shutdown tomcat. Please let me know How to do so?
Re: To start a background thread in tomcat
Yes you can do it with the help of ServletContextListener.
Its registered in web.xml file in tomcat & will be called on the start & stop of your application. You can create the thread normally like the way you do it in java- ExecutionService.
I hope this helps.
Re: To start a background thread in tomcat
What you want is a lifecyle Listener. With the new Servlet engine spec (2.3 (?)) you can specify a listener object in your web.xml. The class references must implement the ServletContextListener interface. This has a contextIntialized and a contextDestroyed method. You can have the former launch your thread and the latter close it down (probably by calling interrupt on it).