apache > ws.apache
Pubscribe
 

Notification Consumer

Introduction

This section provides instructions for creating a notification consumer resource. A service that acts as a notification consumer (implements the WSN NotificationConsumer portType) can receive SOAP notification messages sent by a notification producer. The consumer only receives messages for notifications to which it is subscribed.

The Wsdl2Java tool generates the same artifacts for a notification consumer as it does for a notification producer. In particular, the service, resource, and home class are generated but need to be modified specifically for your consumer. The topics in this section describe how to modify these classes. An example notification consumer that works together with the filesystem example is provided in the INSTALL_DIR/examples/consumer and discussed below. Initially, you should model your consumer based on the example to ensure that you write a valid consumer.

Note
A consumer WSDL must implement the Notify operation from the NotificationConsumer portType. If you use the INSTALL_DIR/template/_TEMPLATE_.wsdl to create the WSDL, this operation is included and only needs to be uncommented. A WSDL is included in the consumer example. To use this example, run the Wsdl2Java tool on the WSDL.

Modify the Service Class

An abstract method Notify is generated in the abstractService class an needs to be implemented in your service class. The implementation of the method is dependant on whatever features you want to include for your consumer. In the consumer example, a property is used to retrieve the last notification message. In this case, the notify method will allow a JSP page to retrieve and display the current notification message. Notice that the service class is also used to setup and initialize the LASTMESSAGE resource property. Therefore, the the resource class will not need to perform these operations. The method is implemented as follows:

public void notify(org.oasisOpen.docs.wsn.x2004.x06.wsnWSBaseNotification12Draft01.NotifyDocument notifyDocument)
    {
        //get the property set
        org.apache.ws.resource.properties.ResourcePropertySet resourcePropertySet = ((org.apache.ws.resource.PropertiesResource)getResource()).getResourcePropertySet();

        //get the property
        org.apache.ws.resource.properties.ResourceProperty resourceProperty = resourcePropertySet.get(ConsumerPropertyQNames.LASTMESSAGE);
        resourceProperty.clear();//clear old notifs
        
        //add the new entry
        //build the LastMessage type for updating this resoruce property
        LastMessageDocument lastMessageDocument = LastMessageDocument.Factory.newInstance();
        org.apache.ws.resource.example.notifConsumer.LastMessageType lastMessageType = lastMessageDocument.addNewLastMessage();
        lastMessageType.setOccurred(java.util.Calendar.getInstance());
        org.apache.xmlbeans.XmlObject lastMessageNotif = lastMessageType.addNewNotification();
        org.apache.ws.util.XmlBeanUtils.addChildElement(lastMessageNotif, notifyDocument);

        //add the LastMessageDocument to the resource property
        resourceProperty.add(lastMessageDocument);
    }

Modify the Resource Class

The resource class is used to setup and initialize any resource properties. For the consumer example, the resource class is not used to setup and initialize the LASTMESSAGE resource property, but instead the service class is used to perform these operations. If you choose to use the resource class, you should update the init() method to add your properties to the ResourcePropertySet. The following sample code is required for the consumer example. Below the consumer example code is an example of setting up and initializing resource properties in the resource class.

public void init()
    {
        super.init();               
    }
   			

Generic example of setting and initialing resource properties.

public void init()
    {
        super.init();               

        /**
		 * The ResourcePropertySet which contains all the defined ResourceProperties
		 */
		org.apache.ws.resource.properties.ResourcePropertySet resourcePropertySet = getResourcePropertySet();
		org.apache.ws.resource.properties.ResourceProperty resourceProperty = null;


	try{	
		// init the {http://ws.apache.org/resource/example/NotifConsumer}YourResourceProperty Property
		resourceProperty = resourcePropertySet.get(ConsumerPortPropertyQNames.YourResourceProperty);
		
		}
	catch (Exception e)
	{
	   throw new javax.xml.rpc.JAXRPCException("There was a problem in initializing your resource properties.  Please check your init() method. Cause: " + e.getLocalizedMessage());
	}       
}			

Modify the Home Class

The home class is used to lookup the resource instance. It can act as a factory for creating instances upon request, or build all instances. It is meant to be the entry point for locating a resource instance. For the consumer example the init() method is modified to get an instance of the consumer resource. The resource is a singleton and therfore only requires a single instance. For more information on creating a singleton, see the Creating a Singleton Service in the Apache WSRF documentation.

 public void init() throws Exception
    {
        super.init();
        add( createInstance( null ) );
    }
Note
At runtime, a client for the consumer must use the EPR to access the consumer resource. The EPR is returned by the notification producer when you subscribe to a notification topic.