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 using the WSDL2Java tool (run against the Notification Broker WSDL generated as a result of creating the WS-Notification service point) and WebSphere Application Server APIs and SPIs.
In WebSphere Application Server there are two implementations of the WS-Notification service: Version 6.1 and Version 7.0. This JAX-RPC example can interact successfully with Version 6.1 or Version 7.0 WS-Notification service points. However to use WS-Notification with policy sets, for example to enable composition with WS-ReliableMessaging, then the WS-Notification applications must be encoded to use the Java API for XML-based Web Services (JAX-WS) programming model and must interact with Version 7.0 WS-Notification service points. If we are new to programming JAX-WS client applications, see the following topics:
- JAX-WS
- JAX-WS client programming model
- Implement static JAX-WS web services clients
- Writing JAX-WS applications for WS-Notification
- Web services hints and tips: JAX-RPC versus JAX-WS, Part 1
The article Writing 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... referring to the example code extract for further information.
- Look up the JAX-RPC service. The JNDI name is specific to the web services client implementation.
- Get a stub for the port on which 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 the 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);
Related concepts
JAX-RPC Brokered notification JAX-RPC WS-Notification
Related tasks
Writing a WS-Notification application that exposes a web service endpoint Writing a WS-Notification application that does not expose a web service endpoint Filtering the message content of publications Example: Subscribing a WS-Notification consumer Example: Pausing a WS-Notification subscription Example: Create a WS-Notification pull point Example: Getting messages from a WS-Notification pull point Example: Registering a WS-Notification publisher Example: Create a Notification consumer web service skeleton Use WS-Notification for publish and subscribe messaging for web services Secure WS-Notification
WSDL2Java command for JAX-RPC applications WSDL2Java command for JAX-RPC applications WS-Notification troubleshooting tips
WS-BrokeredNotification Version 1.3 OASIS Standard
Writing JAX-WS applications for WS-Notification