21

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?

Sangram Anand
  • 9,494
  • 21
  • 64
  • 102
breedish
  • 5,331
  • 11
  • 36
  • 57

6 Answers6

36

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

Luke Hutton
  • 9,906
  • 6
  • 28
  • 54
blob
  • 3,655
  • 2
  • 26
  • 42
  • 1
    The limitation of this approach is **not** cluster aware. Java doc: *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.* – 卢声远 Shengyuan Lu Apr 28 '16 at 07:39
5

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());
    }
}
fl4l
  • 1,324
  • 3
  • 18
  • 25
1

The best solution in my opinion is the one described in this thread: http://forums.terracotta.org/forums/posts/list/7700.page

I've just introduced a "sleep" after set stop flag to true to allow the job to finish cleanly.

    @Override
public void interrupt() throws UnableToInterruptJobException {
    stopFlag.set(true);
    try {
        Thread.sleep(30000);
    } catch (InterruptedException e) {
        //logger.error("interrupt()", e);
    }
    Thread thread = runningThread.getAndSet(null);
    if (thread != null)
        thread.interrupt();
}
melfore
  • 159
  • 1
  • 6
0

I don't know why nobody mentioned this, or maybe this was not available at the time the question was asked.

There is a method called shutdown for a Scheduler instance.

 SchedulerFactory factory = new StdSchedulerFactor();
 Scheduler scheduler = factory.getScheduler();

The above is used to start a job like

 scheduler.start();

Use a flag or something to know when to stop the job from running. Then use

 scheduler.shutdown();

How I implemented my requirement:

if(flag==true)
    {
        scheduler.start();
        scheduler.scheduleJob(jobDetail, simpleTrigger);
    }
    else if(flag==false)
    {
        scheduler.shutdown();
    }

Where jobDetail and simpleTrigger are self explanatory.

Hope it helps. :)

Nikhil Kumar
  • 79
  • 1
  • 7
  • 17
    This solution is shutting down the whole scheduler which is not really correct unless you have one job assigned to the scheduler. You should have a way to remove only specific job not the whole scheduler because it is possible to have more than one job running on the same scheduler. – Salman Oct 18 '15 at 06:06
  • Having more than one job on a Scheduler? I would even say it's extremely common, and shutting down the Scheduler would be a disaster in that case. – Jolta Mar 20 '19 at 09:27
0

Probably a little late to answer this, but may be it can help:

If you no longer need a job, you can delete that particular job using

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

On the other hand if you want to completely shutdown the scheduler and all the jobs therein you can do

scheduler.shutdown();

This is a pretty nice example of different job related tasks.

Swapnil B.
  • 649
  • 6
  • 21
0

To interrupt all running jobs

for (JobExecutionContext currentlyExecutingJob : scheduler.getCurrentlyExecutingJobs()) {
    if (InterruptableJob.class.isAssignableFrom(currentlyExecutingJob.getJobDetail().getJobClass())) // Otherwise it will throw an exception
        scheduler.interrupt(currentlyExecutingJob.getFireInstanceId());
}

You can get jobs informations in JobExecutionContext, retrieve the Job class or things like that.

apflieger
  • 408
  • 3
  • 9