Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Quartz - How to shutdown and restart the scheduler?

I am getting the scheduler as follows:

StdScheduler schedulerBean = (StdScheduler) ApplicationContextUtil.getBean(schedulerBeanName);
schedulerBean.start();

and later

schedulerBean.shutdown();

after executing few other codes, then i am calling schedulerBean.start() again.

Now the code throws the error - "org.quartz.SchedulerException: The Scheduler cannot be restarted after shutdown() has been called"

Could you please let me know how to fix the problem?

Thanks in advance, Kathir

like image 499
Kathir Avatar asked Feb 22 '13 09:02

Kathir


1 Answers

Once you have shutdown a scheduler, it cannot be restarted as threads and other resources are permanently destroyed.

You may read the Quartz Scheduler API references in order to find the method which covers your needs. Below you can find some available methods.

  • standby() method. Temporarily halts the Scheduler's firing of Triggers. When start() is called (to bring the scheduler out of stand-by mode), trigger misfire instructions will NOT be applied during the execution of the start() method - any misfires will be detected immediately afterward (by the JobStore's normal process).
  • pauseJob(JobKey jobKey) - resumeJob(JobKey jobKey). Pause the JobDetail with the given key - by pausing all of its current Triggers. On resumeJob(JobKey jobKey) if any of the Job'sTrigger s missed one or more fire-times, then the Trigger's misfire instruction will be applied.
  • interrupt(JobKey jobKey). Request the interruption, within this Scheduler instance, of all currently executing instances of the identified Job, which must be an implementor of the InterruptableJob interface. If more than one instance of the identified job is currently executing, the InterruptableJob#interrupt() method will be called on each instance. However, there is a limitation that in the case that interrupt() on one instances throws an exception, all remaining instances (that have not yet been interrupted) will not have their interrupt() method called. If you wish to interrupt a specific instance of a job (when more than one is executing) you can do so by calling getCurrentlyExecutingJobs() to obtain a handle to the job instance, and then invoke interrupt() on it yourself. 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.
  • unscheduleJob(TriggerKey triggerKey). Remove the indicated Trigger from the scheduler. If the related job does not have any other triggers, and the job is not durable, then the job will also be deleted.

Note that you can create a listener which implements the ServletContextListener interface so that:

  • all the jobs are interrupted and the scheduler is shutdown when the container is shutting down
  • the scheduler is created and the jobs are scheduled when the container is started

In case you would like to create such a listener I could provide more details.

I hope this helps.

like image 90
Apostolos Emmanouilidis Avatar answered Oct 05 '22 11:10

Apostolos Emmanouilidis