I have a web application, which has a servlet running in infinite loop in Tomcat. From the forums, it is suggested that in order to terminate the servlet running in infinite loop, we have to shutdown the server. If we shutdown the server, other web applications will also be removed from service. Can anyone suggest how to pull out the servlet from service without affecting the remaining web applications?
I have implemented the business logic in a separate Java class and have instantiated the object from the context listener. This is resulting in the infinite loop, which is not allowing the server to startup completely. Any suggestions please.
That sounds as if you are not creating a new thread to run this code, but instead use the current thread. Even better than just starting a new thread would be to schedule this functionality periodically by using the java.util.Timer and TimerTask classes.
Joined: Sep 06, 2005
I implemented the process in a new thread and it worked. Now, how to stop that particular thread.
ThreadListener listener = new ThreadListener(); Thread t = new Thread(listener); t.setName("SimpleThread"); t.setDaemon(true); t.start();
Joined: Mar 22, 2005
You should introduce a boolean variable, which is set from outside the thread. The thread should then check that variable every so often, and if it is set, it should quit.
Note that threads started this way aren't necessarily terminated if the web app is stopped and restarted (as ooposed to the server being stopped and restarted). It would be better to use the Timer approach mentioned above, which doesn't have this problem.
Ulf's advice sounds good, Do yourself a favor and follow it. In either case, if you're running this within a servlet container, make sure to set the daemon property correctly or the container will not be able to stop the thread on shutdowns or restarts.
It sounds like most of your issues are now thread related and not Tomcat related so I'm going to move this topic to our threads forum.
Joined: Sep 06, 2005
Thanks Ben and Ulf,
I am succesful in implementing what you have said. Now, to figure out the status of the current thread, I used Thread.isAlive(). Even after I have stopped the thread, I am still getting a positive response that the thread is still running. How do I know the status of the thread;i.e. whether it is stopped or running?
I hope the code you have posted is just an indication of what you are using and not the actual code. As, i think you will have some business logic inside the while loop. So, it does not guarantee that as soon as you call your pleaseStop() method, the thread is stopped i.e. exist the run() method. It may still be executing code inside the while loop. isAlive() will return false, only when the thread comes out of the run() method.
Probably, i would need to know as to what is that you want to achieve. Then i will be in a situation to help you. Also, you can use Thread.join() to wait for the some thread to die before you execute further.