创建使用 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,请完成此任务。
过程
结果
提供返回对目标服务的端点引用的 web service 接口
以下示例对应于过程中的步骤 1 到 4。这些示例显示 IT 组织可能如何使用 Web service 来管理打印机网络。该组织可将每个打印机表示为可通过端点引用来寻址的一个资源。以下示例显示了如何使用由 WebSphere Application Server 提供的 IBM 专有的 Web Service 寻址 (WS-Addressing) 应用程序编程接口 (API) 和 JAX-WS 来编写此类服务。
该 IT 组织实现了提供 CreatePrinter portType 元素的 PrinterFactory 服务。此 portType 元素接受 CreatePrinterRequest 消息以创建表示逻辑打印机的资源,并使用端点引用(对该资源的引用)作出响应。
<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 方法获取的打印机身份将消息转发到相应的打印机实例。
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 寻址支持。