Using Multiple Plug-Ins

Table of contents:

You can register as many plug-ins in the quartz.properties file as you like. However, the order of loading and initialization can't be guaranteed because Quartz loads all the properties into a map and then loops through the plug-ins in the order that they are retrieved from the map.

To get around this limitation, you can create a Quartz plug-in that acts as a parent plug-in and loads multiple other plug-ins in a given order. Listing 8.6 shows what the ParentPlugin looks like.

Listing 8.6. The ParentPlugin Can Load Child Plug-Ins in a Specified Order

package org.cavaness.quartzbook.chapter8;

import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.quartz.Scheduler;
import org.quartz.SchedulerConfigException;
import org.quartz.SchedulerException;
import org.quartz.spi.SchedulerPlugin;

public class ParentPlugin implements SchedulerPlugin {
 private static Log logger = LogFactory.getLog(ParentPlugin.class);

 // A list of child plug-ins
 private List childPlugins = new ArrayList();

 private String childPluginNames;

 private String pluginName;

 private Scheduler scheduler;

 /**
 * Default no-arg Constructor
 *
 */
 public ParentPlugin() {
 }

 /**
 * Pass the initialize call on to the child plug-ins.
 *
 * @throws SchedulerConfigException
 * if there is an error initializing.
 */

public void initialize(String name, final Scheduler scheduler)
 throws SchedulerException {

 this.pluginName = name;
 this.scheduler = scheduler;

 logger.info("Searching for child plugins to load");

 // The child plug-ins are comma-separated
 StringTokenizer tokenizer =
 new StringTokenizer(childPluginNames, ",");

 while (tokenizer.hasMoreElements()) {
 String pluginClassname = tokenizer.nextToken();

 try {
 Class pluginClass =
 Class.forName(pluginClassname);

 Object obj = pluginClass.newInstance();

 // Make sure the specified class is a plug-in
 if (obj instanceof SchedulerPlugin) {
 // Initialize the Plugin
 SchedulerPlugin childPlugin =
 (SchedulerPlugin) obj;

 logger.info("Init child Plugin " +
 pluginClassname);

 childPlugin.initialize(pluginClassname,
 scheduler);

 // Store the child plug-in in the list
 childPlugins.add(childPlugin);
 } else {
 // Skip loading class
 logger.error("Class is not a plugin " +
 pluginClass);
 }

 } catch (Exception ex) {
 // On error, log and go to next child plug-in
 logger.error("Error loading plugin " +
 pluginClassname, ex);
 }
 }
 }
 public void start() {
 // Start each child plug-in
 int size = childPlugins.size();
 for (int i = 0; i < size; i++) {
 SchedulerPlugin childPlugin =
 ((SchedulerPlugin) childPlugins.get(i));

 logger.info("Starting Child Plugin " + childPlugin);
 childPlugin.start();
 }
 }

 public void shutdown() {
 // Stop each child plug-in
 int size = childPlugins.size();
 for (int i = 0; i < size; i++) {
 SchedulerPlugin childPlugin =
 ((SchedulerPlugin) childPlugins.get(i));

 logger.info("Stopping Plugin " + childPlugin);
 childPlugin.shutdown();
 }
 }

 public String getPluginName() {
 return pluginName;
 }

 public void setPluginName(String pluginName) {
 this.pluginName = pluginName;
 }

 public String getChildPluginNames() {
 return childPluginNames;
 }

 public void setChildPluginNames(String childPluginNames) {
 this.childPluginNames = childPluginNames;
 }
}

The plug-in in Listing 8.6 doesn't do anything, as far as a plug-in goes, but it acts as a loader of the child plug-ins. A child plug-in is any valid Quartz plug-in. It can be one of your own or one of those included with the framework.

Configuring the ParentPlugin in the quartz.properties File

To configure the ParentPlugin in the quartz.properties file, just add the parent as you would any other plug-in. That is, add the following line:

org.quartz.plugin.parentPlugin.class =
 org.cavaness.quartzbook.chapter8.ParentPlugin

Then, to add the child plug-ins and the order you want them loaded, just specify a comma-separated list of plug-ins:


 

[View full width]

org.quartz.plugin.parentPlugin.childPluginNames=org.quartz.plugins.history .LoggingJobHistoryPlugin,org.quartz.plugins.history.LoggingTriggerHistoryPlugin

As Listing 8.6 shows, the ParentPlugin tokenizes the comma-separated string and loads the plug-ins in the order they were in the list. This might seem like a lot of complexity, but it gets the job done. A future version of the Quartz framework might support a load order mechanism for plug-ins, but for now, the ParentPlugin works fine.


Quartz Utility Plug Ins

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