title: tomcat cannot reload context because of a background thread
body: <p>I have a JSF2 project with Spring. It is developed on eclipse with tomcat attached to it. It is pretty straight forward and mostly with default settings.</p>
  
  <p>But, we have a few background threads that look like this:</p>
  
  <pre><code>public class CrawlingServiceImpl implements CrawlingService, InitializingBean{
      private final Runnable crawlingRunnable = new Runnable() {
          @Override
          public void run() {
              //...
          }
      };
  
      public void startCrawling() {
          crawlingThread = new Thread(crawlingRunnable);
          crawlingThread.start();
      }
  
      public void stopCrawling(){
          if ( crawlingThread!=null )
              crawlingThread.interrupt();
          crawlingThread = null;
      }
  
      @Override
      public void afterPropertiesSet() throws Exception {
          startCrawling();
      }
  
      public void destroy(){
          stopCrawling();
      }
  }
  </code></pre>
  
  <p>Here's who's calling the <code>destroy()</code> method:</p>
  
  <pre><code>&lt;bean
      id="crawlingService"
      class="com.berggi.myjane.service.CrawlingServiceImpl"
      autowire="byName"
      scope="singleton"
      destroy-method="destroy"/&gt;
  </code></pre>
  
  <p>I know that there is a better way all this to be done. But this is not my code and I don't want to rewrite it.</p>
  
  <p>My problem is the following:
  When I change a class (every single time) or when I change an xhtml file (very rarely) the server attempts to reload it, but it fails with the following errors:</p>
  
  <pre><code>INFO: Illegal access: this web application instance has been stopped already.  Could not load org.apache.xml.dtm.ref.DTMManagerDefault.  The eventual following stack trace is caused by an error thrown for debugging purposes as well as to attempt to terminate the thread which caused the illegal access, and has no functional impact.
  java.lang.IllegalStateException
      at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1562)
      at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1521)
      at org.apache.xml.dtm.ObjectFactory.findProviderClass(ObjectFactory.java:508)
      ...
      at package.CrawlingServiceImpl.crawl(CrawlingServiceImpl.java:92)
      at package.CrawlingServiceImpl$1.run(CrawlingServiceImpl.java:39)
      at java.lang.Thread.run(Thread.java:680)
  </code></pre>
  
  <p><strong>Note</strong>: Check the stacktrace. There are a lot of these exceptions.</p>
  
  <p>Then there are more exceptions for a missing jdbc driver which is absolutely fine.</p>
  
  <p>Any ideas?</p>