1.为什么用插件模式?
1.1 场景
想创建一个开放的应用,主应用只包含标准的功能,个性化的功能交给其他开发者实现
1.2 解决方法
- 主应用不知道子应用的存在:开放API,子应用独立运行。百度地图开放API、jar依赖、SDK、小程序1都属于这种。
- 主应用知道子应用的存在,子应用运行依赖于主应用:插件模式
2.插件模式怎么实现–Quartz中的插件
2.1 定义插件接口
public interface SchedulerPlugin {
void initialize(String name, Scheduler scheduler, ClassLoadHelper loadHelper)
throws SchedulerException;
void start();
void shutdown();
}
- 说明:开发者开发插件时需实现上面的接口
2.2 初始化时注册插件
//StdSchudulerFactory的instantiate方法片段
//从配置文件中加载插件类,注册到QuartzSchedulerResources对象中
String[] pluginNames = cfg.getPropertyGroups(PROP_PLUGIN_PREFIX);
SchedulerPlugin[] plugins = new SchedulerPlugin[pluginNames.length];
for (int i = 0; i < pluginNames.length; i++) {
Properties pp = cfg.getPropertyGroup(PROP_PLUGIN_PREFIX + "."
+ pluginNames[i], true);
String plugInClass = pp.getProperty(PROP_PLUGIN_CLASS, null);
if (plugInClass == null) {
initException = new SchedulerException(
"SchedulerPlugin class not specified for plugin '"
+ pluginNames[i] + "'");
throw initException;
}
SchedulerPlugin plugin = null;
try {
plugin = (SchedulerPlugin)
loadHelper.loadClass(plugInClass).newInstance();
} catch (Exception e) {
initException = new SchedulerException(
"SchedulerPlugin class '" + plugInClass
+ "' could not be instantiated.", e);
throw initException;
}
try {
setBeanProps(plugin, pp);
} catch (Exception e) {
initException = new SchedulerException(
"JobStore SchedulerPlugin '" + plugInClass
+ "' props could not be configured.", e);
throw initException;
}
plugins[i] = plugin;
}
// add plugins
for (int i = 0; i < plugins.length; i++) {
rsrcs.addSchedulerPlugin(plugins[i]);
}
2.3 启动时调用插件的start方法
//QuartzScheduler中的方法,启动时会调用
private void startPlugins() {
java.util.Iterator<SchedulerPlugin> itr = resources.getSchedulerPlugins().iterator();
while (itr.hasNext()) {
SchedulerPlugin plugin = itr.next();
plugin.start();
}
}
3. Quartz插件示例
// quartz.properties文件配置
org.quartz.plugin.triggHistory.class: org.quartz.plugins.history.LoggingJobHistoryPlugin
public class LoggingTriggerHistoryPlugin implements SchedulerPlugin,
TriggerListener {
//此处省略了非插件相关代码
/*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*
* Constructors.
*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
public LoggingTriggerHistoryPlugin() {
}
/*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*
* SchedulerPlugin Interface.
*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
public void initialize(String pname, Scheduler scheduler, ClassLoadHelper classLoadHelper)
throws SchedulerException {
this.name = pname;
scheduler.getListenerManager().addTriggerListener(this, EverythingMatcher.allTriggers());
}
public void start() {
// do nothing...
}
public void shutdown() {
// nothing to do...
}
}
2020新版微信小程序可以脱离微信客户端运行;以前版本以及其他小程序可能需要依赖于主应用 ↩︎