JBoss NamingSystem

  服务定义及启动

<mbean code="org.jboss.naming.NamingService"

      name="jboss:service=Naming"

      xmbean-dd="resource:xmdesc/NamingService-xmbean.xml">

  <attribute name="CallByValue">false</attribute>

  <attribute name="Port">1099</attribute>

  <attribute name="BindAddress">${jboss.bind.address}</attribute>

  <attribute name="RmiPort">1098</attribute>

  <attribute name="RmiBindAddress">${jboss.bind.address}</attribute>

  <depends optional-attribute-name="LookupPool"         proxy-type="attribute">jboss.system:service=ThreadPool</depends>

</mbean> 

NamingService

  // 构造函数 NamingService()

  naming = new Main(this.getClass().getName());

  Main是一个jnp名字服务的实现.

  // 请关注,proxyFactory(JRMPProxyFactoryMBean) 

  // 启动服务 startService()

  naming.start();

    bind ObjectFactory to "java:comp" 

Main.start() 

  // 构造NamingServer, NamingServerJNDI名字服务的实现.

  theServer = new NamingServer(); 

  // initCustomSockFactories() 

  // initJnpInvoker(), 创建NamingServer的服务Stub.

  Remote stub = UnicastRemoteObject.exportObject(theServer, ...);

  serverStub = new MarshalledObject(stub); 

  // initBootstrapListencer()

  启动一个AcceptHandler线程开始listen, 输出serverStub; 

NamingServer 

NamingServerjbossns的核心实现类, 它保存着所有注册的组件,组件的存储结构为树状的。

 

InitialContext

InitialContext是客户端访问JNDI的入口,它通过给定的properties值进行初始化,它可以连接到本地(同一JVM)或者远程的JNDI,典型的初始化代码如下:

Properties props = new Properties();

props.put("java.naming.factory.initial", "...");

props.put("java.naming.factory.url.pkgs", "...");

props.put("java.naming.provider.url", "jnp://...");

InitialContext initCtx = new InitialContext(props); 

InitialContext中有一个getDefaultInitCtx方法,它根据java.naming.factory.initial的值来创建Context.

// initialContext.java

protected Context getDefaultInitCtx() throws NamingException{

    if (!gotDefault) {

    defaultInitCtx = NamingManager.getInitialContext(myProps);

        gotDefault = true;

    }

    return defaultInitCtx;

NamingContextFactory实现了InitialContextFactory接口(必须).

// org.jnp.interfaces.NamingContextFactory

public Context getInitialContext(Hashtable env)

      throws NamingException {

String providerURL = (String) env.get(Context.PROVIDER_URL);

Name prefix = null;

int comma = providerURL != null ? providerURL.indexOf(',') : -1;

    if( providerURL != null && comma < 0 ) {

        // 处理prefix

    }

   return new NamingContext(env, prefix, null);

}

这里返回一个NamingContext, 它是Context的实现类,对InitialContext的一些操作都将转发给它,如lookup, bindunbind等操作。 

NamingContext

NamingContextJBossNS中是一个客户端的角色,它与服务端进行通信并得到一个NamingServerstub 

static Naming getServer(String host, int port,

        Hashtable serverEnv)

      throws NamingException { 

  // step 1.

  SocketFactory factory = loadSocketFactory(serverEnv);

  Socket s; 

  // parse localAddr, localPort

  // ... 

  // step 2.

  s = factory.createSocket(host, port, localAddr, localPort); 

  // Get stub from naming server

  // step 3.

  BufferedInputStream bis = new BufferedInputStream(s.getInputStream());

  ObjectInputStream in = new ObjectInputStream(bis);

  MarshalledObject stub = (MarshalledObject) in.readObject();

  server = (Naming) stub.get();

  s.close(); 

  return server;

}

首先装载SocketFactory的实现类,然后解析localAddrlocalPort并创建客户端socket与服务端进行通信,还记得前面的initBootstrapListencer吗?这里创建的socket就是与initBootstrapListencer创建的服务端socket进行连接.

最后我们就得到了一个NameingServerstub. 



 

[jbossns
关系图]

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值