title: Shutdown Thread Pool when application stops or server shutsdown
body: <p>I have a thread-pool in my application which I want to shutdown only when application stops. I tried 3 ways:</p>
  
  <p>1.Shutting down in context destroyed:</p>
  
  <pre><code>@Override
  public void contextDestroyed(ServletContextEvent sce) {
      System.out.println("CALLING CONTEXT DESTROYED.");
      PersistSigningExecutor.getService().shutdown();
  }
  </code></pre>
  
  <p>2.Shutting down in destroy method of any servlet:</p>
  
  <pre><code>@Override
  public void destroy() {   
      super.destroy();
      System.out.println("SHUTTING DOWN THREAD-POOLS");  
      PersistSigningExecutor.getService().shutdown();
  }  
  </code></pre>
  
  <p>3.Adding shutdown hook in contextInitialised</p>
  
  <pre><code>@Override
  public void contextInitialized(ServletContextEvent sce) {       
          Runtime.getRuntime().addShutdownHook(new Thread() {
              @Override
              public void run() {
                  try {
                      System.out.println("SHUTTING DOWN THREAD-POOLS");                        
                      PersistSigningExecutor.getService().shutdown();
  
              }
          });
  
  }
  </code></pre>
  
  <p>But none of them are working. I am not seeing any print statements. And also at the end I am getting log saying : </p>
  
  <pre><code>SEVERE: The web application [/app] appears to have started a thread named [pool-1-thread-1] but has failed to stop it. This is very likely to create a memory leak.
  </code></pre>
  
  <p>which means the thread is still not shutdown. How to shutdown this thread-pool properly when server is shutdown or application is undeployed?</p>