示例:从 WS-Notification 拉出点获取消息
使用此任务为充当拉出样式用户角色的 JAX-RPC 客户机编写代码(根据提供的示例代码摘录),请求来自拉出点的消息。
关于此任务
本示例的依据是,将 Java™ API for XML-based remote procedure calls (JAX-RPC) API 与使用 WSDL2Java 工具生成的代码(此代码针对创建 WS-Notification 服务点时生成的通知代理 WSDL 运行)以及 WebSphere® Application Server API 和 SPI 配合使用。
在 WebSphere Application Server 中,WS-Notification 服务有两个实现:V6.1 和 V7.0。此 JAX-RPC 示例可与 V6.1 或 V7.0 WS-Notification 服务点成功交互。 但是,如果要将 WS-Notification 与策略集配合使用(例如,启用与 WS-ReliableMessaging 的组合),那么 WS-Notification 应用程序必须编码为使用 Java API for XML-based Web Services (JAX-WS) 编程模型,并且必须与 V7.0 WS-Notification 服务点交互。 如果您不熟悉 JAX-WS 客户机应用程序编程,请参阅下列主题:
要为充当拉出样式用户角色的 JAX-RPC 客户机编写代码(请求来自拉出点的消息),请完成以下步骤并参阅示例代码摘录以了解进一步信息。
过程
- 查找 JAX-RPC 服务。 JNDI 名称特定于您的 Web Service 客户机信息。
- 获取要对其调用操作的端口的存根。
- 使请求与拉出点相关联。 pullPointEPR 是调用 CreatePullPoint 操作返回的 EndpointReference。
- 指定要检索的消息数。
- 创建任何可选信息。
- 创建请求信息。
- 通过对存根调用关联方法来调用 GetMessages 操作。
- 获取从响应返回的消息。
示例
以下示例代码描述以拉出样式使用者角色执行操作的 JAX-RPC 客户机从拉出点请求消息:
// Look up the JAX-RPC service. The JNDI name is specific to your web services client implementation
InitialContext context = new InitialContext();
javax.xml.rpc.Service service = (javax.xml.rpc.Service) context.lookup(
"java:comp/env/services/NotificationBroker");
// Get a stub for the port on which you want to invoke operations
NotificationBroker stub = (NotificationBroker) service.getPort(NotificationBroker.class);
// Associate the request with a pull point. The pullPointEPR is the EndpointReference returned
// from invoking the CreatePullPoint operation
((Stub) stub)._setProperty(WSAConstants.WSADDRESSING_DESTINATION_EPR, pullPointEPR);
// Specify the number of messages you want to retrieve
Integer numberOfMessages = new Integer(2);
// Create any optional information
SOAPElement[] optionalInformation = new SOAPElement[] {};
// Create the request information
GetMessages request = new GetMessages(numberOfMessages, optionalInformation);
// Invoke the GetMessages operation by calling the associated method on the stub
GetMessagesResponse response = stub.getMessages(request);
// Get the messages returned from the response
NotificationMessage[] messages = response.getMessages();