title: Spring Quartz reschedule job execute twice at the first time
body: <p>I use <strong><a href="https://mvnrepository.com/artifact/quartz/quartz/1.5.2" rel="nofollow noreferrer">Quartz1.5.2</a></strong> and <strong>Spring3.2.1</strong> to do scheduler task, in my application,I need to reschedule the task at sometime,but I found that each time when the task is rescheduled,it will execute twice at the first time.</p>
  
  <p>Below is my <strong>quartz+spring</strong> configuration file:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.springframework.org/schema/beans 
      http://www.springframework.org/schema/beans/spring-beans-4.3.xsd "&gt;
  
      &lt;bean id="testScheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"&gt;
          &lt;property name="startupDelay" value="0"/&gt; 
          &lt;property name="triggers"&gt;
              &lt;list&gt;
                  &lt;ref bean="testJobTrigger" /&gt;
              &lt;/list&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
  
      &lt;bean id="testJob" class="com.lucumt.quartz.JobScheduler"&gt;&lt;/bean&gt;
  
      &lt;bean id="testJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"&gt;
          &lt;property name="targetObject" ref="testJob" /&gt;
          &lt;property name="targetMethod" value="schedulerJob" /&gt;
          &lt;property name="concurrent" value="false"/&gt;
      &lt;/bean&gt;
  
      &lt;bean id="testJobTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean"&gt;
          &lt;property name="jobDetail" ref="testJobDetail" /&gt;
          &lt;property name="cronExpression" value="0/10 * * * * ?" /&gt;
      &lt;/bean&gt;
  
  &lt;/beans&gt;
  </code></pre>
  
  <p>I made it as a web application,below is the code for <strong>web.xml</strong>:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"&gt;
      &lt;display-name&gt;Dynamic Quartz Scheduler&lt;/display-name&gt;
      &lt;listener&gt;
          &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt;
      &lt;/listener&gt;
      &lt;context-param&gt;
          &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
          &lt;param-value&gt;classpath*:spring-context-*.xml&lt;/param-value&gt;
      &lt;/context-param&gt;
  
      &lt;servlet&gt;
          &lt;servlet-name&gt;springmvc&lt;/servlet-name&gt;
          &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt;
          &lt;init-param&gt;
              &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
              &lt;param-value&gt;classpath*:spring-context-*.xml&lt;/param-value&gt;
          &lt;/init-param&gt;
      &lt;/servlet&gt;
  
      &lt;servlet-mapping&gt;
          &lt;servlet-name&gt;springmvc&lt;/servlet-name&gt;
          &lt;url-pattern&gt;/&lt;/url-pattern&gt;
      &lt;/servlet-mapping&gt;
  
      &lt;filter&gt;
          &lt;filter-name&gt;characterEncodingFilter&lt;/filter-name&gt;
          &lt;filter-class&gt;org.springframework.web.filter.CharacterEncodingFilter&lt;/filter-class&gt;
          &lt;init-param&gt;
              &lt;param-name&gt;encoding&lt;/param-name&gt;
              &lt;param-value&gt;UTF-8&lt;/param-value&gt;
          &lt;/init-param&gt;
      &lt;/filter&gt;
  
      &lt;servlet-mapping&gt;
          &lt;servlet-name&gt;default&lt;/servlet-name&gt;
          &lt;url-pattern&gt;*.css&lt;/url-pattern&gt;
      &lt;/servlet-mapping&gt;
      &lt;servlet-mapping&gt;
          &lt;servlet-name&gt;default&lt;/servlet-name&gt;
          &lt;url-pattern&gt;*.js&lt;/url-pattern&gt;
      &lt;/servlet-mapping&gt;
  
  &lt;/web-app&gt;
  </code></pre>
  
  <p>Below it's the java scheduler file,quartz will call <strong>schedulerJob()</strong> at fixed time and we can call <strong>resetJob(String expression)</strong> to reschedule the scheduler execution time.</p>
  
  <pre><code>import java.text.DateFormat;
  import java.text.ParseException;
  import java.text.SimpleDateFormat;
  import java.util.Date;
  
  import javax.servlet.ServletContext;
  
  import org.quartz.Scheduler;
  import org.quartz.SchedulerException;
  import org.springframework.context.ApplicationContext;
  import org.springframework.scheduling.quartz.CronTriggerBean;
  import org.springframework.web.context.ServletContextAware;
  import org.springframework.web.context.support.WebApplicationContextUtils;
  
  public class JobScheduler implements ServletContextAware {
  
      private DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  
      private ServletContext context;
  
      private int count = 0;
      private String type = "10";
  
      @Override
      public void setServletContext(ServletContext context) {
         this.context=context;        
      }
  
      public void schedulerJob() {
          count++;
          System.out.println("=========fixed output: " + df.format(new Date())+"\t"+count);
          if(count%4==0){
              if(type.equals("10")){
                  this.resetJob("0/30 * * * * ?");
              }else if(type.equals("30")){
                  this.resetJob("0/10 * * * * ?");
              }
  
              if(type.equals("10")){
                  type = "30";
              }else{
                  type = "10";
              }
          }
      }
  
      public void resetJob(String expression){
          System.out.println("****************change to:\t" + expression);
          ApplicationContext applicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(context);
          Scheduler scheduler = (Scheduler) applicationContext.getBean("testScheduler");
          try {
              CronTriggerBean trigger = (CronTriggerBean) scheduler.getTrigger("testJobTrigger", Scheduler.DEFAULT_GROUP);
              trigger.setCronExpression(expression);
              scheduler.rescheduleJob("testJobTrigger", Scheduler.DEFAULT_GROUP, trigger);
          } catch (SchedulerException e) {
              e.printStackTrace();
          } catch (ParseException e) {
              e.printStackTrace();
          }
      }
  }
  </code></pre>
  
  <p>Below is the output console in my IDE,as you can see,each time when reschedule it,the job will execute twice at the first time,I guess it may has something to do with the following code,but I don't know how to modify it,can anyone help me?
  Thanks in advance!</p>
  
  <p>PS:If upgrade the version of <strong>Quartz</strong> to <strong>2.3.0</strong> and use <strong>CronTriggerImpl</strong> to implement,this issue will not occur,but due to some reasons,I need to use the version <strong>1.5.2</strong>,I want to solve my problem at this version.</p>
  
  <pre><code>//I think these two lines may cause the problem,but I don't know how to modify it.
  ApplicationContext applicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(context);
  Scheduler scheduler = (Scheduler) applicationContext.getBean("testScheduler");
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/hRo9i.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/hRo9i.png" alt="Reschedule will execute twice at the first time"></a></p>
  
  <p>If I using a new trigger and update the <strong>resetJob</strong> as below,the problem will disappear,but I am wonder why if I use the same trigger,it will execute twice?
  I found both <strong>quartz1.5.2</strong> and <strong>quartz2.3.0</strong> has the same problem if using the same trigger！</p>
  
  <pre><code>public void resetJob(String expression){
      System.out.println("****************change to:\t" + expression);
      ApplicationContext applicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(context);
      Scheduler scheduler = (Scheduler) applicationContext.getBean("testScheduler");
      try {
          //CronTriggerBean trigger = (CronTriggerBean) scheduler.getTrigger("testJobTrigger", Scheduler.DEFAULT_GROUP);
          CronTriggerBean trigger = new CronTriggerBean();
          trigger.setName("testJobTrigger");
          trigger.setGroup(Scheduler.DEFAULT_GROUP);
          trigger.setJobName("testJobDetail");
          trigger.setCronExpression(expression);
          scheduler.rescheduleJob("testJobTrigger", Scheduler.DEFAULT_GROUP, trigger);
      } catch (SchedulerException e) {
          e.printStackTrace();
      } catch (ParseException e) {
          e.printStackTrace();
      }
  }
  </code></pre>