示例:用 CosNaming 查找 EJB home
您可以通过 CORBA CosNaming 接口从 WebSphere® Application Server 名称服务器中查找 EJB 主对象或其他 CORBA 对象。
可以在初始上下文中调用 resolve 或 resolve_str,也可以在 ORB 上调用 string_to_object。您可以使用限定名,以使得不管是否在执行名称服务器查找时都能解析名称,或者使用仅从实际上包含对象绑定的名称服务器上的服务器根上下文解析的未限定名。(限定名遍历联合系统名称空间到达指定的服务器根上下文。)
限定名和未限定名
每个应用程序服务器都包含名称服务器。在该名称服务器中绑定系统工件(例如 EJB home)。根据系统名称空间结构联合不同的名称服务器。建议使用限定名来查找不同服务器上的对象。
限定名可以是基于拓扑的名称,并且它基于集群或包含对象的单个服务器和节点的名称。
您可以为对象定义固定的限定名。使用限定名,您可以遍历系统名称空间结构,从同一初始上下文中查找驻留在不同服务器上的对象。另外,您还可以使用未限定名,但是未限定名将只能使用与该对象的应用程序服务器关联的名称服务器进行解析。
CosNaming.resolve(和 resolve_str)与 ORB.string_to_object
如果在 WebSphere Application Server 单元中有来自任何名称服务器的初始上下文,那么可以使用限定名来查找任何 CORBA 对象。对于目标对象的名称服务器,您不需要其他主机和端口信息。
另外,还可以通过在 ORB 上调用 string_to_object 并传入 corbaname URL 来查找对象。通常,指定一个 IIOP 类型 URL,因此该 URL 中必须包含初始上下文所需的引导地址信息。您可以使用限定或未限定的字符串型名称,但是仅当初始上下文是来自绑定对象的名称服务器时才解析未限定名。
下面的示例显示使用限定的基于拓扑的查找名和未限定的查找名的 CosNaming 解析操作。
使用限定名的 CosNaming 解析操作
对象的基于拓扑的限定名是根据对象是绑定在单个服务器还是服务器集群中。下面是每种情况的示例。
单个服务器
下面的示例显示查找在单个服务器中运行的 EJB home。要查找的企业 Bean 在节点 Node1 的服务器 MyServer 上运行。
// Get the initial context as shown in the 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 name under which the EJB home is bound
org.omg.CORBA.Object ejbHome = initialContext.resolve_str(
"cell/nodes/Node1/servers/MyServer/mycompany/accounting/AccountEJB");
accountHome =
(AccountHome)javax.rmi.PortableRemoteObject.narrow(ejbHome, AccountHome.class);
服务器集群
下面的示例显示查找在集群中运行的 EJB home。要查找的企业 Bean 在集群 Cluster1 中运行。如果有任何的集群成员在运行,那么可以解析名称。
// Get the initial context as shown in the 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 name under which the EJB home is bound
org.omg.CORBA.Object ejbHome = initialContext.resolve_str(
"cell/clusters/Cluster1/mycompany/accounting/AccountEJB");
accountHome =
(AccountHome)javax.rmi.PortableRemoteObject.narrow(ejbHome, AccountHome.class);
使用未限定的字符串型名称的 ORB string_to_object 操作
如果在包含对象的名称服务器上执行解析操作,那么无需遍历系统名称空间,并且您可以使用非限定的查询名称。注意,不在其他名称服务器上解析此名称。如果提供未限定名,那么对象键必须是 NameServiceServerRoot,以选择正确的初始上下文。如果提供限定名,那么可以使用 NameService 的缺省键。
下面的示例显示查找 EJB home。要查找的企业 Bean 绑定在端口 2809 的主机 myHost 上运行的名称服务器上。注意 NameServiceServerRoot 的对象键。
// Assume orb is an existing ORB instance
...
// Look up the home interface using the name under which the EJB home is bound
org.omg.CORBA.Object ejbHome = orb.string_to_object(
"corbaname:iiop:myHost:2809/NameServiceServerRoot#mycompany/accounting");
accountHome =
(AccountHome)javax.rmi.PortableRemoteObject.narrow(ejbHome, AccountHome.class);