servicemix-wsn2005
The servicemix-wsn2005
is a standard JBI Service Engine which implements the WS-Notification specification from Oasis.
Overview
While this article does not intend to be a review of the WS-Notification specification, here is a few terms you need to fully understand:
- Subscriber
- Publisher
- Subscription
- Publishing request
- Broker
- PullPoint
The current implementation has several limitations:
- subscriptions are not persistent: message will be lost if the subscriber can not be reached
- subscriptions can not be clustered: if you register the same subscriber in a cluster, each node will receive all notifications
- publishing can not be restricted to registered publishers: everyone can publish through an anonymous publisher
Usage scenarii
The WS-Notification SE can be used to provide publish/subscribe routing in the JBI bus. There are several usage:
- configuration using JBI packaging
- dynamic configuration inside the JBI bus
- outside the JBI bus
- embedding WS-Notification SE
Configuration using JBI packaging
The servicemix-wsn2005
accepts deployment of Service Units.
The SU must contain one or more files with an xml extension, each one containing a single WS-Notification request. The currently supported set of requests include:
The requests will be started in this very order, so that you can create a PullPoint and create a Subscription for it inside the same service unit.
Dynamic configuration inside the JBI bus
In addition to the deployment of service units, you can also create pull points or subscriptions dynamically from inside the JBI bus by using a simple API.
To create a subscription for a given JBI endpoint, you can use the following code:
QName service = ...; String endpoint = ...; String topic = ...; NotificationBroker wsnBroker = new NotificationBroker(getContext()); EndpointReferenceType consumer = new EndpointReferenceType(); consumer.setAddress(new AttributedURIType()); consumer.getAddress().setValue(service.getNamespaceURI() + "/" + service.getLocalPart() + "/" + endpoint); wsnBroker.subscribe(consumer, topic);
To publish a message on a given topic:
String topic = ...; NotificationBroker wsnBroker = new NotificationBroker(getContext()); wsnBroker.notify(topic, message);
where message
is a DOM element.
You can also create pull points and subscriptions for them:
CreatePullPoint wsnCreatePullPoint = new CreatePullPoint(getContext()); PullPoint pullPoint = wsnCreatePullPoint.createPullPoint(); NotificationBroker wsnBroker = new NotificationBroker(getContext()); wsnBroker.subscribe(pullPoint.getEndpoint(), topic);
Later, you can pull notifications from this pull point:
List<NotificationMessageHolderType> msgs = pullPoint.getMessages(0);
Outside the JBI bus
If you want to use WS-Notification ouside the JBI world, you will need to be able to receive incoming requests or send notifications to the external services. This must be done through by using Binding Components, either using HTTP or JMS.
To expose the NotificationBroker and CreatePullPoint services using HTTP/SOAP, you can deploy the following configuration file to servicemix-http
:
<beans xmlns:http="http://servicemix.apache.org/http/1.0" xmlns:wsn="http://servicemix.org/wsnotification"> <http:endpoint service="wsn:NotificationBroker" endpoint="http-binding" targetService="wsn:NotificationBroker" targetEndpoint="Broker" role="consumer" locationURI="http://localhost:8192/Broker/" defaultMep="http://www.w3.org/2004/08/wsdl/in-out" soap="true"/> <http:endpoint service="wsn:CreatePullPoint" endpoint="http-binding2" targetService="wsn:CreatePullPoint" targetEndpoint="Broker" role="consumer" locationURI="http://localhost:8192/CreatePullPoint/" defaultMep="http://www.w3.org/2004/08/wsdl/in-out" soap="true"/> </beans>
Embedding WS-Notification
If you use a single static configuration file for ServiceMix, you can easily leverage this component to create subscription, pull-points and publish messages.
xmlns:sm="http://servicemix.apache.org/config/1.0" xmlns:wsn="http://servicemix.apache.org/wsn/1.0" xmlns:test="http://servicemix.org/test" <sm:activationSpec id="receiver" service="test:service" endpoint="endpoint"> <sm:component> <bean class="org.apache.servicemix.tck.ReceiverComponent" /> </sm:component> </sm:activationSpec> <sm:activationSpec> <sm:component> <wsn:component> <wsn:requests> <wsn:subscribe consumer="http://servicemix.org/test/service/endpoint" topic="myTopic" /> </wsn:requests> </wsn:component> </sm:component> </sm:activationSpec> <sm:activationSpec service="test:publisher" endpoint="endpoint"> <sm:component> <wsn:publisher topic="myTopic" /> </sm:component> </sm:activationSpec>
The above code snippet creates a publisher proxy, a subscription and a subscriber.
The publisher proxy is a simple component that wraps incoming JBI exchanges into WS-Notification notify
requests and send them to a notification broker on the given topic. This could also be written in plain spring style:
<sm:activationSpec service="test:publisher" endpoint="endpoint"> <sm:component> <bean class="org.apache.servicemix.wsn.spring.PublisherComponent"> <property name="topic" value="myTopic" /> </bean> </sm:component> </sm:activationSpec>
The subscription is created inside a servicemix-wsn2005
component. You must fill the consumer
and topic
properties, where the consumer
is the URI-encoded target JBI endpoint, which will be resolved to the first component activated. You could also use the plain spring syntax:
<sm:activationSpec> <sm:component> <bean class="org.apache.servicemix.wsn.spring.WSNSpringComponent"> <property name="requests"> <list> <bean class="org.apache.servicemix.wsn.spring.SubscribeFactoryBean"> <property name="consumer" value="http://servicemix.org/test/service/endpoint" /> <property name="topic" value="myTopic" /> </bean> </list> </property> </bean> </sm:component> </sm:activationSpec>
WS-Notification messages
The following xml snippets are just examples of WS-Notification requests. You should refer to the specification for more informations.
Subscribe
<wsnt:Subscribe xmlns:wsnt="http://docs.oasis-open.org/wsn/b-2" xmlns:wsa="http://www.w3.org/2005/08/addressing"> <wsnt:ConsumerReference> <wsa:Address> http://www.consumer.org/service/endpoint </wsa:Address> </wsnt:ConsumerReference> <wsnt:Filter> <wsnt:TopicExpression Dialect="http://docs.oasis-open.org/wsn/t-1/TopicExpression/Simple"> myTopic </wsnt:TopicExpression> </wsnt:Filter> </wsnt:Subscribe>
CreatePullPoint
<wsnt:CreatePullPoint xmlns:wsnt="http://docs.oasis-open.org/wsn/b-2" xmlns:sm="http://servicemix.apache.org/wsn2005/1.0"> <sm:address> http://www.consumer.org/service/endpoint </sm:address> </wsnt:CreatePullPoint>
Note that the <sm:address />
element is a ServiceMix extension that creates a PullPoint on a specific JBI endpoint. This is very useful when you want to create a subscription for this endpoint at deployment time (see the previous paragraph).
Notify
<wsnt:Notify xmlns:wsnt="http://docs.oasis-open.org/wsn/b-2"> <wsnt:NotificationMessage> <wsnt:Topic>myTopic</wsnt:Topic> <wsnt:Message> <hello>world</hello> </wsnt:Message> </wsnt:NotificationMessage> </wsnt:Notify>