Jxta初始化过程(一)

导读:
  本文使用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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值