示例:使用 JNDI 查找 EJB Home 接口或业务接口

使用 Java™ 命名和目录接口 (JNDI) 的大部分应用程序运行在容器中。有些则不是。用于查询对象的名称取决于应用程序是否在容器中运行。有时,应用程序使用 corbaname URL 作为查询名称更为方便。基于容器的 JNDI 客户机和瘦 Java 客户机可以使用 corbaname URL。

以下示例显示如何从不同类型的应用程序执行 JNDI 查询。

从容器中运行的应用程序进行 JNDI 查询

容器中运行的应用程序可以使用 java: 查询名称。此表单的查询名称提供一个间接层,这样当对象名称绑定在名称服务器的名称空间中时,用于查询对象的查询名称将不依赖于对象的名称。应用程序的部署描述符提供来自 java: 名称的映射以及名称服务器查询名称。容器根据部署描述符信息设置 java: 名称空间,这样可以将java: 名称正确地映射到相应的对象。

以下示例说明如何查询 EJB 3.0 远程业务接口。按照接口 ibm-ejb-jar-bnd.xml 绑定文件(如果存在),或者按照 EJB 容器分配的确实名称(如果绑定文件不存在)来确定实际的主查询名称。有关更多信息,请参阅与业务接口和 Home 接口的缺省绑定相关的主题,以及与 EJB 业务接口和 Home 接口相关的主题。
// Get the initial context as shown in a previous example.
...
// Look up the business interface using the JNDI name.
try {
   java.lang.Object ejbBusIntf =
      initialContext.lookup(
         "java:comp/env/com/mycompany/accounting/Account");
   accountIntf =
      (Account)javax.rmi.PortableRemoteObject.narrow(ejbBusIntf, Account.class);
}
   catch (NamingException e) { // Error getting the business interface
   ...
}

以下示例显示 EJB 1.x 或 2.x EJB Home 接口的查询。实际的 home 查询名称由应用程序的部署描述符确定。企业 Bean (EJB) 驻留在 EJB 容器中,该容器在 Bean 和它所驻留的应用程序服务器之间提供接口。

// Get the initial context as shown in a previous example
...
// Look up the home interface using the JNDI name
try {
   java.lang.Object ejbHome =
      initialContext.lookup(
         "java:comp/env/com/mycompany/accounting/AccountEJB");
   accountHome = (AccountHome)javax.rmi.PortableRemoteObject.narrow(
      (org.omg.CORBA.Object) ejbHome, AccountHome.class);
}
   catch (NamingException e) { // Error getting the home interface
   ...
}

从不在容器中运行的应用程序进行 JNDI 查询

不是运行在容器中的应用程序不能使用 java: 查询名称,因为由容器设置应用程序的 java: 名称空间。相反,此类型的应用程序必须直接从名称服务器查询对象。每个应用程序服务器都包含名称服务器。系统工件(例如 EJB home)绑定到相对于该名称服务器中的服务器根上下文。根据系统名称空间结构联合不同的名称服务器。查询不同服务器上的对象的建议方法是限定名称,以从单元中的任何初始上下文解析名称。如果使用相对名,那么初始上下文必须是在其下绑定对象的服务器根上下文。限定名的格式是根据限定名是基于拓扑的名称还是固定名。以下是每种格式的限定名的示例。

  • 基于拓扑的限定名

    基于拓扑的限定名遍历系统名称空间到达绑定目标对象的服务器根上下文。从单元中的任何初始上下文解析基于拓扑的限定名。

    基于拓扑的限定名是根据对象在单个服务器上还是服务器集群上。以下是每种查询的示例。

    单个服务器
    以下示例说明如何查询在节点 Node1 中配置的单个服务器 MyServer 上运行的 EJB 业务接口。
    // Get the initial context as shown in a previous example.
    // Using the form of lookup name below, it does not matter which
    // server in the cell is used to obtain the initial context.
    ...
    // Look up the business interface using the JNDI name
    try {
       java.lang.Object ejbBusIntf = initialContext.lookup(
          "cell/nodes/Node1/servers/MyServer/com/mycompany/accounting/Account");
       accountIntf =
         (Account)javax.rmi.PortableRemoteObject.narrow(ejbBusIntf, Account.class);
    }
    catch (NamingException e) { // Error getting the business interface
       ...
    }

    以下示例说明了对节点 Node1 中配置的单个服务器 MyServer 中运行的 EJB Home 接口的查询。

    // Get the initial context as shown in a previous example
    // Using the form of lookup name below, it doesn't matter which
    // server in the cell is used to obtain the initial context.
    ...
    // Look up the home interface using the JNDI name
    try {
       java.lang.Object ejbHome = initialContext.lookup(
          "cell/nodes/Node1/servers/MyServer/com/mycompany/accounting/AccountEJB");
       accountHome = (AccountHome)javax.rmi.PortableRemoteObject.narrow(
          (org.omg.CORBA.Object) ejbHome, AccountHome.class);
    }
    catch (NamingException e) { // Error getting the home interface
       ...
    }
    服务器集群
    以下示例说明如何查询在集群 MyCluster 中运行的 EJB 业务接口。如果有任何的集群成员在运行,那么可以解析名称。
    // Get the initial context as shown in a previous example.
    // Using the form of lookup name below, it does not matter which
    // server in the cell is used to obtain the initial context.
    ...
       // Look up the business interface using the JNDI name
    try {
       java.lang.Object ejbBusIntf = initialContext.lookup(
          "cell/clusters/MyCluster/com/mycompany/accounting/Account");
       accountIntf =
         (Account)javax.rmi.PortableRemoteObject.narrow(ejbBusIntf, Account.class);
    }
    catch (NamingException e) { // Error getting the business interface
       ...
    }

    以下示例说明如何查询在集群 MyCluster 中运行的 EJB Home 接口。如果有任何的集群成员在运行,那么可以解析名称。

    // Get the initial context as shown in a previous example
    // Using the form of lookup name below, it does not matter which
    // server in the cell is used to obtain the initial context.
    ...
       // Look up the home interface using the JNDI name
    try {
       java.lang.Object ejbHome = initialContext.lookup(
          "cell/clusters/MyCluster/com/mycompany/accounting/AccountEJB");
       accountHome = (AccountHome)javax.rmi.PortableRemoteObject.narrow(
          (org.omg.CORBA.Object) ejbHome, AccountHome.class);
    }
    catch (NamingException e) { // Error getting the home interface
       ...
    }
  • 固定的限定名

    如果目标对象为它定义了以单元为作用域的固定名,那么可以使用它的限定格式而不是基于拓扑的限定名。即使基于拓扑的名称有效,已修订的名称也不会更改为特定的单元拓扑或从目标对象移动至其他服务器。

    以下示例说明如何使用固定的限定名来查询 EJB 业务接口。
    // Get the initial context as shown in a previous example.
    // Using the form of lookup name below, it does not matter which
    // server in the cell is used to obtain the initial context.
    ...
    // Look up the business interface using the JNDI name
    try {
       java.lang.Object ejbBusIntf = initialContext.lookup(
         "cell/persistent/com/mycompany/accounting/Account");
       accountIntf =
         (Account)javax.rmi.PortableRemoteObject.narrow(ejbBusIntf, Account.class);
       }
    catch (NamingException e) { // Error getting the business interface
    ... 
    
    }

    对标准固定名称进行的示例查询如下所示:

    // Get the initial context as shown in a previous example
    // Using the form of lookup name below, it doesn't matter which
    // server in the cell is used to obtain the initial context.
    ...
    // Look up the home interface using the JNDI name
    try {
       java.lang.Object ejbHome = initialContext.lookup(
         "cell/persistent/com/mycompany/accounting/AccountEJB");
       accountHome = (AccountHome)javax.rmi.PortableRemoteObject.narrow(
          (org.omg.CORBA.Object) ejbHome, AccountHome.class);
       }
    catch (NamingException e) { // Error getting the home interface
    ...
    }

用 corbaname URL 进行 JNDI 查询

有时,corbaname 作为查询名称可能是很有用的。例如,如果目标对象不是联合名称空间的成员,且无法使用限定名进行查找,那么可以使用 corbaname 方便地查找对象。

以下示例说明如何使用 corbaname URL 来查询 EJB 业务接口。
// Get the initial context as shown in a previous example. 
... 
// Look up the business interface using a corbaname URL. 
try { 
   java.lang.Object ejbBusIntf = initialContext.lookup(
      "corbaname:iiop:someHost:2809#com/mycompany/accounting/Account"); 
   accountIntf =
     (Account)javax.rmi.PortableRemoteObject.narrow(ejbBusIntf, Account.class); 
} 
catch (NamingException e) { // Error getting the business interface 
   ... 
} 

以下示例说明如何使用 corbaname URL 进行查询。

// Get the initial context as shown in a previous example 
... 
// Look up the home interface using a corbaname URL 
try { 
   java.lang.Object ejbHome = initialContext.lookup(
      "corbaname:iiop:someHost:2809#com/mycompany/accounting/AccountEJB"); 
   accountHome = (AccountHome)javax.rmi.PortableRemoteObject.narrow( 
      (org.omg.CORBA.Object) ejbHome, AccountHome.class); 
} 
catch (NamingException e) { // Error getting the home interface 
   ... 
} 

指示主题类型的图标 参考主题



时间戳记图标 最近一次更新时间: last_date
http://www14.software.ibm.com/webapp/wsbroker/redirect?version=cord&product=was-nd-mp&topic=rnam_example_prop3
文件名:rnam_example_prop3.html