
In this part, we describe how notifications can be used with SOAP client and services. As an example, just think that all external actors are SOAP ones in the architecture figure above. This really simple sample is composed of several external actors : Some WS notification subscribers which will receive notifications they are interested in; A notification producer which will be in charge of sending notifications to a topic and the DSB which will route notification messages to the right notification subscribers.
{info}It is important to note that in the DSB, the notification engine is a core service a so is automatically exposed as Web service by the SOAP connector. But, even if we only use Web services in this sample, we can also have DSB service clients which directly send subscribe and notify messages to the DSB WSN endpoint itself.{info}
A code sample is available at [https://svn.petalslink.com/svnroot/trunk/research/commons/dsb/samples/dsb-wsn-sample/src/main/java/org/petalslink/dsb/sample/wsn/Main.java]
It uses Web service clients to send subscribe and notify messages; service exposed with the help of CXF to receive notifications; and the DSB as notification broker.
{code:language=java}package org.petalslink.dsb.sample.wsn;
import java.util.concurrent.TimeUnit;
import javax.xml.namespace.QName;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.TransformerException;
import org.petalslink.dsb.commons.service.api.Service;
import org.petalslink.dsb.notification.client.http.HTTPNotificationConsumerClient;
import org.petalslink.dsb.notification.client.http.HTTPNotificationProducerClient;
import org.petalslink.dsb.notification.client.http.HTTPNotificationProducerRPClient;
import org.petalslink.dsb.notification.service.NotificationConsumerService;
import org.petalslink.dsb.soap.CXFExposer;
import org.petalslink.dsb.soap.api.Exposer;
import org.w3c.dom.Document;
import com.ebmwebsourcing.easycommons.xml.XMLHelper;
import com.ebmwebsourcing.wsstar.basenotification.datatypes.api.abstraction.Notify;
import com.ebmwebsourcing.wsstar.basenotification.datatypes.api.abstraction.Subscribe;
import com.ebmwebsourcing.wsstar.basenotification.datatypes.api.abstraction.SubscribeResponse;
import com.ebmwebsourcing.wsstar.basenotification.datatypes.api.utils.WsnbException;
import com.ebmwebsourcing.wsstar.topics.datatypes.api.WstopConstants;
import com.ebmwebsourcing.wsstar.wsnb.services.INotificationConsumer;
import com.ebmwebsourcing.wsstar.wsnb.services.INotificationProducer;
import com.ebmwebsourcing.wsstar.wsnb.services.INotificationProducerRP;
import com.ebmwebsourcing.wsstar.wsnb.services.impl.util.Wsnb4ServUtils;
import com.ebmwebsourcing.wsstar.wsrfbf.services.faults.AbsWSStarFault;
/**
* The notification engine is hosted on some DSB node. Let's subscribe to events
* and send events. We will check if all is working...
*
* @author chamerling
*
*/
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
System.out.println("****** CREATING LOCAL SERVER ******");
// local address which will receive notifications
String address = "http://localhost:8878/petals/services/NotificationConsumerPortService";
// DSB adress to subscribe to
String dsbSubscribe = "http://localhost:8084/petals/services/NotificationConsumerPortService";
// DSB address to send notifications to
String dsbNotify = "http://localhost:8084/petals/services/NotificationProducerPortService";
// the one which will receive notifications
System.out
.println("Creating service which will receive notification messages from the DSB...");
Service server = null;
QName interfaceName = new QName("http://docs.oasis-open.org/wsn/bw-2",
"NotificationConsumer");
QName serviceName = new QName("http://docs.oasis-open.org/wsn/bw-2",
"NotificationConsumerService");
QName endpointName = new QName("http://docs.oasis-open.org/wsn/bw-2",
"NotificationConsumerPort");
// expose the service
INotificationConsumer consumer = new INotificationConsumer() {
public void notify(Notify notify) throws WsnbException {
System.out
.println("Got a notify on HTTP service, this notification comes from the DSB itself...");
Document dom = Wsnb4ServUtils.getWsnbWriter().writeNotifyAsDOM(notify);
System.out.println("==============================");
try {
XMLHelper.writeDocument(dom, System.out);
} catch (TransformerException e) {
}
System.out.println("==============================");
}
};
NotificationConsumerService service = new NotificationConsumerService(interfaceName,
serviceName, endpointName, "NotificationConsumerService.wsdl", address, consumer);
Exposer exposer = new CXFExposer();
try {
server = exposer.expose(service);
server.start();
System.out.println("Local server is started and is ready to receive notifications");
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
System.out.println("****** SUBSCRIBE TO NOTIFICATION ******");
// Create the subscription, ie as a client let's send a subscribe
// message with reference to the previously registered endpoint
System.out.println("Subscribing to receive DSB notifications...");
INotificationProducer producerClient = new HTTPNotificationProducerClient(dsbSubscribe);
Subscribe subscribe = loadSubscribe();
try {
SubscribeResponse response = producerClient.subscribe(subscribe);
System.out.println("Got a response from the DSB");
Document dom = Wsnb4ServUtils.getWsnbWriter().writeSubscribeResponseAsDOM(response);
XMLHelper.writeDocument(dom, System.out);
} catch (WsnbException e) {
e.printStackTrace();
} catch (AbsWSStarFault e) {
e.printStackTrace();
} catch (TransformerException e) {
e.printStackTrace();
}
System.out.println("****** SEND NOTIFICATION TO THE DSB ******");
// send a notification to the DSB, since we just subscribed, we should
// receive it back...
System.out.println("Sending a notification to the DSB...");
INotificationConsumer consumerClient = new HTTPNotificationConsumerClient(dsbNotify);
Notify notify = loadNotify();
for (int i = 0; i < 2; i++) {
try {
consumerClient.notify(notify);
} catch (WsnbException e) {
e.printStackTrace();
}
}
try {
System.out.println("Waiting...");
TimeUnit.SECONDS.sleep(10);
} catch (InterruptedException e) {
}
System.out.println("****** GET RESOURCE PROPERTIES ******");
// getting resources
INotificationProducerRP resourceClient = new HTTPNotificationProducerRPClient(dsbSubscribe);
try {
QName qname = WstopConstants.TOPIC_SET_QNAME;
com.ebmwebsourcing.wsstar.resourceproperties.datatypes.api.abstraction.GetResourcePropertyResponse response = resourceClient
.getResourceProperty(qname);
System.out.println("Get Resource response :");
Document dom = Wsnb4ServUtils.getWsrfrpWriter().writeGetResourcePropertyResponseAsDOM(
response);
XMLHelper.writeDocument(dom, System.out);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("-Bye");
}
/**
* @return
*/
private static Notify loadNotify() {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
Document document;
try {
document = factory.newDocumentBuilder().parse(
Main.class.getResourceAsStream("/notify.xml"));
return Wsnb4ServUtils.getWsnbReader().readNotify(document);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* @return
*/
private static Subscribe loadSubscribe() {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
Document document;
try {
document = factory.newDocumentBuilder().parse(
Main.class.getResourceAsStream("/subscribe.xml"));
return Wsnb4ServUtils.getWsnbReader().readSubscribe(document);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}{code}
h1. Configuration
The notification configuration files are located under _$DSB/conf/_ folder. You can configure kernel and business services level from these configuration files.
h2. conf/topics/*-topicset.xml
Describes the topicsets supported by the notification engine. For example:
{code:language=xml}<?xml version="1.0" encoding="utf-8"?>
<wstop:TopicSet xmlns:wstop="http://docs.oasis-open.org/wsn/t-1">
<!-- DSB topics -->
<tns:DSBTopic xmlns:tns="http://www.petalslink.org/resources/event/1.0"
wstop:topic="true" />
<tns:MonitoringTopic xmlns:tns="http://www.petalslink.org/resources/event/1.0"
wstop:topic="true" />
<tns:CreationResourcesTopic
xmlns:tns="http://www.petalslink.org/resources/event/1.0" wstop:topic="true" />
</wstop:TopicSet>{code}
You can configure topic sets for :
* kernel service in the kernel-topicset.xml file
* business services in the business-topicset.xml file
h2. conf/notification.cfg
The default configuration file for the notification engine. Provides a way to define the engine internal endpoint/interface/service values. For example:
{code}#Configuration file for the notification engine
endpoint=Endpoint
interface={http://dsb.petalslink.com/notification}NotificationInterface
service={http://dsb.petalslink.com/notification}NotificationService