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; 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 thekaraf-soap-archetype
archetype, which generates a simple Apache CXF demonstration, as follows:mvn archetype:generate \ -DarchetypeGroupId=io.fabric8.archetypes \ -DarchetypeArtifactId=karaf-soap-archetype \ -DarchetypeVersion=1.2.0.redhat-621084 \ -DgroupId=org.jboss.fuse.example \ -DartifactId=wsn-publisher \ -Dversion=1.0-SNAPSHOT \ -Dpackage=org.jboss.fuse.example.wsn.publisher \ -Dfabric8-profile=wsn-publisher-profile
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 fabric8-profile: wsn-publisher-profile 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/OSGI-INF/blueprint/blueprint.xml src/main/java/org/jboss/fuse/example/wsn/publisher/HelloWorld.java src/main/java/org/jboss/fuse/example/wsn/publisher/HelloWorldImpl.java
- 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> </dependency> </dependencies> ... </project>
NoteThere is no need to specify the version of this artifact, because the version is provided by the Fabric8 BOM, which uses Maven dependency management to specify the artifact versions. - Delete the
cxf-java2ws-plugin
plug-in configuration from thewsn-publisher/pom.xml
file. That is, open thepom.xml
file and delete thecxf-java2ws-plugin
plug-in configuration as highlighted in the following example:<?xml version="1.0" encoding="UTF-8"?> <project ...> ... <build> <plugins> ... <!-- DELETE THE FOLLOWING LINES! --> <plugin> <groupId>org.apache.cxf</groupId> <artifactId>cxf-java2ws-plugin</artifactId> <version>${cxf-version}</version> <executions> <execution> <id>process-classes</id> <phase>process-classes</phase> <goals> <goal>java2ws</goal> </goals> <configuration> <className>org.jboss.fuse.example.wsn.publisher.HelloWorld</className> <genWsdl>true</genWsdl> <attachWsdl>false</attachWsdl> <verbose>true</verbose> </configuration> </execution> </executions> </plugin> ... </plugins> </build> ... </project>
- Add a
client
profile to the POM file, which provides an easy way to run the publisher client code. Edit thewsn-publisher/pom.xml
file and add the newprofile
element, as highlighted in the following example:<?xml version="1.0" encoding="UTF-8"?> <project ...> ... <profiles> ... <profile> <id>client</id> <build> <defaultGoal>test</defaultGoal> <plugins> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>exec-maven-plugin</artifactId> <executions> <execution> <phase>test</phase> <goals> <goal>java</goal> </goals> <configuration> <mainClass>org.jboss.fuse.example.wsn.publisher.Client</mainClass> </configuration> </execution> </executions> </plugin> </plugins> </build> </profile> </profiles> ... </project>
- Create a new
Client.java
file in thewsn-publisher/src/main/java/org/jboss/fuse/example/wsn/publisher/
directory, and add the code from Example 2.1, “Publisher Client Code” to this file. - 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] --- exec-maven-plugin:1.4.0:java (default) @ wsn-publisher ---
Notification messages are now accumulating in the broker, but you will not be able to receive the messages until you create a consumer client.