先从下面这两行代码为进入点:
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
*