2.2. Create a Publisher Client
Overview
This section describes how to create a publisher client of the notification broker. The publisher client is capable of sending messages on a specific topic to the notification broker.
Prerequisites
In order to access artifacts from the Maven repository, you need to add the
fusesource
repository to Maven's settings.xml
file. Maven looks for your settings.xml
file in the following standard location:
- UNIX:
home/User/.m2/settings.xml
- Windows:
Documents and Settings\User\.m2\settings.xml
If there is currently no
settings.xml
file at this location, you need to create a new settings.xml
file. Modify the settings.xml
file by adding the repository
element for fusesource
, as highlighted in the following example:
<settings> <profiles> <profile> <id>my-profile</id> <activation> <activeByDefault>true</activeByDefault> </activation> <repositories> <repository> <id>fusesource</id> <url>http://repo.fusesource.com/nexus/content/groups/public/</url> <snapshots> <enabled>false</enabled> </snapshots> <releases> <enabled>true</enabled> </releases> </repository> ... </repositories> </profile> </profiles> ... </settings>
Sample publisher client code
Example 2.1, “Publisher Client Code” shows the code for a sample publisher client that pushes a simple
Hello World!
message to the MyTopic
topic on the notification broker.
Example 2.1. Publisher Client Code
// Java package org.jboss.fuse.example.wsn.publisher.client; import javax.xml.bind.JAXBElement; import javax.xml.namespace.QName; import org.w3c.dom.Element; import org.apache.cxf.wsn.client.Consumer; import org.apache.cxf.wsn.client.NotificationBroker; import org.apache.cxf.wsn.client.Subscription; import org.oasis_open.docs.wsn.b_2.NotificationMessageHolderType; /** * */ public final class Client { private Client() { //not constructed } /** * @param args */ public static void main(String[] args) throws Exception { String wsnPort = "8182"; if (args.length > 0) { wsnPort = args[0]; } // Create a NotificationBroker proxy NotificationBroker notificationBroker = new NotificationBroker("http://localhost:" + wsnPort + "/wsn/NotificationBroker"); for (int i=0; i<120; i++) { // Send notifications on the Topic notificationBroker.notify( "MyTopic", new JAXBElement<String>( new QName("urn:test:org", "foo"), String.class, "Hello World!" ) ); // Sleep for 1s between notifications Thread.sleep(1000); } // Cleanup and exit System.exit(0); } }
NotificationBroker proxy class
The client code from Example 2.1, “Publisher Client Code” uses the
NotificationBroker
proxy class to connect to the remote notification broker and to publish notifications to the broker. In this example, the following NotificationBroker
methods are invoked:
NotificationBroker(String address, Class<?>... cls)
- The
NotificationBroker
constructor normally takes a single argument, which is the URL of the remote notification broker Web service. notify(String topic, Object msg)
- Sends a message,
msg
, on the topic,topic
, to the notification broker, where the format of themsg
argument is an XML document. For example, you can use the JAX-B API to create a single XML element containing a string for the message, as shown in Example 2.1, “Publisher Client Code”.
Note
This
NotificationBroker
proxy class belongs to the simplified client API provided by the Apache CXF implementation of WS-Notification; it is not an instance of the standard NotificationBroker
SEI defined by JAX-WS (although the standard SEI is also available and could be used instead).
Steps to create a publisher client
Perform the following steps to create a publisher client:
- You can create a Maven project directly from the command line, by invoking the
archetype:generate
goal. First of all, create a directory to hold the WS-Notification client projects. Open a command prompt, navigate to a convenient location in your file system, and create thewsn
directory, as follows:mkdir wsn cd wsn
You can now use thearchetype:generate
goal to invoke theservicemix-cxf-code-first-osgi-bundle
archetype, which generates a simple Apache CXF demonstration, as follows:mvn archetype:generate \ -DarchetypeGroupId=org.apache.servicemix.tooling \ -DarchetypeArtifactId=servicemix-cxf-code-first-osgi-bundle \ -DarchetypeVersion=2013.01.0.redhat-610379 \ -DgroupId=org.jboss.fuse.example \ -DartifactId=wsn-publisher \ -Dversion=1.0-SNAPSHOT \ -Dpackage=org.jboss.fuse.example.wsn.publisher
NoteThe backslash characters at the end of each line are effective as line-continuation characters on UNIX and LINUX platforms. If you are entering the command on a Windows platform, however, you must enter the entire command on a single line.You will be prompted to confirm the project settings, with a message similar to this one:Confirm properties configuration: groupId: org.jboss.fuse.example artifactId: wsn-publisher version: 1.0-SNAPSHOT package: org.jboss.fuse.example.wsn.publisher Y: :
Type Return to accept the settings and generate the project. When the command finishes, you should find a new Maven project in thewsn/wsn-publisher
directory. - Some of the generated project files are not needed for this tutorial. Under the
wsn/wsn-publisher
directory, delete the following files and directories:src/main/resources/META-INF/spring/beans.xml src/main/java/org/jboss/fuse/example/wsn/publisher/Person.java src/main/java/org/jboss/fuse/example/wsn/publisher/PersonImpl.java src/main/java/org/jboss/fuse/example/wsn/publisher/UnknownPersonFault.java src/main/java/org/jboss/fuse/example/wsn/publisher/types
- Edit the
pom.xml
file in thewsn-publisher
directory, and add the dependency required for WS-Notification clients:<?xml version="1.0" encoding="UTF-8"?> <project ...> ... <dependencies> ... <!-- Needed for WS-Notification --> <dependency> <groupId>org.apache.cxf.services.wsn</groupId> <artifactId>cxf-services-wsn-api</artifactId> <version>2.7.0.redhat-610379</version> </dependency> </dependencies> ... </project>
- Edit the
Client.java
file in thewsn-publisher/src/main/java/org/jboss/fuse/example/wsn/publisher/client/
directory, remove the existing content, and replace it with the code from Example 2.1, “Publisher Client Code”. - You can now run the publisher client from the
wsn-publisher
directory by entering the following command:mvn -Pclient
In the command window, you should see some output like the following:INFO: Creating Service {http://cxf.apache.org/wsn/jaxws}NotificationBrokerService from WSDL: jar:file:/Users/fbolton/.m2/repository/org/apache/cxf/services/wsn/ cxf-services-wsn-api/2.6.0.redhat-60024/cxf-services-wsn-api-2.6.0.redhat-60024.jar !/org/apache/cxf/wsn/wsdl/wsn.wsdl
Notification messages are now accumulating in the broker, but you will not be able to receive the messages until you create a consumer client.