/** * Interrupt the identified InterruptableJob executing in this Scheduler instance. * * <p> * This method is not cluster aware. That is, it will only interrupt * instances of the identified InterruptableJob currently executing in this * Scheduler instance, not across the entire cluster. * </p> * * @see org.quartz.core.RemotableQuartzScheduler#interrupt(JobKey) */ public boolean interrupt(String fireInstanceId) throws UnableToInterruptJobException { List<JobExecutionContext> jobs = getCurrentlyExecutingJobs(); Job job = null; for(JobExecutionContext jec : jobs) { if (jec.getFireInstanceId().equals(fireInstanceId)) { job = jec.getJobInstance(); if (job instanceof InterruptableJob) { ((InterruptableJob)job).interrupt(); return true; } else { throw new UnableToInterruptJobException( "Job " + jec.getJobDetail().getKey() + " can not be interrupted, since it does not implement " + InterruptableJob.class.getName()); } } } return false; }
@Override public void interrupt() throws UnableToInterruptJobException { log.warn( "Received interrupt request." ); if ( interruptException == null ) { log.warn( "Interrupting job." ); interruptFlag.set( true ); } else { log.warn( "Failing job interrupt with message: {}", interruptException ); throw new UnableToInterruptJobException( interruptException ); } }
@Override protected void executeJob( JobExecutionContext context ) throws JobExecutionException { JobDataMap jobDataMap = context.getMergedJobDataMap(); // optionally set the exception Object quartzdeskInterruptException = jobDataMap.get( JDM_KEY_INTERRUPT_EXCEPTION ); if ( quartzdeskInterruptException != null ) { log.debug( "Interrupt attempts will throw {} with message: {}", UnableToInterruptJobException.class.getName(), quartzdeskInterruptException ); interruptException = quartzdeskInterruptException.toString(); } super.executeJob( context ); }