Could you please help me in figuring out the following problem.
"I am running a web application, which will constantly monitor a ftp folder and whenever a new file comes on that folder, the application will download it. I have scheduled a thread to run every 15 mins to do this task. The problem, when I undeploy my web application from Tomcat, the scheduled process is not closing. It is still running."
Any clue on how to destroy this scheduled process while undeploying the web application?
If you are just spawning a new thread from inside your web application, Tomcat will not be able to interrupt it when the web app is undeployed. May be you can ask the question in Tomcat forum, they will be able to tell whether you can attach a hook to get a callback on webapp undeployment.
In order to terminate threads you need to stop them from your code. Generally this is done by having the loop in the Thread periodically the value of a boolean, which can be set from outside of the trhead.
Alternatively, you could use the java.util.Timer and TimerTask classes -instead of dealing with Thread or Runnable-; those will be terminated if the web app is shut down.
Veera Sundar I will post this question in Tomcat forum. Thanks for the suggestion. :-)[/QB]
Oops, sorry dont do that. This is not correct according to this javaranch policy. Moderators may do this, if they think that this is not the correct forum for this question.
Ulf: Are you specifying that the Timer should be run as a daemon in the constructor? That's required to be able to shut it down.
Ulf, correct me if i am wrong, marking it as Daemon will make the JVM exit when only daemon threads are running. Does Tomcat keeps an account as to which thread is created by which webapp and interrupt it when that webapp is stopped/undeployed? [ January 11, 2008: Message edited by: Nitesh Kant ]
Non daemon threads will keep the JVM alive until they're done processing which can be problematic in an environment like a web server.
Making them daemon might not be enough to stop them when an application is stopped (but Tomcat is not). You should, as Ulf mentioned have code that is executed when the web application is shutdown that interrupts the thread.
How are you kicking off the thread in Tomcat? Are you using a contextListener?