title: Why is Quartz Scheduler not checking existence of JobDetail properly?
body: <p>I have the following method that builds a new <code>JobDetail</code> instance;</p>
  
  <pre><code>    private JobDetail getJob(JobID jobID) throws SchedulerException {
          Class&lt;? extends Job&gt; jobClass = jobID.getJobClass();
          if(jobClass != null){
              return JobBuilder
                      .newJob(jobClass)
                      .withIdentity(jobID.jobName(), jobID.jobGroup())
                      .requestRecovery(true)
                      .build();
          }
          return null;
      }
  </code></pre>
  
  <p>Note: <code>JobID</code> is just a helper class of mine that provides all the necessary raw data required to build a <code>JobDetail</code></p>
  
  <p>Then another method that does the actual scheduling;</p>
  
  <pre><code>    doSchedule(Scheduler scheduler, JobDetail job, String triggerName){
  
          Trigger trigger = buildSomeTrigger(triggerName);
  
          //check whether job exists in scheduler instance
          if(scheduler.checkExists(job.getKey())){
  
              //If trigger also exists, then it probably holds new info
              //So, reschedule the existing job with the trigger
              if(scheduler.checkExists(trigger.getKey())){
                  System.out.println("update job with Trigger");
  
                  job = job.getJobBuilder().storeDurably(true).build();
                  scheduler.addJob(job, true);
                  Trigger oldTrigger = scheduler.getTrigger(trigger.getKey());
                  scheduler.rescheduleJob(oldTrigger.getKey(), trigger);
              }else{
  
                  //If trigger does not exist, it's an entirely new trigger
                  //Add the new trigger to the existing job
                  System.out.println("save new trigger for job");
  
                  trigger = trigger.getTriggerBuilder().forJob(job).build();
                  scheduler.scheduleJob(trigger);
              }
          }else{
  
              //If job does not exist, schedule new job with the trigger
              System.out.println("schedule new job with trigger");
  
              scheduler.scheduleJob(job, trigger);
          }
      }
  </code></pre>
  
  <p><strong>EDIT:</strong> Here's what <strong><code>getNewSchedulerInstance()</code></strong> looks like;</p>
  
  <pre><code>    private Scheduler getNewSchedulerInstance() {
          Scheduler scheduler = null;
          try {
              scheduler = new StdSchedulerFactory("quartz.properties").getScheduler();
              scheduler.setJobFactory(cdiJobFactory); //&lt;== Utilizes the JobFactory implementation specified in the CdiJobFactory.java class
              scheduler.getListenerManager().addJobListener(new SimpleJobListener());
              scheduler.getListenerManager().addTriggerListener(new SimpleTriggerListener());
          } catch (SchedulerException ex) {
              System.out.println("Scheduler instantiation failed!");
              ex.printStackTrace();
          }
          return scheduler;
      }
  </code></pre>
  
  <p>Then finally in another method, I have the following;</p>
  
  <pre><code>    //This returns a new instance of Scheduler
      Scheduler scheduler = getNewSchedulerInstance();
  
      if(scheduler != null){
          JobID jobID = new ExtendedJobID(); // &lt;== extends JobID
  
          JobDetail job = getJob(jobID);
          doSchedule(scheduler, job);
      }
  </code></pre>
  
  <p><strong><em>Normally I expect this to print;</em></strong> </p>
  
  <blockquote>
    <p>schedule new job with trigger</p>
  </blockquote>
  
  <p><strong><em>But instead it's printing;</em></strong> </p>
  
  <blockquote>
    <p>save new trigger for job</p>
  </blockquote>
  
  <p><strong>EDIT:</strong> And hence throws the following exception;</p>
  
  <pre><code>org.quartz.JobPersistenceException: Couldn't store trigger 'GOALS.ACTIVATE_GOALS_0:0' for 'GOALS.ACTIVATE_GOALS' job:
  The job (GOALS.ACTIVATE_GOALS) referenced by the trigger does not exist. 
  [See nested exception: org.quartz.JobPersistenceException: The job (GOALS.ACTIVATE_GOALS) referenced by the trigger does not exist.]
  </code></pre>
  
  <ul>
  <li>This suggests that the <code>scheduler.checkExists(job.getKey())</code> returns <code>true</code> </li>
  <li><code>scheduler.checkExists(trigger.getKey())</code> returns <code>false</code></li>
  <li>Yet exception says that <code>The job (*jobName*) referenced by the (*trigger*) does not exist</code></li>
  </ul>
  
  <p>So, this issuse only gets me confused as it seems that <code>scheduler.checkExists(...)</code> is failing to appropriately check the existence of the job or not in the scheduler</p>
  
  <p>The Exception shouldn't even get thrown if the check did what I expect it to do;</p>
  
  <p>But as you can see, the <code>JobDetail</code> is newly built and has not in any way been previously associated with the scheduler... </p>
  
  <ul>
  <li><p>So I'm wondering whether i'm missing some Quartz Scheduler points?... </p></li>
  <li><p>If so, can someone please explain what I might be doing wrong here?... </p></li>
  <li><p>Or could this actually be a bug?... </p></li>
  <li><p>Are there any work-arounds?</p></li>
  </ul>