user1950349
user1950349

Reputation: 5146

shutdown all the quartz jobs as soon as app server is shut down?

I have a web application in which I am running some jobs periodically so for that I am using quartz framework here. Below is how I am starting all my jobs:

As soon as the server gets started up, it calls postInit method autmatically. And then I start all my jobs and it works fine:

  @PostConstruct
  public void postInit() {
    logger.logInfo("Starting all jobs");
    StdSchedulerFactory factory = new StdSchedulerFactory();
    try {
      factory.initialize(App.class.getClassLoader().getResourceAsStream("quartz.properties"));
      Scheduler scheduler = factory.getScheduler();
      // starts all our jobs using quartz_config.xml file
      scheduler.start();
    } catch (SchedulerException ex) {
      logger.logError("error while starting scheduler= ", ExceptionUtils.getStackTrace(ex));
    }
  }

  @PreDestroy
  public void shutdown() {
    logger.logInfo("Shutting down all jobs");
  }

Now I want to stop all the jobs that are running as soon as we try to shutdown the app server. So whenever we try to shutdown the app server, it will call shutdown method automatically. Now I need some way where we can shutdown all the jobs as soon as shutdown method is called. What is the best way by which I can shutdown all the jobs as soon as shutdown method is called?

Below is my "quartz.properties" file. Do I really need "quartz.properties" file since I guess I am using default values anyways I think?

#------------------------- Threads ---------------------------------#
# how many jobs we should run at the same time?
org.quartz.threadPool.threadCount=15

# ----------------------------- Plugins --------------------------- #
# class from where we should load the configuration data for each job and trigger.
org.quartz.plugin.jobInitializer.class=org.quartz.plugins.xml.XMLSchedulingDataProcessorPlugin
org.quartz.plugin.jobInitializer.fileNames = quartz_config.xml
org.quartz.plugin.jobInitializer.failOnFileNotFound = true
org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore

Upvotes: 2

Views: 4099

Answers (1)

shazin
shazin

Reputation: 21883

You can use Scheduler.shutdown() method as below and it is a good idea to externalize the quartz configuration even if you use the default parameters. This will make your code flexible.

  private Scheduler scheduler;

  @PostConstruct
  public void postInit() {
    logger.logInfo("Starting all jobs");
    StdSchedulerFactory factory = new StdSchedulerFactory();
    try {
      factory.initialize(App.class.getClassLoader().getResourceAsStream("quartz.properties"));
      scheduler = factory.getScheduler();
      // starts all our jobs using quartz_config.xml file
      scheduler.start();
    } catch (SchedulerException ex) {
      logger.logError("error while starting scheduler= ", ExceptionUtils.getStackTrace(ex));
    }
  }

  @PreDestroy
  public void shutdown() throws SchedulerException {
    logger.logInfo("Shutting down all jobs");
    scheduler.shutdown();
  }

Upvotes: 4

Related Questions