(四)Tomcat源码阅读:Service组件分析
一、概述
这介绍中表达的比较有价值的信息是各个service是相互隔离的,但是又共享jvm的一些基础类。
/** * A <strong>Service</strong> is a group of one or more * <strong>Connectors</strong> that share a single <strong>Container</strong> * to process their incoming requests. This arrangement allows, for example, * a non-SSL and SSL connector to share the same population of web apps. * <p> * A given JVM can contain any number of Service instances; however, they are * completely independent of each other and share only the basic JVM facilities * and classes on the system class path. * * @author Craig R. McClanahan */
二、阅读源码
Service中的组件其实和Server中的组件大差不差,我这里重点介绍Service广播事件的一个方法。
其中的顺序大概是这样的:StandardService调用PropertyChangeSupport的广播方法,然后利用PropertyChangeEvent传参完成事件广播。其中比较重要的就是PropertyChangeSupport类,我接下会介绍它的广播方法。
1、PropertyChangeSupport
//用来装监听器的map
private PropertyChangeListenerMap map = new PropertyChangeListenerMap();
public void firePropertyChange(PropertyChangeEvent event) {
Object oldValue = event.getOldValue();
Object newValue = event.getNewValue();
//进行条件判断,判断通过则进入下一步
if (oldValue == null || newValue == null || !oldValue.equals(newValue)) {
String name = event.getPropertyName();
//拿到通用监听器
PropertyChangeListener[] common = this.map.get(null);
//拿到指定监听器
PropertyChangeListener[] named = (name != null)
? this.map.get(name)
: null;
//分别进行广播
fire(common, event);
fire(named, event);
}
}
//这是最后执行广播的方法就是调用了监听器,把事件变化发送出去
private static void fire(PropertyChangeListener[] listeners, PropertyChangeEvent event) {
if (listeners != null) {
for (PropertyChangeListener listener : listeners) {
listener.propertyChange(event);
}
}
}