I have some tasks that are executed with the help of Java Quartz Jobs, but I need to stop some tasks by some condition in my code. I read that this can be done via InterruptableJob. But i didn't understand in what way i should do it?
deleteJob(jobKey(<JobKey>, <JobGroup>)); This method will only interrupt/stop the job uniquely identified by the Job Key and Group within the scheduler which may have many other jobs running.
Just delete your scheduler object after schedulerBean. Shutdown() and call your scheduler method again as you did it first time.
As a first step create a web application and keep all the Quartz Scheduler dependent jars in classpath. Put the quartz. properties and jobs XML file under classes folder. The configuration of the above job is defined in the job configuration XML file.
You need to write a your job as an implementation of InterruptableJob. To interrupt this job, you need handle to Scheduler , and call interrupt(jobKey<<job name & job group>>)
Please have a look @ javadoc for above classes, also quartz distribution contains an example for this (example7).
In Quartz 2.1 with Spring you can:
@Autowired private Scheduler schedulerFactoryBean; //injected by spring ... ... List<JobExecutionContext> currentlyExecuting = schedulerFactoryBean.getCurrentlyExecutingJobs(); //verifying if job is running for (JobExecutionContext jobExecutionContext : currentlyExecuting) { if(jobExecutionContext.getJobDetail().getKey().getName().equals("JobKeyNameToInterrupt")){ result = schedulerFactoryBean.interrupt(jobExecutionContext.getJobDetail().getKey()); } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With