What Is a Plug-In?

If you have used other open source frameworks such as Apache Struts, you're already familiar with concept of plug-ins and their use. Quite simply, a Quartz plug-in is a Java class that implements the org.quartz.spi.SchedulerPlugin interface and is registered as a plug-in with the Scheduler. The plug-in interface contains three methods and is shown in Listing 8.1.

Listing 8.1. A Quartz Plug-In Must Implement the SchedulerPlugin Interface

public interface SchedulerPlugin { public void initialize(String name, Scheduler scheduler) throws SchedulerException; public void start(); public void shutdown(); }

The methods of the SchedulerPlugin are called during the initialization and startup of a Scheduler. The Scheduler calls these methods on every plug-in that is registered. The following sections describe when each method of the plug-in is called.

The initialize() Method

The initialize() method is called during the creation of the Scheduler. When the getScheduler() method is called on the StdSchedulerFactory, the factory calls the initialize() method on all the registered plug-ins.

Plug Ins Don t Work with the DirectSchedulerFactory

Plug-ins are designed to be used only with the StdSchedulerFactory. It's a limitation within the framework. If you want to use plug-ins, then you'll need to use the StdSchedulerFactory to retrieve your Scheduler instance.

Each plug-in is registered with a unique name. This given name and Scheduler instance is included in the call to the initialize() method. You should take whatever action is necessary to initialize your plug-in. For example, your plug-in might need to read and parse data from a file or database.

Scheduler Is Not Fully Set Up at initialize() Time

The Scheduler has not been fully initialized when this method is called, so interaction with the Scheduler should be kept to a minimum. For example, you should not attempt to schedule any jobs during the initialize() method.

If there is a problem initializing your plug-in, you should throw an org.quartz.SchedulerConfigException, which is a subclass of SchedulerException. This prevents the plug-in from being loaded and stops any further interaction with the Scheduler.

The start() Method

The Scheduler instance calls the start() method to let the plug-in know that it can perform any startup actions it needs. For example, if you have jobs to schedule, this is the time to schedule them.

The shutdown() Method

The shutdown() method is called to inform the plug-in that the Scheduler is shutting down. This is an opportunity for the plug-in to clean up any open resources that are open. For example, database connections or open files should be closed.

Scheduler Instance Not Passed in start() or shutdown()

Notice that the Scheduler instance is not passed as an argument to the start() or shutdown() methods. If your plug-in needs access to the Scheduler during start() or shutdown(), you need to store the Scheduler in an instance variable in the plug-in.

Creating a Quartz Plug In

Категории