使用 WSIF 将 JNDI 引用绑定到 Web Service
可使用 Web Service 调用框架 (WSIF) 以将引用绑定到 Web Service,然后使用 JNDI 查询该引用。
关于此任务
可通过此服务的 Web 服务描述语言 (WSDL) 文档中提供的信息,访问 Web Service。如果您不知道在何处查找此服务的 WSDL 文档,但知道它已在 UDDI 注册中心中注册,那么在此注册中心中查找服务。Java™ 程序以类似的方式访问 Java 对象和资源,但使用 JNDI 接口。
以下步骤中的代码片段说明如何使用 WSIF 将引用绑定到 Web Service,然后使用 JNDI 查询引用。
过程
- 指定 Web Service 的自变量值。
Web Service 在 WSIF 中由 org.apache.wsif.naming.WSIFServiceRef 类的实例表示。此样本可引用对象具有以下构造函数:
public WSIFServiceRef( String WSDL, String sNS, String sName, String ptNS, String ptName) { [...] }
在本示例中- WSDL 是包含服务定义的 WSDL 文件的位置。
- sNS 是服务定义的全名称空间(如果 WSDL 文件中只定义了一个服务,那么可指定 null)。
- sName 是服务定义的局部名(如果 WSDL 文件中只定义了一个服务,那么可指定 null)。
- ptNS 是您要使用的服务中端口类型的全名称空间(如果只有一个端口类型可用于此服务,那么可指定 null)。
- ptName 是端口类型的局部名(如果只有一个端口类型可用于此服务,那么可指定 null)。
例如,可以从 Web 地址 http://myServer/WSDL/Example.WSDL 获得 Web Service 的 WSDL 文件,它包含下列服务和端口类型定义:
您可为 WSIFServiceRef 类指定以下自变量值:<definitions targetNamespace="http://hostname/namespace/example" xmlns:abc="http://hostname/namespace/abc" [...] <portType name="ExamplePT"> <operation name="exampleOp"> <input name="exampleInput" message="tns:ExampleInputMsg"/> </operation> </portType> [...] <service name="abc:ExampleService"> [...] </service> [...] </definitions>
- WSDL 是 http://myServer/WSDL/Example.WSDL
- sNS 是 http://hostname/namespace/abc
- sName 是 ExampleService
- ptNS 是 http://hostname/namespace/example
- ptName 是 ExamplePT
- 使用 JNDI 绑定服务。
要使用 JNDI 将服务引用绑定在命名目录中,您可在 WebSphere® Application Server 中使用 com.ibm.websphere.naming.JndiHelper 类:
[...] import com.ibm.websphere.naming.JndiHelper; import org.apache.wsif.naming.*; [...] try { Context startingContext = new InitialContext(); WSIFServiceRef ref = new WSIFServiceRef(“http://myServer/WSDL/Example.WSDL”, “http://hostname/namespace/abc” “ExampleService”, “http://hostname/namespace/example”, “ExamplePT”); JndiHelper.recursiveRebind(startingContext, “myContext/mySubContext/myServiceRef”, ref); } catch (NamingException e) { // Handle error. } [...]
- 使用 JNDI 查询服务。
以下代码片段显示了使用 JNDI 查找服务:
[...] try { [...] InitialContext ic = new InitialContext(); WSIFService myService = (WSIFService) ic.lookup(“myContext/mySubContext/myServiceRef”); [...] } catch (NamingException e) { // Handle error. } [...]
相关概念:


http://www14.software.ibm.com/webapp/wsbroker/redirect?version=cord&product=was-nd-mp&topic=twsf_jndi
文件名:twsf_jndi.html