Red Hat AMQ 6
As of February 2025, Red Hat is no longer supporting Red Hat AMQ 6. If you are using AMQ 6, please upgrade: Migrating to AMQ 7.Este contenido no está disponible en el idioma seleccionado.
2.2. Create a Publisher Client
Overview Copiar enlaceEnlace copiado en el portapapeles!
Copiar enlaceEnlace copiado en el portapapeles!
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 Copiar enlaceEnlace copiado en el portapapeles!
Copiar enlaceEnlace copiado en el portapapeles!
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:
Sample publisher client code Copiar enlaceEnlace copiado en el portapapeles!
Copiar enlaceEnlace copiado en el portapapeles!
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
NotificationBroker proxy class Copiar enlaceEnlace copiado en el portapapeles!
Copiar enlaceEnlace copiado en el portapapeles!
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
NotificationBrokerconstructor 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 themsgargument 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 Copiar enlaceEnlace copiado en el portapapeles!
Copiar enlaceEnlace copiado en el portapapeles!
Perform the following steps to create a publisher client:
- You can create a Maven project directly from the command line, by invoking the
archetype:generategoal. 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 thewsndirectory, as follows:mkdir wsn cd wsn
mkdir wsn cd wsnCopy to Clipboard Copied! Toggle word wrap Toggle overflow You can now use thearchetype:generategoal to invoke thekaraf-soap-archetypearchetype, which generates a simple Apache CXF demonstration, as follows:Copy to Clipboard Copied! Toggle word wrap Toggle overflow 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:Copy to Clipboard Copied! Toggle word wrap Toggle overflow Type Return to accept the settings and generate the project. When the command finishes, you should find a new Maven project in thewsn/wsn-publisherdirectory. - Some of the generated project files are not needed for this tutorial. Under the
wsn/wsn-publisherdirectory, 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
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.javaCopy to Clipboard Copied! Toggle word wrap Toggle overflow - Edit the
pom.xmlfile in thewsn-publisherdirectory, and add the dependency required for WS-Notification clients:Copy to Clipboard Copied! Toggle word wrap Toggle overflow 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-pluginplug-in configuration from thewsn-publisher/pom.xmlfile. That is, open thepom.xmlfile and delete thecxf-java2ws-pluginplug-in configuration as highlighted in the following example:Copy to Clipboard Copied! Toggle word wrap Toggle overflow - Add a
clientprofile to the POM file, which provides an easy way to run the publisher client code. Edit thewsn-publisher/pom.xmlfile and add the newprofileelement, as highlighted in the following example:Copy to Clipboard Copied! Toggle word wrap Toggle overflow - Create a new
Client.javafile 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-publisherdirectory by entering the following command:mvn -Pclient
mvn -PclientCopy to Clipboard Copied! Toggle word wrap Toggle overflow In the command window, you should see some output like the following:[INFO] --- exec-maven-plugin:1.4.0:java (default) @ wsn-publisher ---
[INFO] --- exec-maven-plugin:1.4.0:java (default) @ wsn-publisher ---Copy to Clipboard Copied! Toggle word wrap Toggle overflow Notification messages are now accumulating in the broker, but you will not be able to receive the messages until you create a consumer client.