Network Deployment (Distributed operating systems), v8.0 > Develop and deploying applications > Develop web services - Notification (WS-Notification) > Develop applications that use WS-Notification


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.

This example is based on using the Java API for XML-based remote procedure calls (JAX-RPC) APIs with code generated by using the WSDL2Java tool (run against the Notification Broker WSDL generated as a result of creating your WS-Notification service point) and WAS APIs and SPIs.

In WAS there are two implementations of the WS-Notification service: v6.1 and v7.0. This JAX-RPC example can interact successfully with v6.1 or v7.0 WS-Notification service points. However to use WS-Notification with policy sets, for example to enable composition with WS-ReliableMessaging, then your WS-Notification applications must be encoded to use the Java API for XML-based Web Services (JAX-WS) programming model and must interact with v7.0 WS-Notification service points. If you are new to programming JAX-WS client applications, see the following topics:

The article Write JAX-WS applications for WS-Notification includes an example of a JAX-WS publisher client application.

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

  1. Look up the JAX-RPC service. The JNDI name is specific to your web services client implementation.
  2. Get a stub for the port on which to invoke operations.
  3. Create the message contents for a notification message.
  4. Create a notification message from the contents.

  5. Add a topic expression to the notification message. The topic expression must indicate to which topic or topics the message corresponds.
  6. Create any optional information.

  7. 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.

  8. 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 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) {
    // We 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.
We 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);

JAX-RPC
Brokered notification
JAX-RPC
WS-Notification
Use WS-Notification for publish and subscribe messaging for web services
Secure WS-Notification


Related


WSDL2Java command for JAX-RPC applications
WSDL2Java command
WS-Notification troubleshooting tips

+

Search Tips   |   Advanced Search