title: How to stop execution of particular quartz job when it is running?
body: <p>I am implementing the <strong>quartz job</strong> scheduling by code. Below is my method to schedule and reschedule the quartz job. </p>
  
  <pre><code>void setJobSchedular(ScraperSearchOnRequest scraperSearchOnRequestInstance) {
          try {
              String groupName = "Scraper Group" + scraperSearchOnRequestInstance?.id
              String jobName = "ScraperOnRequestJob"
              Scheduler scheduler = getJobManagerService().quartzScheduler
  
              JobKey jobKey = new JobKey(jobName, groupName);
              //create JobDetail and Trigger
              JobDetail jobDetail = JobBuilder.newJob(com.dogmasystems.scraper.ScraperOnRequestJob1.class).withIdentity(jobKey).build();
  
              //add passing parameters to JobDataMap for first JobDetail
              jobDetail.getJobDataMap().put("id", scraperSearchOnRequestInstance?.id?.toString());
              jobDetail.getJobDataMap().put("groupName", groupName);
  
              if (scheduler.checkExists(jobKey)) {
                  println("Rescheduling the old job having group name " + groupName)
                  Trigger oldTrigger = scheduler.getTrigger(new TriggerKey(jobName, groupName))
                  TriggerBuilder tb = oldTrigger.getTriggerBuilder();
                  Trigger newTrigger = tb.startAt(scraperSearchOnRequestInstance.scheduledTime).usingJobData("id",
                          scraperSearchOnRequestInstance?.id?.toString()).build()
                  scheduler.rescheduleJob(oldTrigger.key, newTrigger);
              } else {
                  println("Scheduling the new job having group name " + groupName)
                  SimpleTrigger trigger = TriggerBuilder.newTrigger().withIdentity(jobName, groupName)
                          .startAt(scraperSearchOnRequestInstance.scheduledTime).usingJobData("id",
                          scraperSearchOnRequestInstance?.id?.toString()).build();
                  scheduler.scheduleJob(jobDetail, trigger);
              }
          } catch (Exception e) {
              println("**********************Error in setJobSchedular for scraperOnRequestJob****************************")
              e.printStackTrace("Error while setting ScraperOnRequestJob " + e.printStackTrace())
              println("******************************************************")
          }
      }
  </code></pre>
  
  <p>Basically my job is running at specified time and it is working fine using the above method.</p>
  
  <p><strong>Requirement:</strong></p>
  
  <ol>
  <li>I want to stop my running job execution by clicking the <strong>STOP</strong> button at UI using the ajax call.</li>
  </ol>
  
  <p><a href="https://i.stack.imgur.com/YSjGv.png" rel="noreferrer"><img src="https://i.stack.imgur.com/YSjGv.png" alt="enter image description here"></a></p>
  
  <p><strong>For stopping the job</strong> i am giving <strong>jobName</strong> and <strong>groupName</strong> of that particular job.</p>
  
  <pre><code>void stopSchedularJob(String id) throws Exception {
          String groupName = "Scraper Group" + id
          String jobName = "ScraperOnRequestJob"
          JobKey jobKey = new JobKey(jobName, groupName);
          TriggerKey triggerKey = TriggerKey.triggerKey(jobName, groupName);
          Scheduler scheduler = getJobManagerService().quartzScheduler
          Trigger trigger = scheduler.getTrigger(triggerKey)
          if (trigger) {
              println "Trigger key name to stop " + triggerKey.getName()
              // wait long enough to see the job execution
              Thread.sleep(10 * 1000); //1 minute
              scheduler.unscheduleJob(triggerKey)
              println "The ScraperOnRequestJob having " + groupName + " has been stopped."
          } else {
              println "No trigger could be found for " + triggerKey.getName()
          }
  
      }
  </code></pre>
  
  <p><strong>But it is giving me error:</strong></p>
  
  <pre><code>Error |
  2019-11-21 11:20:10,411 [http-bio-8078-exec-2] ERROR spi.SqlExceptionHelper  - ERROR: canceling statement due to user request
    Where: while updating tuple (0,36) in relation "scraper_search"
  Error |
  org.springframework.dao.DataAccessResourceFailureException: Hibernate operation: could not execute statement; SQL [n/a]; ERROR: canceling statement due to user request
    Where: while updating tuple (0,36) in relation "scraper_search"; nested exception is org.postgresql.util.PSQLException: ERROR: canceling statement due to user request
    Where: while updating tuple (0,36) in relation "scraper_search"
  </code></pre>
  
  <p>I tried many solutions like</p>
  
  <ol>
  <li><p><code>scheduler.interrupt(jobKey)</code></p></li>
  <li><p><code>scheduler.shutdown()</code> (I only want to kill only particular job, not all the jobs)</p></li>
  <li><p>Quartz Job Listeners</p></li>
  </ol>
  
  <p>But still i am not able to find the particular solution. </p>