导读:
本文使用Jxta的版本为2.5_rc1
Jxta的初始化比较简单,一般都是实例化一个NetworkManager对象,并调用它的startNetwork方法。下面我们来看看背后发生的事。
实例化NetworkManager
调用NetworkManager.startNetwork首先来看NetworkManager的构造函数,同样比较简单,包括一些类私有变量的赋值(instanceName 、mode 、instanceHome ),同时调用configure方法根据mode参数(目前支持预定义的mode类型包括:ADHOC、EDGE、RENDEZVOUS、RELAY、RENDEZVOUS_RELAY、PROXY、SUPER,当然我们可以在此基础上进一步定制)对节点进行配置。这些不是本文所关心的。
java 代码
/**
* Creates NetworkManger instance.
* At this point, alternate Infrastructure PeerGroupID maybe specified, as well as a PeerID. if neither are
* specified, the default NetPeerGroupID will be used, and a new PeerID will be generated. Also note the default
* seeding URIs are the to development. Alternate values must be specified, if desired, prior to a call to {@link #startNetwork}
*
* @param mode Operating mode the node operating {@link ConfigMode}
* @param instanceName Node name
* @param instanceHome instance home is a uri to the instance persistent store (aka Cache Manager store home)
* @throws IOException if an io error occurs
*/
public NetworkManager(ConfigMode mode, String instanceName, URI instanceHome) throws IOException {
this.instanceName = instanceName;
this.mode = mode;
this.instanceHome = instanceHome;
configure(mode);
}
那么下面我们来看一下NetworkManager.startNetwork到底做了哪些操作
相对于以前的版本(例如2.4.1),NetworkManager这个类是在2.5_rc1(2.5_beta1应该也有,这个没有查过)开始加入的,原来需要我们手工处理的(例如发现或创建NetPeerGroup,请参考2.4.1的相关文档)现在转由这个类自动实现,下面我们来看一下这个实现过程
java 代码
/**
* Creates and starts the JXTA infrastructure peer group (aka NetPeerGroup) based on the specified mode
* template. This class also registers a listener for rendezvous events.
*
* @return The Net Peer Group
* @throws net.jxta.exception.PeerGroupException
* if the group fails to initialize
* @throws java.io.IOException if an io error occurs
*/
public synchronized PeerGroup startNetwork() throws PeerGroupException, IOException {
if (started) {
return netPeerGroup;
}
// create, and Start the default jxta NetPeerGroup
NetPeerGroupFactory factory = new NetPeerGroupFactory(config.getPlatformConfig(), instanceHome);
netPeerGroup = factory.getInterface();
if (configPersistent) {
config.save();
}
rendezvous = netPeerGroup.getRendezVousService();
rendezvous.addListener( this);
started = true
return netPeerGroup;
}
这个方法返回的就是NetPeerGroup,通常我们下一步的工作就是用这个NetPeerGroup来创建子对等组供我们进一步使用。
首先判断Jxta网络是否已经启动,如果已经启动,那么netPeerGroup肯定已经初始化,直接返回即可。
java 代码
if (started) {
return netPeerGroup;
}
接着创建并启动默认的jxta NetPeerGroup,这部分是我们下面要说的重点,这里先略过(下一篇文章会讲),我们先跳到下面的部分。
java 代码
// create, and Start the default jxta NetPeerGroup
NetPeerGroupFactory factory = new NetPeerGroupFactory(config.getPlatformConfig(), instanceHome);
netPeerGroup = factory.getInterface();
是否将当前的配置持久化,如果需要持久化(例如我们在预定义的那几种模式的基础上作了自定义),那么调用config.save(),将配置持久化到硬盘上(该部分内容会令开帖子叙述),持久化的配置可以直接从硬盘加载。
java 代码
if (configPersistent) {
config.save();
}
为集合点注册监听器,置Jxta网络为启动状态并返回NetPeerGroup实例
本文转自
http://cuizhenfu.javaeye.com/blog/102394
本文使用Jxta的版本为2.5_rc1
Jxta的初始化比较简单,一般都是实例化一个NetworkManager对象,并调用它的startNetwork方法。下面我们来看看背后发生的事。
实例化NetworkManager
调用NetworkManager.startNetwork首先来看NetworkManager的构造函数,同样比较简单,包括一些类私有变量的赋值(instanceName 、mode 、instanceHome ),同时调用configure方法根据mode参数(目前支持预定义的mode类型包括:ADHOC、EDGE、RENDEZVOUS、RELAY、RENDEZVOUS_RELAY、PROXY、SUPER,当然我们可以在此基础上进一步定制)对节点进行配置。这些不是本文所关心的。
java 代码
/**
* Creates NetworkManger instance.
* At this point, alternate Infrastructure PeerGroupID maybe specified, as well as a PeerID. if neither are
* specified, the default NetPeerGroupID will be used, and a new PeerID will be generated. Also note the default
* seeding URIs are the to development. Alternate values must be specified, if desired, prior to a call to {@link #startNetwork}
*
* @param mode Operating mode the node operating {@link ConfigMode}
* @param instanceName Node name
* @param instanceHome instance home is a uri to the instance persistent store (aka Cache Manager store home)
* @throws IOException if an io error occurs
*/
public NetworkManager(ConfigMode mode, String instanceName, URI instanceHome) throws IOException {
this.instanceName = instanceName;
this.mode = mode;
this.instanceHome = instanceHome;
configure(mode);
}
那么下面我们来看一下NetworkManager.startNetwork到底做了哪些操作
相对于以前的版本(例如2.4.1),NetworkManager这个类是在2.5_rc1(2.5_beta1应该也有,这个没有查过)开始加入的,原来需要我们手工处理的(例如发现或创建NetPeerGroup,请参考2.4.1的相关文档)现在转由这个类自动实现,下面我们来看一下这个实现过程
java 代码
/**
* Creates and starts the JXTA infrastructure peer group (aka NetPeerGroup) based on the specified mode
* template. This class also registers a listener for rendezvous events.
*
* @return The Net Peer Group
* @throws net.jxta.exception.PeerGroupException
* if the group fails to initialize
* @throws java.io.IOException if an io error occurs
*/
public synchronized PeerGroup startNetwork() throws PeerGroupException, IOException {
if (started) {
return netPeerGroup;
}
// create, and Start the default jxta NetPeerGroup
NetPeerGroupFactory factory = new NetPeerGroupFactory(config.getPlatformConfig(), instanceHome);
netPeerGroup = factory.getInterface();
if (configPersistent) {
config.save();
}
rendezvous = netPeerGroup.getRendezVousService();
rendezvous.addListener( this);
started = true
return netPeerGroup;
}
这个方法返回的就是NetPeerGroup,通常我们下一步的工作就是用这个NetPeerGroup来创建子对等组供我们进一步使用。
首先判断Jxta网络是否已经启动,如果已经启动,那么netPeerGroup肯定已经初始化,直接返回即可。
java 代码
if (started) {
return netPeerGroup;
}
接着创建并启动默认的jxta NetPeerGroup,这部分是我们下面要说的重点,这里先略过(下一篇文章会讲),我们先跳到下面的部分。
java 代码
// create, and Start the default jxta NetPeerGroup
NetPeerGroupFactory factory = new NetPeerGroupFactory(config.getPlatformConfig(), instanceHome);
netPeerGroup = factory.getInterface();
是否将当前的配置持久化,如果需要持久化(例如我们在预定义的那几种模式的基础上作了自定义),那么调用config.save(),将配置持久化到硬盘上(该部分内容会令开帖子叙述),持久化的配置可以直接从硬盘加载。
java 代码
if (configPersistent) {
config.save();
}
为集合点注册监听器,置Jxta网络为启动状态并返回NetPeerGroup实例
本文转自
http://cuizhenfu.javaeye.com/blog/102394