apache > ws.apache

## Apache Muse - Receive Any Notification Message

If you add the WSN NotificationConsumer capability to your resource type, you can use it to filter incoming notification messages and execute code in response to them. There are three steps:

- Subscribe your consumer resource with the notification producer using the NotificationProducerClient class. This will start the flow of notifications to your consumer resource.
- The consumer resource must create one or more NotificationMessageListener objects; these listener objects will be fired each time the resource receives a message through the WSN Notify operation.
- Register the listener objects with the NotificationConsumer capability.

The sample code below illustrates all three steps. It creates a listener for notifications that come from a specific WSN topic; upon receiving a new notification, the NotificationConsumer capability will ask the listener if it wants to process the message (via accepts()), and if so, allows it to do so (via process()). Our listener extracts the message payload from notifications that have the right topic and prints the payload to the console.

> `import javax.xml.namespace.QName;
> import org.w3c.dom.Element;
> import org.apache.muse.ws.addressing.EndpointReference;
> import org.apache.muse.ws.addressing.soap.SoapFault;
> import org.apache.muse.ws.addressing.soap.SoapFault;
> import org.apache.muse.ws.dm.muws.events.WefConstants;
> import org.apache.muse.ws.notification.NotificationConsumer;
> import org.apache.muse.ws.notification.NotificationMessage;
> import org.apache.muse.ws.notification.NotificationMessageListener;
> import org.apache.muse.ws.notification.WsnConstants;
> import org.apache.muse.ws.notification.impl.TopicFilter;
> import org.apache.muse.ws.notification.remote.NotificationProducerClient;
> public class MyCapability extends AbstractCapability
> {
> public void initialize()
> throws SoapFault
> {
> super.initialize();
> QName topicName = new QName("http://example.com/server-product", "ServerUpdates");
> //
> // Step 1: suscribe ourselves with the producer resource
> //
> EndpointReference producerEPR = ... // you fill in the producer's EPR
> EndpointReference myEPR = getResource().getEndpointReference();
> NotificationProducerClient producer = new NotificationProducerClient(, myEPR);
> producer.subscribe(myEPR, new TopicFilter(topicName), null);
> //
> // Step 2: create a listener for the topic we're interested in
> //
> MyListener listener = new MyListener(topicName);
> //
> // Step 3: register our listener so it can receive incoming notifications
> //
> NotificationConsumer consumerCap = (NotificationConsumer)getResource().getCapability(WsnConstants.CONSUMER_URI);
> consumerCap.addMessageListener(listener);
> }
> private class MyListener implements NotificationMessageListener
> {
> private QName _myTopicName = null;
> public MyListener(QName topicName)
> {
> _myTopicName = topicName;
> }
> public boolean accepts(NotificationMessage message)
> {
> QName topicName = message.getTopic();
> return _myTopicName.equals(topicName);
> }
> public void process(NotificationMessage message)
> {
> Element wefEvent = message.getMessagContent(WefConstants.MGMT_EVENT_QNAME);
> System.out.println("Here is the WEF Event:\n");
> System.out.println(XmlUtils.toString(wefEvent));
> }
> }
> }
> `