How to stop Thread when tomcat shuts down

Hi,

When I shut down tomcat, tomcat shuts down but the console window stays open and the system.out.prinln text from my class using Thread still shows.

Can I sense in my class if tomcat is still running? Or maby I call a method when tomcat stops to kill my thread?

In my web.xml I have the following code:

<servlet>

<servlet-name>Background Processor</servlet-name>

<servlet-class>src.com.business.Engine</servlet-class>

<load-on-startup>1</load-on-startup>

</servlet>

The Engine class is as follows:

publicclass Engineextends HttpServlet{

publicvoid init(ServletConfig config)throws ServletException{

super.init(config);

CheckEndedItems checkEndedItems = CheckEndedItems.getInstance();

checkEndedItems.start();

}

}

The CheckEndedItems class implements Thread

Does someone know how I can stop this Thread when tomcat shuts down?

Thanks.

[1291 byte] By [jposthuma] at [2007-11-27 11:35:08]
# 1

Try to set it as a daemon thread using Thread#setDaemon().

The JVM only stops if all remaning threads are daemon threads.

BalusCa at 2007-7-29 17:01:07 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

I believe you can override the destroy method from GenericServlet, which is in the inheritance hierarchy of HttpServlet. That will be called when you shutdown Tomcat. Actually, that's not working for me when I try it on one that I have here, but there are some known problems with the servlet I am working with. Try what I am saying, and in the destroy method, make sure that thread shuts down so that the application can stop. Sorry if it's a waste of time, but I think it should work.

jackett_dada at 2007-7-29 17:01:07 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

Hi,

Thanks for the response. I set it as a daemon thread and now it works accordingly.

Thanks,

Jarno

jposthuma at 2007-7-29 17:01:07 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

I've verified that the destroy method is called. It's just that my breakpoint wasn't being hit because they were disabled when I shut down the application.

I'm not sure that setting a thread to daemon is right, but it could be. By implementing destroy, you can explicitly clean up behind yourself by shutting down your thread. This is the approach I would prefer.

Does the daemon thread die when the JVM shuts down? I guess it would, but what if it is in a seperate process running and is still running? I'm not sure how this works. But if it is still running, then there is still a problem, just not one you can detect.

jackett_dada at 2007-7-29 17:01:07 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5

> Does the daemon thread die when the JVM shuts down?

Yes, the JVM should kill all remaining deamon threads on shutdown.

I recall that there was an issue when using daemon threads in applets, but that's not applicable here.

BalusCa at 2007-7-29 17:01:07 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...