Example: Publishing a WS-Notification message
Use this task to write the code for a publisher client application that can publish a notification message to a broker, based on the example code extract provided.
About this task
這個範例的基礎如下:搭配 WSDL2Java 工具產生的程式碼(針對建立 WS-Notification 服務點所產生的「通知分配管理系統 WSDL」來執行這個工具)及 WebSphere® Application Server API 和 SPI,來使用 Java™ API for XML 型遠端程序呼叫 (JAX-RPC) API。
In WebSphere Application Server there are two implementations
of the WS-Notification service: Version 6.1 and Version 7.0. 這個 JAX-RPC 範例可以與 6.1 版或 7.0 版 WS-Notification 服務點順利互動。 不過,如果您想要搭配原則集來使用 WS-Notification,例如,能夠與 WS-ReliableMessaging 組合起來,您的 WS-Notification 應用程式就必須編碼為使用 Java API for XML 型 Web 服務 (JAX-WS) 程式設計模型,且必須與 7.0 版 WS-Notification 服務點互動。 如果您還不熟悉如何撰寫 JAX-WS 用戶端應用程式,請參閱下列主題:
- JAX-WS
- JAX-WS client programming model
- Implementing static JAX-WS web services clients
- 撰寫 WS-Notification 的 JAX-WS 應用程式
- Web 服務提示和要訣:JAX-RPC 和 JAX-WS,第 1 篇
To write the code for a publisher client application that can publish a notification message to a broker, complete the following steps, referring to the example code extract for further information.
Procedure
- Look up the JAX-RPC service. The JNDI name is specific to your web services client implementation.
- Get a stub for the port on which you want to invoke operations.
- Create the message contents for a notification message.
- Create a notification message from the contents.
- Add a topic expression to the notification message. The topic expression must indicate to which topic or topics the message corresponds.
- Create any optional information.
- Optional: If the broker requires publisher client applications to register, associate the request with a particular publisher registration. The registrationEPR is the ConsumerReference EndpointReference returned by the broker in relation to an invocation of the RegisterPublisher operation.
- Invoke the Notify operation by calling the associated method on the stub.
Example
The following example code represents a publisher client application that can publish a notification message to a broker:
// 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);