File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Threads and Synchronization and the fly likes Servlet Running in Infinite loop Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Servlet Running in Infinite loop" Watch "Servlet Running in Infinite loop" New topic
Author

Servlet Running in Infinite loop

visu Nekk
Ranch Hand

Joined: Sep 06, 2005
Posts: 122
Hi,

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?

Thanks in advance,
Visu
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Why are you running an infinite loop from a servlet?


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
visu Nekk
Ranch Hand

Joined: Sep 06, 2005
Posts: 122
Hi,

The servlet is supposed to talk to a web service, get the data, and insert the data into the database. This has to happen round the clock.

Visu
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

A servlet is a poor choice for this type of functionality.
Servlets are meant to respond to requests.

It would be better to put this logic in a plain old Java object (POJO) that runs in it's own thread and instanciate this object from a context listener.

Make sure to read up on the daemon property of the thread class. http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Thread.html#setDaemon(boolean).
Understanding that property will help you to insure that your application can be shut down or restarted independently of the rest of the apps running under your instance of Tomcat.
visu Nekk
Ranch Hand

Joined: Sep 06, 2005
Posts: 122
Thanks Ben,

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.

Visu
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42929
    
  68
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.
visu Nekk
Ranch Hand

Joined: Sep 06, 2005
Posts: 122
Thanks Ulf,

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();

Visu
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42929
    
  68
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.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

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.
visu Nekk
Ranch Hand

Joined: Sep 06, 2005
Posts: 122
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?

Visu
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42929
    
  68
How are you stopping the thread?
Nitesh Kant
Bartender

Joined: Feb 25, 2007
Posts: 1638


Even after I have stopped the thread

How did you manage to stop the thread? Do you mean that the thread has finished execution and has came out of the run() method?


apigee, a better way to API!
visu Nekk
Ranch Hand

Joined: Sep 06, 2005
Posts: 122
private boolean stillRunning;

public void run()
{
this.stillRunning = true;

while(this.stillRunning)
{
System.out.println("Hai from runnable");

}
}

public void pleaseStop()
{
this.stillRunning = false;
}

I am calling the pleaseStop method from another class.

Thanks,
Visu
Nitesh Kant
Bartender

Joined: Feb 25, 2007
Posts: 1638

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.
visu Nekk
Ranch Hand

Joined: Sep 06, 2005
Posts: 122
Thanks Nitesh,

Any suggestions on how to exit the loop?

Visu
Nitesh Kant
Bartender

Joined: Feb 25, 2007
Posts: 1638

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Servlet Running in Infinite loop