JGROUPS源码分析之一:协议栈的创建(一)

先从下面这两行代码为进入点:

1.      JChannel channel = new JChannel();

2.         channel.connect("x");

 

第一行代码创建一个数据传输通道channel,第二行代码表示channel连接到一个名称为x的集群上。

先看 JChannel channel = new JChannel() 做了哪些动作。

 

1.    调用默认的构造函数

public JChannel() throws ChannelException {

        this(DEFAULT_PROTOCOL_STACK);

    }

说明:

DEFAULT_PROTOCOL_STACK为协议栈配置参数,是这样定义的:

public static final String DEFAULT_PROTOCOL_STACK="udp.xml";

 

2.    接着调用一个带有字符串参数的构造函数

public JChannel(String properties) throws ChannelException {

        this(ConfiguratorFactory.getStackConfigurator(properties));

    }

说明:properties为协议栈配置文件,默认为udp.xml

       ConfiguratorFactory是一个协议栈配置工厂类,负责返回一个协议栈配置对象,协议栈对象解析协议,并负责创建协议对象。

3.    ConfiguratorFactory.getStackConfigurator(properties)调用过程,

/**

     * Returns a protocol stack configurator based on the provided properties

     * string.

     *

     * @param properties an old style property string, a string representing a

     *                   system resource containing a JGroups XML configuration,

     *                   a string representing a URL pointing to a JGroups XML

     *                   XML configuration, or a string representing a file name

     *                   that contains a JGroups XML configuration.

     */

    public static ProtocolStackConfigurator getStackConfigurator(String properties) throws ChannelException {

        if (propertiesOverride != null) {

            properties = propertiesOverride;

        }

 

        // added by bela: for null String props we use the default properties

        if(properties == null)

            properties=JChannel.DEFAULT_PROTOCOL_STACK;

 

        checkForNullConfiguration(properties);

 

        ProtocolStackConfigurator returnValue;

 

        // Attempt to treat the properties string as a pointer to an XML

        // configuration.

        XmlConfigurator configurator = null;

 

        try {

            configurator=getXmlConfigurator(properties);

        }

        catch (IOException ioe) {

            throw createChannelConfigurationException(ioe);

        }

 

        // Did the properties string point to a JGroups XML configuration?

        if (configurator != null) {

            returnValue=configurator;

        }

        else {

            // Attempt to process the properties string as the old style

            // property string.

            returnValue=new PlainConfigurator(properties);

        }

 

        return returnValue;

    }

从这段代码可以看出,先检查协议配置参数,通过则返回一个XML协议栈配置对象configurator。协议栈定义为一个接口

public interface ProtocolStackConfigurator

{

    String         getProtocolStackString();

    ProtocolData[] getProtocolStack(); // ProtocolData对象包含协议名称,实现类等

}

下面重点瞧configurator=getXmlConfigurator(properties);

调用过程,负责解析XML文件:


/**

     * Returns an XmlConfigurator based on the provided properties string (if

     * possible).

     *

     * @param properties a string representing a system resource containing a

     *                   JGroups XML configuration, a string representing a URL

     *                   pointing to a JGroups ML configuration, or a string

     *                   representing a file name that contains a JGroups XML

     *                   configuration.

     *

     * @return an XmlConfigurator instance based on the provided properties

     *         string; <code>null</code> if the provided properties string does

     *         not point to an XML configuration.

     *

     * @throws IOException  if the provided properties string appears to be a

     *                     

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值