You can directly publish notifications to the available kernel topics from Java clients by using the _NotificationCenter_ defined in _org.petalslink.dsb.kernel.pubsub.service.NotificationCenter_ (module deb-kernel-pubsubservice). Here is the NotificationSender interface definition:
{code}/**
*
*/
package org.petalslink.dsb.notification.commons.api;
import javax.xml.namespace.QName;
import org.petalslink.dsb.notification.commons.NotificationException;
import org.w3c.dom.Document;
import com.ebmwebsourcing.wsstar.basenotification.datatypes.api.abstraction.Notify;
/**
* Send notification on topics ie on all the consumers which are registered in
* the topic... A call to {@link #notify(Document, QName, String)} can reach N
* destination. It is up to the implementation to handle thread pools for
* efficiency for example...
*
* @author chamerling
*
*/
public interface NotificationSender {
/**
* Notify with a given payload. This will build the notification message
* with the given parameters.
*
* @param notify
* @param topic
* @param dialect
* @throws NotificationException
*/
void notify(final Document payload, final QName topic, final String dialect)
throws NotificationException;
/**
* Notify with a prefilled notification message.
*
* @param notify
* @throws NotificationException
*/
void notify(Notify notify) throws NotificationException;
}
{code}
To send the notification, just get the instance of the NotificationCenter and its notification sender, then call the notify operation:
{code:language=java}/**
*
*/
package org.petalslink.dsb.kernel.pubsub.service;
import org.objectweb.fractal.fraclet.annotation.annotations.FractalComponent;
import org.objectweb.fractal.fraclet.annotation.annotations.Interface;
import org.objectweb.fractal.fraclet.annotation.annotations.LifeCycle;
import org.objectweb.fractal.fraclet.annotation.annotations.Monolog;
import org.objectweb.fractal.fraclet.annotation.annotations.Provides;
import org.objectweb.fractal.fraclet.annotation.annotations.type.LifeCycleType;
import org.objectweb.util.monolog.api.Logger;
import org.ow2.petals.util.LoggingUtil;
import org.petalslink.dsb.notification.commons.NotificationException;
import org.petalslink.dsb.notification.commons.api.NotificationSender;
import com.ebmwebsourcing.wsstar.basenotification.datatypes.api.abstraction.Notify;
import com.ebmwebsourcing.wsstar.basenotification.datatypes.api.utils.WsnbException;
import com.ebmwebsourcing.wsstar.wsnb.services.INotificationConsumer;
/**
* This consumer service is a facade to the notification engine. It is just used
* to forward internal kernel calls to interested parties...
*
* @author chamerling
*
*/
@FractalComponent
@Provides(interfaces = { @Interface(name = "service", signature = INotificationConsumer.class) })
public class NotificationConsumerServiceImpl implements INotificationConsumer {
@Monolog(name = "logger")
private Logger logger;
private LoggingUtil log;
@LifeCycle(on = LifeCycleType.START)
protected void start() {
this.log = new LoggingUtil(this.logger);
}
@LifeCycle(on = LifeCycleType.STOP)
protected void stop() {
}
/*
* (non-Javadoc)
*
* @see
* com.ebmwebsourcing.wsstar.wsnb.services.INotificationConsumer#notify(
* com.ebmwebsourcing
* .wsstar.basenotification.datatypes.api.abstraction.Notify)
*/
public void notify(Notify notify) throws WsnbException {
if (log.isDebugEnabled()) {
log.debug("Got a notify message at the kernel level!");
}
// Let see if the notification center is available...
NotificationSender sender = NotificationCenter.get().getSender();
if (sender == null) {
log.warning("Can not find the notification sender");
return;
}
try {
sender.notify(notify);
} catch (NotificationException e) {
throw new WsnbException(e);
}
}
}
{code}
As a result, the notification will eb published to the topic (if available) and then distributed to all subscribers. Up to the notification engine will send notifications using the right transport...
h2. Receive notifications
You can also receive notifications directly in your Java code by subscribing to the notification engine. Hopefully, the DSB extensions mechanism ease this part, you do not have to subscribe yourself, the DSB will automatically subscribe to topics if your classes are well annotated. Simply use the _org.petalslink.dsb.annotations.notification.Notify_ annotation. The parameters of this annotation allows to customize the way you subscribe and receive notifications:
* *topics*: A list of topics you want to subscribe to. The topic format is defined like \{topicns}topicprefix:topicname
* *mode*: Do you want to receive just the business message? So just use the WSN value (default one). If you want to receive the complete WSN Notify payload, use the PAYLOAD value
\\
{info}The annotated method must have just one parameter which is a DOM document, more support soon.{info}
\\
{code:language=java}package org.petalslink.dsb.kernel.pubsub.service.internal.sample;
import javax.xml.transform.TransformerException;
import org.objectweb.fractal.fraclet.annotation.annotations.FractalComponent;
import org.objectweb.fractal.fraclet.annotation.annotations.LifeCycle;
import org.objectweb.fractal.fraclet.annotation.annotations.type.LifeCycleType;
import org.ow2.petals.util.XMLUtil;
import org.petalslink.dsb.annotations.notification.Mode;
import org.petalslink.dsb.annotations.notification.Notify;
import org.w3c.dom.Document;
/**
* @author chamerling
*
*/
@FractalComponent
public class BusinessConsumer {
@LifeCycle(on = LifeCycleType.START)
protected void start() {
}
@LifeCycle(on = LifeCycleType.STOP)
protected void stop() {
}
@Notify(topics = { "{http://www.petalslink.org/resources/event/1.0}dsb:DSBTopic" }, mode = Mode.PAYLOAD)
public void interestedInNotificationButNotOnly(Document document) {
System.out.println("I just received a payload notification!");
try {
System.out.println("####\n" + XMLUtil.parseToString(document) + " \n####");
} catch (TransformerException e) {
e.printStackTrace();
}
}
}{code}The previous class will subscribe to the _DSBTopic_ in the PAYLOAD mode, so each time someone publishes a notification in the DSBTopic, the _interestedInNotificationButNotOnly_ method will be called with the complete WSN Notify payload as DOM document.