创建使用 Web service 寻址的 JAX-RPC Web Service 应用程序

Web Service 寻址 (WS-Addressing) 通过定义用来对 Web Service 进行寻址和在消息中提供寻址信息的标准方法,帮助实现 Web Service 之间的互操作性。本任务描述了创建使用 WS-Addressing 端点引用访问的 JAX-RPC web service 所需的步骤。该任务还描述了其他步骤,在将有状态资源用作 web service 的一部分时需要执行这些步骤。

开始之前

本任务中描述的步骤适用于在 WebSphere® Application Server 上运行的服务器和客户机。

关于此任务

如果要创建使用 WS-Addressing 规范的 web service,请完成此任务。

过程

  1. 通过为将端点引用返回到目标服务的 web service 创建或生成 Web Service 描述语言 (WSDL) 文档来提供 web service 接口。 该接口必须返回端点引用,您可以通过使用工厂操作或单个的工厂服务来完成本任务。目标服务可以面向资源实例,例如,购物车。
  2. 实现在前一步骤中创建的 web service。对于实现的 WS-Addressing 部分,请完成以下步骤:
    1. 通过遵循使用 IBM 专有 Web Service 寻址 API 来创建端点引用中的指示信息来创建引用 Web Service 的端点引用。
    2. 可选: 如果接口包括面向资源实例的 Web Service,请创建或查找该资源实例。
    3. 可选: 如果要使用资源实例,请获取该资源的标识,并通过使用 EndpointReference.setReferenceParameter(QName resource_id_name, String value) 方法来使它与作为引用参数的端点引用相关联。 该资源标识依赖于应用程序,可能是在资源实例的创建期间生成的。
      注意: 由于将在 SOAP 消息中传播标识,所以不要将敏感信息放在资源标识中。
      现在,端点引用确定了目标资源。
    4. 返回端点引用。
  3. 如果 web service 使用了资源实例,那么扩展实现以使入局消息与适当的资源实例匹配。 因为已使该资源标识与先前创建的端点引用相关联,所以在该端点引用上确定的任何目标入局消息都将包含资源标识信息,作为消息 SOAP 头中的引用参数。因为在 SOAP 头中传递资源标识,所以不必在 Web Service 接口上公开资源标识。当 WebSphere Application Server 收到消息时,它会将此信息放入线程的消息上下文中。请扩展该实现以执行以下操作:
    1. 通过使用 EndpointReferenceManager.getReferenceParameterFromMessageContext(QName resource_id_name) 方法来从消息上下文中获取资源实例标识。
    2. 将消息转发至适当的资源实例。
  4. 要配置与服务通信的客户机,请使用在第一步中由服务产生的端点引用将消息发送至端点。
    1. 通过在 Java™ 命名和目录接口 (JNDI) 中查找服务来获取 Stub 对象或创建空的 Call 对象。
    2. 将端点引用与代理对象相关联。 使用 Stub 或 Call 对象的 setProperty(String property_name, Object value) 方法。将 WS-Addressing 常量 WSADDRESSING_DESTINATION_EPR 用作属性名,并将端点引用用作该值。
      此过程将自动配置 Stub 或 Call 对象,以表示端点引用的 web service(如果接口使用位于资源实例前面的 web service,那么表示资源实例)。对于 Call 对象,此过程包括与端点引用相关联的接口和端点元数据(portType 和 port 元素)配置。
      注: 如果端点引用的元数据与出站消息所关联的信息冲突(例如,将 Stub 对象配置为表示不同接口),那么在尝试调用端点时将抛出 javax.xml.rpc.JAXRPCException 异常。

    对 Stub 或 Call 对象的调用现在以端点引用定义的 web service 或资源实例为目标。 当调用发生时,产品会将相应的消息寻址属性(例如,标识目标资源的端点引用中包含的引用参数)添加到消息头。

结果

已配置 web service 和客户机,以通过 WS-Addressing 支持来使用端点引用。

提供返回对目标服务的端点引用的 web service 接口

以下示例对应于过程中的步骤 1 到 4。这些示例显示 IT 组织可能如何使用 Web service 来管理打印机网络。该组织可将每个打印机表示为可通过端点引用来寻址的一个资源。以下示例显示了如何使用由 WebSphere Application Server 提供的 IBM 专有的 Web Service 寻址 (WS-Addressing) 应用程序编程接口 (API) 和 JAX-WS 来编写此类服务。

该 IT 组织实现了提供 CreatePrinter portType 元素的 PrinterFactory 服务。此 portType 元素接受 CreatePrinterRequest 消息以创建表示逻辑打印机的资源,并使用端点引用(对该资源的引用)作出响应。

这种 PrinterFactory 服务的 WSDL 定义可包含以下代码:
<wsdl:definitions targetNamespace="http://example.org/printer" ...
                  xmlns:pr=" http://example.org/printer">
  <wsdl:types>
    ...
    <xsd:schema...>
      <xsd:element name="CreatePrinterRequest"/>
      <xsd:element name="CreatePrinterResponse" 
                   type="wsa:EndpointReferenceType"/>
    </xsd:schema>
  </wsdl:types>
  <wsdl:message name="CreatePrinterRequest">
    <wsdl:part name="CreatePrinterRequest" 
               element="pr:CreatePrinterRequest" />
  </wsdl:message>
  <wsdl:message name="CreatePrinterResponse">
    <wsdl:part name="CreatePrinterResponse" 
               element="pr:CreatePrinterResponse" />
  </wsdl:message>
  <wsdl:portType name="CreatePrinter">
    <wsdl:operation name="createPrinter">
      <wsdl:input name="CreatePrinterRequest" 
                  message="pr:CreatePrinterRequest" />
      <wsdl:output name="CreatePrinterResponse"
                  message="pr:CreatePrinterResponse" />
    </wsdl:operation>
  </wsdl:portType>
</wsdl:definitions>

上述示例中的 CreatePrinter 操作返回表示新创建的打印机资源的 wsa:EndpointReference 对象。客户机可以使用此端点引用将消息发送至表示该打印机的服务实例。

以下示例中显示的 createPrinter 方法创建对打印机服务的端点引用。然后,该操作将获取各打印机资源实例的标识,并将标识与端点引用相关联。最后,createPrinter 方法将现在表示新打印机的 EndpointReference 对象转换为 W3CEndpointReference 对象,并返回转换的端点引用。

import com.ibm.websphere.wsaddressing.EndpointReferenceManager;
import com.ibm.websphere.wsaddressing.EndpointReference;
import com.ibm.websphere.wsaddressing.jaxws.EndpointReferenceConverter;
import com.ibm.websphere.wsaddressing.jaxws.W3CEndpointReference;

import javax.xml.namespace.QName;

public class MyClass {

    // Create the printer
    ...

    // Define the printer resource ID as a static constant as it is required in later steps
    public static final QName PRINTER_ID_PARAM_QNAME = new QName("example.printersample",
                                                                 "IBM_WSRF_PRINTERID", "ws-rf-pr" ); 
    public static final QName PRINTER_SERVICE_QNAME = new QName("example.printer.com", "printer", "..."); 
    public static final String PRINTER_ENDPOINT_NAME = new String("PrinterService");
    
    public W3CEndpointReference createPrinter(java.lang.Object createPrinterRequest)
    throws Exception {
     // Create an EndpointReference that targets the appropriate WebService URI and port name.
     EndpointReference epr =  EndpointReferenceManager.createEndpointReference(PRINTER_SERVICE_QNAME, 
                                                                               PRINTER_ENDPOINT_NAME);

     // Create or lookup the stateful resource and derive a resource
     // identifier string.
     String resource_identifier = "...";

     // Associate this resource identifier with the EndpointReference as  
     // a reference parameter.
     // The choice of name is arbitrary, but should be unique     
     // to the service.
     epr.setReferenceParameter(PRINTER_ID_PARAM_QNAME,resource_identifier);
     // The endpoint reference now targets the resource rather than the service.
     ...

     return EndpointReferenceConverter.createW3CEndpointReference(epr);
    }
}

因为先前描述的 web service 实现,现在,打印机资源实例将一个唯一标识嵌入在其端点引用中。此标识成为引用参数显示在目标为 web service 的后续消息的 SOAP 头中,web service 可以使用该标识将入局消息与相应的打印机相匹配。

web service 收到一则包含 WS-Addressing 消息寻址属性的消息后,WebSphere Application Server 将在该消息被分派至应用程序端点之前对这些属性进行处理,并将它们放置到线程上的消息上下文中。打印机 web service 应用程序可访问与 WebServiceContext 对象中的目标端点相关联的引用参数,如以下示例所示:

import com.ibm.websphere.wsaddressing.EndpointReferenceManager; 
...
 // Initialize the reference parameter name
 QName name = new QName(..);
 // Extract the String value.
 String resource_identifier = 
        EndpointReferenceManager.getReferenceParameterFromMessageContext(PRINTER_ID_PARAM_QNAME);

web service 实现可根据从 getReferenceParameterFromMessageContext 方法获取的打印机身份将消息转发到相应的打印机实例。

客户机为打印机创建 JAX-WS 代理并将该代理转换为 BindingProvider 对象。然后,客户机将先前获取的 EndpointReference 对象与 BindingProvider 对象的请求上下文相关联,如以下示例中所示。
import javax.xml.ws.BindingProvider;
...

 javax.xml.ws.Service service= ...;
 Printer myPrinterProxy = service.getPort(portName, Printer.class);
	
 javax.xml.ws.BindingProvider bp = (javax.xml.ws.BindingProvider)myPrinterProxy;
	 
 // Retrieve the request context for the BindingProvider object
 Map myMap = myBindingProvider.getRequestContext();  

 // Associate the endpoint reference that represents the new printer to the request context 
 // so that the BindingProvider object now represents a specific printer instance.
 myMap.put(WSADDRESSING_DESTINATION_EPR, destinationEpr);

...
现在,该 BindingProvider 对象表示新的打印机资源实例,并且客户机可以使用该对象通过打印机 web service 将消息发送至打印机。当客户机调用该 BindingProvider 对象时,WebSphere Application Server 会将相应的消息寻址属性添加至消息头,在此情况下为包含在标识目标打印机资源的端点引用中的引用参数。

或者,客户机可使用 JAX-RPC Stub 或 Call 对象,该对象由客户机配置为表示新的打印机。Call 对象的使用情况如以下示例中所示。

import javax.xml.rpc.Call;
...
 :
 // Associate the endpoint reference that represents the new printer with the call.
 call.setProperty(
        "com.ibm.websphere.wsaddressing.WSAConstants.
                             WSADDRESSING_DESTINATION_EPR ", epr);

从客户机的角度看,端点引用是不透明的。客户机无法解释任何端点引用参数的内容,因此不应该尝试以任何方式使用这些内容。因为引用参数是服务提供程序私有的,所以客户机无法直接创建端点引用的实例;客户机必须从服务提供程序获取端点引用(例如通过提供程序工厂服务),然后使用这些端点引用将 web service 操作定向至由端点引用表示的端点,如上所示。

下一步做什么

  • 请参阅 Web Service 寻址安全性,以了解有关 WS-Addressing 安全性的信息。
  • 部署应用程序。对于此情况,由于在客户机上指定了 WS-Addressing 属性,因此不必执行任何其他步骤就可在 WebSphere Application Server 中启用 WS-Addressing 支持。有关更多信息以及可能需要其他步骤的其他情况,请参阅对 JAX-RPC 应用程序启用 Web Service 寻址支持

指示主题类型的图标 任务主题



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