title: Quartz scheduler with tomcat 8 and spring mvc creates multiple threads and fails to stop when tomcat is stopped
body: <p>I have a java spring mvc web application which use Spring 3.2 and Tomcat 8. I use quartz 2.2 to schedule a task. My controller class is:</p>
  
  <pre><code>@Controller
  public class StatusController  implements Job
  {
      @Autowired
      WebContentDefinitionService webContentDefinitionService;
  
  
      public void execute(JobExecutionContext arg0) throws JobExecutionException 
      {
           SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
           System.out.println("Starting Job");
          try 
          {
              webContentDefinitionService.deletePurgedContents();
              webContentDefinitionService.moveContentsToPurged();
  
          }
          catch (Exception e) 
          {
              e.printStackTrace();
          }
           System.out.println("Executed Job");
  
      }
  
  }
  </code></pre>
  
  <p>and I have a QuartzInitializer class as follows:</p>
  
  <pre><code>import javax.servlet.ServletContext;
  import javax.servlet.ServletContextEvent;
  import javax.servlet.annotation.WebListener;
  
  import org.quartz.CronScheduleBuilder;
  import org.quartz.JobBuilder;
  import org.quartz.JobDetail;
  import org.quartz.Scheduler;
  import org.quartz.Trigger;
  import org.quartz.TriggerBuilder;
  import org.quartz.ee.servlet.QuartzInitializerListener;
  import org.quartz.impl.StdSchedulerFactory;
  
  @WebListener
  public class QuartzListener extends QuartzInitializerListener {
  
      @Override
      public void contextInitialized(ServletContextEvent sce) {
          super.contextInitialized(sce);
          ServletContext ctx = sce.getServletContext();
          StdSchedulerFactory factory = (StdSchedulerFactory) ctx.getAttribute(QUARTZ_FACTORY_KEY);
          try {
              Scheduler scheduler = factory.getScheduler();
              JobDetail jobDetail = JobBuilder.newJob(StatusController.class).build();
              Trigger trigger = TriggerBuilder.newTrigger().withIdentity("simple").withSchedule(
                      CronScheduleBuilder.cronSchedule("0 0/5 * * * ?")).startNow().build();
              scheduler.scheduleJob(jobDetail, trigger);
              scheduler.start();
          } catch (Exception e) {
              ctx.log("There was an error scheduling the job.", e);
          }
      }
  
  }
  </code></pre>
  
  <p>My porm.xml file is:</p>
  
  <pre><code>&lt;project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"&gt;
      &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
  
      &lt;groupId&gt;test&lt;/groupId&gt;
      &lt;artifactId&gt;BasicQuartz&lt;/artifactId&gt;
      &lt;packaging&gt;war&lt;/packaging&gt;
      &lt;version&gt;1.0-SNAPSHOT&lt;/version&gt;
  
      &lt;name&gt;BasicQuartz&lt;/name&gt;
      &lt;url&gt;http://maven.apache.org&lt;/url&gt;
  
      &lt;dependencies&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;javax.servlet&lt;/groupId&gt;
              &lt;artifactId&gt;servlet-api&lt;/artifactId&gt;
              &lt;version&gt;2.5&lt;/version&gt;
              &lt;scope&gt;provided&lt;/scope&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.quartz-scheduler&lt;/groupId&gt;
              &lt;artifactId&gt;quartz&lt;/artifactId&gt;
              &lt;version&gt;2.2.1&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.quartz-scheduler&lt;/groupId&gt;
              &lt;artifactId&gt;quartz-jobs&lt;/artifactId&gt;
              &lt;version&gt;2.2.1&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;javax.transaction&lt;/groupId&gt;
              &lt;artifactId&gt;jta&lt;/artifactId&gt;
              &lt;version&gt;1.1&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.slf4j&lt;/groupId&gt;
              &lt;artifactId&gt;slf4j-log4j12&lt;/artifactId&gt;
              &lt;version&gt;1.7.5&lt;/version&gt;
          &lt;/dependency&gt;
      &lt;/dependencies&gt;
  
      &lt;build&gt;
          &lt;plugins&gt;
              &lt;plugin&gt;
                  &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
                  &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
                  &lt;version&gt;2.0.2&lt;/version&gt;
                  &lt;configuration&gt;
                      &lt;source&gt;1.5&lt;/source&gt;
                      &lt;target&gt;1.5&lt;/target&gt;
                  &lt;/configuration&gt;
              &lt;/plugin&gt;
          &lt;/plugins&gt;
      &lt;/build&gt;
  &lt;/project&gt;
  </code></pre>
  
  <p>Now everything works fine for me until I try to stop my tomcat server. I get error telling that there are multiple threads started by the quartz initializer, but unable to stop them and this may lead to memory leak.
  I found lot of similar instances, but could not find a solution that can be applied in my case since I am not using my <code>web.xml</code> file to initialize the quartz scheduler. The error I get on my console is as follows:</p>
  
  <pre><code>   Aug 14, 2015 6:25:43 PM org.apache.catalina.core.ApplicationContext log
  INFO: Closing Spring root WebApplicationContext
  Aug 14, 2015 6:25:43 PM org.apache.catalina.loader.WebappClassLoaderBase clearReferencesJdbc
  WARNING: The web application [smartwcm-admin] registered the JDBC driver [org.postgresql.Driver] but failed to unregister it when the web application was stopped. To prevent a memory leak, the JDBC Driver has been forcibly unregistered.
  Aug 14, 2015 6:25:43 PM org.apache.catalina.loader.WebappClassLoaderBase clearReferencesThreads
  WARNING: The web application [smartwcm-admin] appears to have started a thread named [DefaultQuartzScheduler_Worker-1] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread:
  java.lang.Object.wait(Native Method)
  org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:568)
  Aug 14, 2015 6:25:43 PM org.apache.catalina.loader.WebappClassLoaderBase clearReferencesThreads
  WARNING: The web application [smartwcm-admin] appears to have started a thread named [DefaultQuartzScheduler_Worker-2] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread:
  java.lang.Object.wait(Native Method)
  </code></pre>