• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Servlet Running in Infinite loop

 
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why are you running an infinite loop from a servlet?
 
visu Nekk
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How are you stopping the thread?
 
Bartender
Posts: 1638
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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?
 
visu Nekk
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1638
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Nitesh,

Any suggestions on how to exit the loop?

Visu
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic