Working with the Scheduler

This section provides several examples of using the administrative functions of the Quartz Scheduler.

Creating and Starting the Scheduler

You can start the Quartz Scheduler in several ways, but the easiest is to use one of the two SchedulerFactory implementations. In particular, the org.quartz.impl.StdSchedulerFactory is easy to use and performs all the work of setting up the Schedulerall you need to do is use the static geTDefaultScheduler() method, as Listing 12.1 demonstrates.

Listing 12.1. Starting the Default Scheduler

public void startScheduler() {
 Scheduler scheduler = null;

 try {
 // Get a Scheduler instance from the Factory
 scheduler = StdSchedulerFactory.getDefaultScheduler();

 // Start the scheduler
 scheduler.start();
 logger.info("Scheduler started at " + new Date());

 // Schedule jobs and triggers

 } catch (SchedulerException ex) {
 // deal with any exceptions
 logger.error(ex);
 }

}

When you have an instance of the Scheduler from the factory, you can start the Scheduler and add any necessary jobs and triggers. You can add jobs and triggers either before or after you start the Scheduler.

The Quartz framework supports multiple configuration files, which enables you to create different versions of the Scheduler. As an example, one version of the configuration file might set up the Scheduler as a single instance using the RAMJobStore, whereas a different configuration file might configure the Scheduler to be part of a cluster and use one of the JDBCJobStores.

To specify a configuration file other than the default, you can use the initialize() method on the StdSchedulerFactory and specify the name of the configuration file as an argument. Listing 12.2 illustrates an example of this.

Listing 12.2. Starting a Scheduler Using a Different Quartz Configuration File

public static void main(String[] args) {

 Scheduler scheduler = null;

 try {
 StdSchedulerFactory factory =
 new StdSchedulerFactory();
 factory.initialize("myquartz.properties");
 scheduler = factory.getScheduler();
 scheduler.start();
 logger.info("Scheduler started at " + new Date());

 // Schedule jobs and triggers

 } catch (SchedulerException ex) {
 // deal with any exceptions
 logger.error(ex);
 }
}

Loading Jobs into the Scheduler

These examples started the Scheduler but did not add any jobs to it. You can start the Scheduler first and then add your jobs, or you can choose to add the jobs first and then start the Scheduler. Either way works fine. Later in this chapter, we show some examples of doing it both ways.

 

Stopping a Scheduler

The Scheduler API includes two versions of the shutdown() method. One takes a Boolean argument, and one takes no arguments. The Boolean parameter tells the Scheduler to wait until executing jobs are finished.

Using the no-argument version is the same as passing false to the alternative method. If you don't care about stopping any currently executing jobs, just call this:

scheduler.shutdown();

Alternatively, use this:

scheduler.shutdown(false);

On the other hand, if you want the executing jobs to finish before stopping the Scheduler, pass true to the shutdown method:

scheduler.shutdown(true);

 

Pausing a Scheduler (Standby Mode)

To temporarily halt the firing of any triggers, you can call the standby() method on the Scheduler. The Scheduler and its resources are not destroyed, and the Scheduler can be restarted at any time. Listing 12.3 shows an example of using the standby() method.

Listing 12.3. Putting a Scheduler in Standby Mode

public void runScheduler() {
 Scheduler scheduler = null;

 try {
 // Get a Scheduler instance from the factory
 scheduler = StdSchedulerFactory.getDefaultScheduler();

 // Start the scheduler
 scheduler.start();

 // Pause the scheduler for some reason
 scheduler.standby();

 // Restart the scheduler
 scheduler.start();

 } catch (SchedulerException ex) {
 // deal with any exceptions
 logger.error(ex);
 }
}

When a Scheduler is put in standby mode, scheduled triggers will not fire. When the Scheduler is restarted using the start() method, all triggers that should have fired are processed based on the misfire settings.


Scheduling in the Enterprise

Getting Started with Quartz

Hello, Quartz

Scheduling Jobs

Cron Triggers and More

JobStores and Persistence

Implementing Quartz Listeners

Using Quartz Plug-Ins

Using Quartz Remotely

Using Quartz with J2EE

Clustering Quartz

Quartz Cookbook

Quartz and Web Applications

Using Quartz with Workflow

Appendix A. Quartz Configuration Reference



Quartz Job Scheduling Framework(c) Building Open Source Enterprise Applications
Quartz Job Scheduling Framework: Building Open Source Enterprise Applications
ISBN: 0131886703
EAN: 2147483647
Year: N/A
Pages: 148

Flylib.com © 2008-2020.
If you may any questions please contact us: flylib@qtcs.net