Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Jpoller Directory Listener thread is not stopping  RSS feed

 
abd el haleem salah
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have a problem in shuting down the Jpoller(http://jpoller.sourceforge.net/) i don't know what is the problem i want to start it from a servlet and close it from servlet
the problem is tht the thread is started well but i cannot stop the Jpoller thread from inside servlet

and this code for stopping the Jpoller

for(DirectoryPoller poller:pollerList)
{
Thread.sleep(1000L);
System.out.flush();
System.out.println("Shutting down...");
System.out.flush();
poller.shutdown();
long start=System.currentTimeMillis();
System.out.flush();
System.out.println("Waiting for termination...");
System.out.flush();
while(poller.isAlive());

Thread.sleep(5000L);


System.out.println("Thread is stopped ");

}
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess the first question is, how do you know the JPoller thread is not stopped? The code you provide gives us no context at all. Where is this code stored (what methods)? When does it get executed? What output do you get? Have you looked through your logs to see if you get exceptions?

A few critiques:



This closed while loop that does nothing but continuously cycle can be a bad idea - especially for a web server. You could eat a lot of CPU time doing that, more so if you have some sort of error. My suggestion is to add some delay inside the while loop to give more time for other things to happen. Also inside the while loop you could keep track of how long the 'isAlive()' checks are taking which might allow you to detect problems and if the JPoller API allows it, perhaps use a more aggressive approach to shutting the thing down if it takes too long.

Also, I don't really like having the JPoller thread's life cycle being controlled by a Servlet, since you don't really have control of the Servlet's life cycle. You could have multiple servlets running, or a servlet could be started, killed, and restarted by the servlet engine without your intervention. This could lead to problems with the app. I could be wrong since I don't know what JPoller does or what your application is. But if the JPoller thread is supposed to be long lived (more than one request) make sure you store it someplace safe where its life cycle isn't affected by that of the Servlet (for example store the JPoller in the ServletContext). And if you want to make sure the JPoller starts when the server does and stops when the server shuts down, then ServletContextListeners are the best place to control it, not a Servlet.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!