Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to interrupt or stop currently running quartz job?

Tags:

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?

like image 711
breedish Avatar asked Aug 23 '11 09:08

breedish


People also ask

How do you stop a Quartz job from running?

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.

How do you reset Quartz Scheduler?

Just delete your scheduler object after schedulerBean. Shutdown() and call your scheduler method again as you did it first time.

How do I start Quartz Scheduler in web application?

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.


2 Answers

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).

like image 149
Dhananjay Avatar answered Oct 13 '22 16:10

Dhananjay


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());     } } 
like image 29
fl4l Avatar answered Oct 13 '22 14:10

fl4l