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.
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.
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.
Creating a Quartz Plug In
|