Search code examples
javaquartz-schedulerinterruptschedulerrestart

Start a Single Job in Quarz Scheduler after interrupt


i want to ask if it's possible to start a single Job after i interrupt him. (10 seconds later).

Or does someone know how to restart a single Job in Quarz Scheduler ?

code for the interrupt:

  for(int i=0; i < 2; i++) {
            try {
                Thread.sleep(7000L); 
                // tell the scheduler to interrupt our job
                sched.interrupt(job.getKey());
            } catch (Exception e) {
            }

thanks for your help regards


Solution

  • That should be perfectly possible. Please note that by interrupting a Quartz job (by invoking the interrupt(jobKey) method) you only give the job a hint to finish whatever it is doing and finish prematurely. If the job ignores the interrupt request, then it will continue executing. In order to be able to receive interrupt requests, the job must implement the org.quartz.InterruptableJob interface with a single method that is invoked by the scheduler. In that method you typically set some sort of an interrupt flag (e.g. AtomicBoolean field in the job class) and in the execute method you check the value of the flag at stages when it is safe to interrupt the job (e.g. between independent DB transactions etc.)

    When you start the job again, a new job instance will be allocated and executed by Quartz. You may want to annotate your job with @DisallowConcurrentExecution to prevent situations when you interrupt a job and it does not finish running by the time you attempt to start the job again. The annotation will prevent two concurrent job execution of the same job (i.e. different job instances of the same job class).