Este conteúdo não está disponível no idioma selecionado.

2.4. Create a PullPoint Client


Overview

This section describes how to create a pull-point client of the notification broker. The pull-point client first creates a remote pull-point (which is used to accumulate messages), then subscribes the pull-point to a particular topic. Finally, the pull-point client retrieves the accumulated messages from the pull-point.

Sample PullPoint client code

Example 2.3, “PullPoint Client Code” shows the code for a sample pull-point client that subscribes to messages published on the MyTopic topic.

Example 2.3. PullPoint Client Code

// Java
package org.jboss.fuse.example.wsn.pullpoint;

import java.util.List;
import java.util.Iterator;

import javax.xml.bind.JAXBElement;
import javax.xml.namespace.QName;

import org.w3c.dom.Element;

import org.apache.cxf.wsn.client.PullPoint;
import org.apache.cxf.wsn.client.NotificationBroker;
import org.apache.cxf.wsn.client.CreatePullPoint;
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 PullPoint
        CreatePullPoint createPullPoint
            = new CreatePullPoint("http://localhost:" + wsnPort + "/wsn/CreatePullPoint");
        PullPoint pullPoint = createPullPoint.create();

        // Create a PullPoint style subscription
        NotificationBroker notificationBroker 
            = new NotificationBroker("http://localhost:" + wsnPort + "/wsn/NotificationBroker");
        Subscription subscription = notificationBroker.subscribe(pullPoint, "MyTopic");

        // Wait for some messages to accumulate in the pull point
        Thread.sleep(10000);

        // Now retrieve messages from the pull point
        List<NotificationMessageHolderType> messages = pullPoint.getMessages(10);

        if (!messages.isEmpty()) {
            Iterator<NotificationMessageHolderType> messageIterator = messages.iterator();
            while (messageIterator.hasNext()) {
                NotificationMessageHolderType messageH = messageIterator.next();
                Object o = messageH.getMessage().getAny();
                System.out.println(messageH.getMessage().getAny());
                if (o instanceof Element) {
                    System.out.println(((Element)o).getTextContent());
                }
            }
        }
        else {
            System.out.println("Warn: message list is empty!");
        }

        subscription.unsubscribe();
        pullPoint.destroy();

        System.exit(0);
    }

}

Steps to create a pullpoint client

Perform the following steps to create a PullPoint client:
  1. Use the archetype:generate goal to invoke the servicemix-cxf-code-first-osgi-bundle archetype. Under the wsn directory, invoke the Maven archetype 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-pullpoint \
      -Dversion=1.0-SNAPSHOT \
      -Dpackage=org.jboss.fuse.example.wsn.pullpoint \
      -Dfabric8-profile=wsn-pullpoint-profile
    Note
    The 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-pullpoint
    version: 1.0-SNAPSHOT
    package: org.jboss.fuse.example.wsn.pullpoint
    fabric8-profile: wsn-pullpoint-profile
     Y: :
    Type Return to accept the settings and generate the project. When the command finishes, you should find a new Maven project in the wsn/wsn-pullpoint directory.
  2. Some of the generated project files are not needed for this tutorial. Under the wsn/wsn-pullpoint directory, delete the following files and directories:
    src/main/resources/OSGI-INF/blueprint/blueprint.xml
    src/main/java/org/jboss/fuse/example/wsn/pullpoint/HelloWorld.java
    src/main/java/org/jboss/fuse/example/wsn/pullpoint/HelloWorldImpl.java
  3. Edit the pom.xml file in the wsn-pullpoint directory, and add the following 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>
  4. Delete the cxf-java2ws-plugin plug-in configuration from the wsn-pullpoint/pom.xml file. That is, open the pom.xml file and delete the cxf-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.pullpoint.HelloWorld</className>
                    <genWsdl>true</genWsdl>
                    <attachWsdl>false</attachWsdl>
                    <verbose>true</verbose>
                  </configuration>
                </execution>
              </executions>
            </plugin>
            ...
          </plugins>
        </build>
        ...
    </project>
  5. Add a client profile to the POM file, which provides an easy way to run the publisher client code. Edit the wsn-pullpoint/pom.xml file and add the new profile 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.pullpoint.Client</mainClass>
                                    </configuration>
                                </execution>
                            </executions>
                        </plugin>
                    </plugins>
                </build>
            </profile>
        </profiles>
        ...
    </project>
  6. Create a new Client.java file in the wsn-pullpoint/src/main/java/org/jboss/fuse/example/wsn/pullpoint/ directory, and add the code from Example 2.3, “PullPoint Client Code” to this file.

Test the PullPoint client

Test the PullPoint client as follows:
  1. If the JBoss A-MQ container is not already running (with the notification broker installed), start it up now:
    ./amq
  2. Run the publisher client at the command line. Open a new command prompt, and enter the following commands:
    cd wsn/wsn-publisher
    mvn -Pclient
    In the command window, you should see some output like the following:
    ...
    [INFO] Tests are skipped.
    [INFO] 
    [INFO] --- exec-maven-plugin:1.4.0:java (default) @ wsn-publisher ---
    You now have approximately two minutes before the publisher client times out.
  3. Run the PullPoint client at the command line. Open a new command prompt and enter the following commands:
    cd wsn/wsn-pullpoint
    mvn -Pclient
    After a ten second delay, you should see some output like the following:
    ...
    [INFO] Tests are skipped.
    [INFO] 
    [INFO] --- exec-maven-plugin:1.4.0:java (default) @ wsn-pullpoint ---
    [foo: null]
    Hello World!
    [foo: null]
    Hello World!
    ...
  4. To inspect the state of the notification broker, you can connect to the JMX port of the ActiveMQ broker. Start up a JMX console by entering the following command at the command line:
    jconsole
    In the JConsole: New Connection dialog, select Remote Process and enter the following URL in the accompanying text field:
    service:jmx:rmi:///jndi/rmi://localhost:1099/karaf-root
    In the Username and Password fields, enter one of the user credentials you created at the start of this tutorial. When you are connected to the JMX port, you can inspect the state of the broker by clicking on the MBeans tab and drilling down the object tree in the JConsole.
Red Hat logoGithubRedditYoutubeTwitter

Aprender

Experimente, compre e venda

Comunidades

Sobre a documentação da Red Hat

Ajudamos os usuários da Red Hat a inovar e atingir seus objetivos com nossos produtos e serviços com conteúdo em que podem confiar.

Tornando o open source mais inclusivo

A Red Hat está comprometida em substituir a linguagem problemática em nosso código, documentação e propriedades da web. Para mais detalhes veja oBlog da Red Hat.

Sobre a Red Hat

Fornecemos soluções robustas que facilitam o trabalho das empresas em plataformas e ambientes, desde o data center principal até a borda da rede.

© 2024 Red Hat, Inc.