-1
 I called the MyListener class from the web.xml file
<listener>  
  <listener-class>MyListener</listener-class>  
</listener>

import javax.servlet.*;  

public class MyListener implements ServletContextListener{  
  public void contextInitialized(ServletContextEvent event) {  
    try{  
        (new Thread(new SampleProcessor())).start();
    }catch(Exception e){e.printStackTrace();}  
  }  

  public void contextDestroyed(ServletContextEvent arg0) {}  
}  

public class SampleProcessor implements Runnable{

 public void run(){
 //Here we write the code for listening to a JMS Topic
}

} I have another listener similar to above which is listening to another JMS Topic. When I stop the server I am getting the following errors on Server Error 1: "The web application [/MyServlet] appears to have started a thread named [thread-1] but has failed to stop it. This is very likely to create a memory leak." Error 2: "The web application [/MyServlet] appears to have started a thread named [thread-2] but has failed to stop it. This is very likely to create a memory leak."

Why is the error happening and how we can stop the threads or how can we fix it?

john
  • 5
  • 1
  • 5

1 Answers1

0

I think you'll just need to cleanly shutdown the thread.

Implement the method contextDestroyed(ServletContextEvent sce), and send a signal to the thread to terminate.

How to cleanly stop a thread:

How to properly stop the Thread in Java?

Otherwise, the thread is forcefully killed.

Here's a rough example (haven't compiled):

public class MyListener implements ServletContextListener{  
  static Thread mythread = null;
  public void contextInitialized(ServletContextEvent event) {  
    try{  
        if ( null == mythread ) {
           mythread = new Thread(new SampleProcessor()));
           mytrhread.start();
       }
    }catch(Exception e){e.printStackTrace();}  
  }  

  public void contextDestroyed(ServletContextEvent arg0) {

       // your stop method here ...
       try{  
        if ( null != mythread ) {
           mythread.cleanStop();
           mytrhread.join();
       }
    }catch(Exception e){e.printStackTrace();}  
   }  
}  

public class SampleProcessor implements Runnable{

 public boolean running = false;
 public void run(){
 //Here we write the code for listening to a JMS Topic
   running = true; 
  }

 public void cleanStop(){
     //handle stopping the process here
      running = false;  // your main thread loop should test this var 
  }
}
Community
  • 1
  • 1
Kevin Seifert
  • 3,494
  • 1
  • 18
  • 14
  • Kevin seifret Can you make the changes in the above code to fix the problem? – john Aug 21 '15 at 02:06
  • if (running == false) { // your thread function should exit } – Kevin Seifert Aug 24 '15 at 18:07
  • .I think it will looks like this public boolean running = true; public void run(){ if(running){ //Here we write the code for listening to a JMS Topic } – john Aug 24 '15 at 18:25
  • something like that should work. I'm assuming there's something like a `while` loop inside the run() method that does whatever logic is needed to process in the thread. Check `if (running == true)` on each iteration of the loop. If it's not `true` then return from the run() function -- the thread completes cleanly. – Kevin Seifert Aug 24 '15 at 18:25
  • iow: `if(! running){ return; }` //inside the thread procesing logic – Kevin Seifert Aug 24 '15 at 18:28
  • is it no need to call interrupt() or join() method to stop the thread – john Aug 24 '15 at 18:28
  • no, you shouldn't need to call interupt or join AFAIK. The thread will detect that it needs to stop (from `running`), and will exit the process on its own. IMO it's better to let the thread detect and handle stopping, because you can (more cleanly) release any resources that have been created. – Kevin Seifert Aug 24 '15 at 18:30
  • It works only when the Thread.join() is called in contextDestroyed() after calling mythread.cleanStop().Without calling Thread.join() method the thread wont be stopped. – john Aug 25 '15 at 12:59
  • Ah, that makes sense. That will give the thread time to close down. Otherwise the main thread is closing before the child thread has time to finish shutting down. – Kevin Seifert Aug 25 '15 at 14:11
  • Thanks @Kevin Selfert for your help – john Aug 25 '15 at 14:27
  • In the above code "mythread" thread creates other child threads and I am able to kill the "mythread" thread but the child threads are not killed.How can we kill the child threads also – john Aug 26 '15 at 14:02
  • It can follow the same model. In each child thread, include a `boolean running `, where the child threads check this value and exit if false. The only difference, in the parent thread, I might include an collection like ArrayList or HashMap that tracks all threads that the parent spawns. That way, you can loop through the list afterwards and send them a shut down message. – Kevin Seifert Aug 26 '15 at 15:29
  • I am not creating any child threads.The "mythread" main thread creates them. – john Aug 26 '15 at 16:16
  • That shouldn't matter unless it's a third party API, or if the source code is not available. If this is the case, the only route may be to look at the API itself to see what methods it provides for shutting down the threads and releasing resources. Internally, an API will manage the threads similar to how you are doing it with mythread. This cleanup code would go prior to the `return` that exits mythread. – Kevin Seifert Aug 26 '15 at 16:31