title: Quartz CronScheduler running job multiple times
body: <p>I am using Quartz CronScheduler to execute a job every 15 minutes. Every time the job executes it checks the DB for any change in the cron expression and updates the job scheduler for the next run. I have implemented the above in the following way:</p>
  
  <pre><code>@Service
  public class QuartzSchedulerService {
  
      private static final Logger LOG = LoggerFactory.getLogger(QuartzSchedulerService.class);
  
      private Scheduler           scheduler;
  
      @PostConstruct
      private void init() {
          try {
              scheduler = StdSchedulerFactory.getDefaultScheduler();
              scheduler.start();
          } catch (Exception e) {
              LOG.error("Unable to start quartz scheduler", e);
          }
      }
  
      @PreDestroy
      private void destroy() {
          try {
              scheduler.shutdown();
          } catch (Exception e) {
              LOG.error("Unable to shutdown quartz scheduler", e);
          }
      }
  
      public void registerCronJob(Class&lt;? extends Job&gt; jobClass, String cronExpression) {
          try {
              String jobName = jobClass.getSimpleName();
              CronScheduleBuilder cronBuilder = CronScheduleBuilder.cronSchedule(cronExpression);
              JobDetail jobDetail = JobBuilder.newJob(jobClass).withIdentity(jobName).build();
              CronTrigger cronTrigger = TriggerBuilder.newTrigger().withIdentity(jobName).withSchedule(cronBuilder).build();
              scheduler.scheduleJob(jobDetail, cronTrigger);
              LOG.info("Registered Cron Job:" + jobName + " " + jobDetail.getKey());
          } catch (Exception e) {
              LOG.error("Unable to register cron job", e);
          }
      }
  
      public void updateCronSchedule(Class&lt;? extends Job&gt; jobClass, String cronExpression) {
          try {
              String jobName = jobClass.getSimpleName();
              CronScheduleBuilder cronBuilder = CronScheduleBuilder.cronSchedule(cronExpression);
              CronTrigger newCronTrigger = TriggerBuilder.newTrigger().withIdentity(jobName).withSchedule(cronBuilder).build();
              scheduler.rescheduleJob(TriggerKey.triggerKey(jobName), newCronTrigger);
              LOG.info("Updated Cron Job:" + jobName + " " + newCronTrigger.getJobKey());
              LOG.info("Jobs executed: " + scheduler.getMetaData().getNumberOfJobsExecuted());
          } catch (Exception e) {
              LOG.error("Unable to reschedule cron job", e);
          }
      }
  
  }
  </code></pre>
  
  <p>The class which implements the Job interface is as belows:</p>
  
  <pre><code>@Component
  @DisallowConcurrentExecution
  public class PropertiesReloadJob implements Job {
  
      private static final Logger    LOG = LoggerFactory.getLogger(PropertiesReloadJob.class);
  
      @Autowired
      private QuartzSchedulerService schedulerService;
  
      @Autowired
      private PropertiesService  propertiesService;
  
      public void loadAtStartup() {
          load();
          LOG.info("--- Registerting PropertiesReload Cron Job ---");
          schedulerService.registerCronJob(PropertiesReloadJob.class, propertiesService.getCacheReloadCronExpression());
      }
  
      @Override
      public void execute(JobExecutionContext context) throws JobExecutionException {
          SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
          load();
          LOG.info("--- Updating Pro Cron Job ---");
          schedulerService.updateCronSchedule(PropertiesReloadJob.class, propertiesService.getCacheReloadCronExpression());
      }
  
      public void load(){
            // Load properties from DB
      }
  </code></pre>
  
  <p>The loadAtStartup() method is called during context initializtion and then after every 15 minutes the execute() method is called.</p>
  
  <p>The cron expression used is: <code>0 0/15 * 1/1 * ? *</code></p>
  
  <p>Now, the problem is as follows:
  Lets say that the job starts at 3:00:00, it will execute as many times it can till 3:00:01, rather than executing only once.</p>
  
  <p>Next the job will start at 3:15:00 and again will run as many times it can till 3:15:01.</p>
  
  <p>The number of times the job executes is different every time.</p>
  
  <p>I am not sure what is causing this behaviour. I have tested the cron expression with cronmaker. </p>
  
  <p>Can somebody point out the error here ? </p>