title: Simple example for Quartz 2.2 and Tomcat 7
body: <p>I want to create a scheduler with Quartz 2.2 in java dynamic web application. I am new to this task. I tried all the tutorials around the web. I trying context listener method to initialize the scheduler. It doesn't seem like working. The hello world program only works in general java application. for web application its looks tricky.</p>
  
  <p>pom.xml:</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/maven-v4_0_0.xsd"&gt;
  &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
  &lt;groupId&gt;test.ananth&lt;/groupId&gt;
  &lt;artifactId&gt;test-app&lt;/artifactId&gt;
  &lt;packaging&gt;war&lt;/packaging&gt;
  &lt;version&gt;1.0-SNAPSHOT&lt;/version&gt;
  &lt;name&gt;test-app Maven Webapp&lt;/name&gt;
  &lt;url&gt;http://maven.apache.org&lt;/url&gt;
  &lt;dependencies&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.apache.tomcat&lt;/groupId&gt;
          &lt;artifactId&gt;servlet-api&lt;/artifactId&gt;
          &lt;version&gt;6.0.30&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;log4j&lt;/groupId&gt;
          &lt;artifactId&gt;log4j&lt;/artifactId&gt;
          &lt;version&gt;1.2.16&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.0&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;org.slf4j&lt;/groupId&gt;
          &lt;artifactId&gt;slf4j-api&lt;/artifactId&gt;
          &lt;version&gt;1.6.6&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;junit&lt;/groupId&gt;
          &lt;artifactId&gt;junit&lt;/artifactId&gt;
          &lt;version&gt;3.8.1&lt;/version&gt;
          &lt;scope&gt;test&lt;/scope&gt;
      &lt;/dependency&gt;
  &lt;/dependencies&gt;
  &lt;build&gt;
      &lt;finalName&gt;test-app&lt;/finalName&gt;
  &lt;/build&gt;
  </code></pre>
  
  <p></p>
  
  <p>quartz.properties:</p>
  
  <pre><code>#org.quartz.scheduler.instanceName = MyScheduler
  org.quartz.threadPool.threadCount = 3
  org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore
  
  # Main Quartz configuration
  org.quartz.scheduler.skipUpdateCheck = true
  org.quartz.scheduler.instanceName = MyQuartzScheduler
  org.quartz.scheduler.jobFactory.class = org.quartz.simpl.SimpleJobFactory
  org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool
  #org.quartz.threadPool.threadCount = 5
  </code></pre>
  
  <p>HelloJob.java:</p>
  
  <pre><code>    package com.test;
  
      import org.quartz.Job;
      import org.quartz.JobExecutionContext;
      import org.quartz.JobExecutionException;
  
      public class HelloJob implements Job {
  
  public HelloJob() {
  }
  
  public void execute(JobExecutionContext context)
          throws JobExecutionException {
  
      System.out.println("Hello!  HelloJob is executing.");
  }
      }
  </code></pre>
  
  <p>servlet.java
          package com.test;</p>
  
  <pre><code>import static org.quartz.JobBuilder.newJob;
  import static org.quartz.SimpleScheduleBuilder.simpleSchedule;
  import static org.quartz.TriggerBuilder.newTrigger;
  
  import javax.servlet.ServletConfig;
  import javax.servlet.ServletContext;
  import javax.servlet.http.HttpServlet;
  
  import org.quartz.JobDetail;
  import org.quartz.Scheduler;
  import org.quartz.SchedulerException;
  import org.quartz.Trigger;
  import org.quartz.impl.StdSchedulerFactory;
  
  public class MyServlet extends HttpServlet {
  
      /**
       * 
       */
      private static final long serialVersionUID = 1567185871113714035L;
  
      public void init(ServletConfig cfg) {
          String key = "org.quartz.impl.StdSchedulerFactory.KEY";
          ServletContext servletContext = cfg.getServletContext();
          StdSchedulerFactory factory = (StdSchedulerFactory) servletContext
                  .getAttribute(key);
          // Scheduler quartzScheduler = factory.getScheduler("MyQuartzScheduler");
          Scheduler sched;
          try {
              sched = factory.getScheduler("MyQuartzScheduler");
              //sched = factory.getScheduler();//MyQuartzScheduler
              sched.start();
  
              // define the job and tie it to our HelloJob class
              JobDetail job = newJob(HelloJob.class).withIdentity("myJob",
                      "group1").build();
  
              // Trigger the job to run now, and then every 40 seconds
              Trigger trigger = newTrigger()
                      .withIdentity("myTrigger", "group1")
                      .startNow()
                      .withSchedule(
                              simpleSchedule().withIntervalInSeconds(4)
                                      .repeatForever()).build();
  
              // Tell quartz to schedule the job using our trigger
              sched.scheduleJob(job, trigger);
          } catch (SchedulerException e) {
              e.printStackTrace();
          }
  
      }
  }
  </code></pre>
  
  <p>Web.xml:</p>
  
  <pre><code>&lt;!DOCTYPE web-app PUBLIC
   "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
   "http://java.sun.com/dtd/web-app_2_3.dtd" &gt;
  
  &lt;web-app&gt;
      &lt;context-param&gt;
          &lt;context-param&gt;
              &lt;param-name&gt;quartz:shutdown-on-unload&lt;/param-name&gt;
              &lt;param-value&gt;false&lt;/param-value&gt;
          &lt;/context-param&gt;
          &lt;context-param&gt;
              &lt;param-name&gt;quartz:wait-on-shutdown&lt;/param-name&gt;
              &lt;param-value&gt;true&lt;/param-value&gt;
          &lt;/context-param&gt;
          &lt;context-param&gt;
              &lt;param-name&gt;quartz:start-scheduler-on-load&lt;/param-name&gt;
              &lt;param-value&gt;true&lt;/param-value&gt;
          &lt;/context-param&gt;
          &lt;context-param&gt;
              &lt;param-name&gt;quartz:config-file&lt;/param-name&gt;
              &lt;param-value&gt;/WEB-INF/quartz.properties&lt;/param-value&gt;
          &lt;/context-param&gt;
  
          &lt;listener&gt;
              &lt;listener-class&gt;
                  org.quartz.ee.servlet.QuartzInitializerListener
              &lt;/listener-class&gt;
          &lt;/listener&gt;
  
          &lt;listener&gt;
              &lt;listener-class&gt;com.test.ApplicationStartup&lt;/listener-class&gt;
          &lt;/listener&gt;
          &lt;display-name&gt;Archetype Created Web Application&lt;/display-name&gt;
  &lt;/web-app&gt;
  </code></pre>
  
  <p>I am using maven web app archtype.</p>