I have some quartz jobs to process some data. I used InterruptableJob interface when writing these jobs. I have to add an interrupt feature to these jobs. When user press the terminate button, this method calling:
@Override
protected void immediatelyTerminate() {
try {
String fireInstanceIdToKillJob = "";
Scheduler scheduler = schedulerFactory.getScheduler();
List<JobExecutionContext> currentlyExecutingJobs = scheduler.getCurrentlyExecutingJobs();
for(JobExecutionContext jec : currentlyExecutingJobs) {
if(jec.getJobDetail().getKey().getName().contains(specificJobKey) {
fireInstanceIdToKillJob = jec.getFireInstanceId();
}
}
scheduler.interrupt(fireInstanceIdToKillJob);
} catch (Exception e) {
logger.debug("error in immediatelyTerminate() method:" + e);
}
}
scheduler.interrupt calls the interrupt() method that is overridden in a class and this class has also execute method. The class that has overridden interrupt method is like this:
private Thread threadToKill;
@Override
public void execute() {
threadToKill = Thread.currentThread();
}
@Override
public void interrupt() throws UnableToInterruptJobException {
try {
threadToKill.interrupt();
} catch (Exception e) {
System.out.println("Exception handled "+e);
}
}
But with this code the job doesn't terminate, it continues to working. threadToKill.interrupt(); line doesn't actually terminate the running job.
How can I terminate runnig job?