示例:发布 WS-Notification 消息

使用此任务以根据提供的示例代码摘录为发布者客户机应用程序编写可向代理发布通知消息的代码。

关于此任务

本示例的依据是,将 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 客户机应用程序编程,请参阅下列主题: 为 WS-Notification 编写 JAX-WS 应用程序一文包括 JAX-WS 发布者客户机应用程序的示例。

要为发布者客户机应用程序编写可向代理发布通知消息的代码,请完成以下步骤(参阅示例代码摘录以了解进一步信息)。

过程

  1. 查找 JAX-RPC 服务。 JNDI 名称特定于您的 Web Service 客户机信息。
  2. 获取要对其调用操作的端口的存根。
  3. 为通知消息创建消息内容。
  4. 根据内容创建通知消息。
  5. 向通知消息添加主题表达式。主题表达式必须指示该消息对应的主题。
  6. 创建任何可选信息。
  7. 可选: 如果代理要求发布者客户机应用程序注册,请使该请求与特定发布者注册相关联。 registrationEPR 是与 RegisterPublisher 操作的调用相关的代理返回的 ConsumerReference EndpointReference。
  8. 通过对存根调用关联方法来调用通知操作。

示例

以下示例代码表示可将通知消息发布至代理的发布者客户机应用程序:

// 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);

// Create the message contents for a notification message
SOAPElement messageContents = null;
javax.xml.soap.SOAPFactory soapFactory = javax.xml.soap.SOAPFactory.newInstance();
if (soapFactory instanceof IBMSOAPFactory) {
    // You can use the value add methods provided by the IBMSOAPFactory API to create the SOAPElement
    // from an XML string.
    String messageContentsXML = "<xyz:MyData xmlns:xyz=\"uri:mynamespace\">Some data</xyz:MyData>";        
    messageContents = ((IBMSOAPFactory) soapFactory).createElementFromXMLString(messageContentsXML);
} else {
    // Build up the SOAPElement using the standard javax.xml.soap APIs
    messageContents = soapFactory.createElement("MyData", "xyz", "uri:mynamespace");
    messageContents.addTextNode("Some data");
}

// Create a notification message from the contents
NotificationMessage message = new NotificationMessage(messageContents);               

// Add a topic expression to the notification message indicating to which topic or topics the
// message corresponds
Map prefixMappings = new HashMap();
prefixMappings.put("abc", "uri:example");
TopicExpression exp = 
    new TopicExpression(TopicExpression.SIMPLE_TOPIC_EXPRESSION, "abc:ExampleTopic", prefixMappings);        
message.setTopic(exp);

// Create any optional information
SOAPElement[] optionalInformation =  new SOAPElement[] {};
        
/*
Optional
--------
The following line will cause the request to be associated with a particular publisher registration.
You must do this if the broker requires publishers to register. The registrationEPR is the
ConsumerReference EndpointReference returned by the broker in relation to an invocation of the 
RegisterPublisher operation.
        
    ((Stub) stub)._setProperty(WSAConstants.WSADDRESSING_DESTINATION_EPR, consumerReferenceEPR);
*/

// Invoke the Notify operation by calling the associated method on the stub
stub.notify(new NotificationMessage[] { message }, optionalInformation);

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



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