(四)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);
            }
        }
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小海海不怕困难

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值