5.4.1. Running the Publish-Subscribe Examples

5.4.1. Running the Publish-Subscribe Examples

The example programs discussed in this section are found in /usr/share/rhm-docs-0.2/cpp/pub-sub. To run these programs, do the following:

  1. Make sure that a qpidd broker is running:

     $ ps -eaf | grep qpidd
    

    If a broker is running, you should see the qpidd process in the output of the above command. If no broker is running, see the instructions in Chapter 3, Installing MRG Messaging.

  2. In the pub-sub directory, build the examples using make.

    $ make
    
  3. In separate windows, start one or more topic subscribers by running topic_subscriber, as follows:

    $ ./topic_subscriber
    

    You will see output similar to this:

    Declaring queue: usa59ef73a3-6aad-4838-8bd7-15f373cd4cba
    Subscribing to queue usa59ef73a3-6aad-4838-8bd7-15f373cd4cba
    Declaring queue: europe59ef73a3-6aad-4838-8bd7-15f373cd4cba
    Subscribing to queue europe59ef73a3-6aad-4838-8bd7-15f373cd4cba
    Declaring queue: news59ef73a3-6aad-4838-8bd7-15f373cd4cba
    Subscribing to queue news59ef73a3-6aad-4838-8bd7-15f373cd4cba
    Declaring queue: weather59ef73a3-6aad-4838-8bd7-15f373cd4cba
    Subscribing to queue weather59ef73a3-6aad-4838-8bd7-15f373cd4cba
    Listening for messages ...
    

    Each topic consumer creates a set of private queues, and binds each queue to the amq.topic exchange together with a binding that indicates which messages should be routed to the queue.

  4. In another window, start the topic publisher, which publishes messages to the amq.topic exchange, as follows:

    $ ./topic_publisher
    

    This program has no output; the messages are routed to the message queues for each topic_consumer as specified by the bindings the consumer created.

  5. Go back to the window for each topic consumer. You should see output like this:

    Message 0 from usa59ef73a3-6aad-4838-8bd7-15f373cd4cba
    Message: Message 0 from news59ef73a3-6aad-4838-8bd7-15f373cd4cba
    Message: Message 1 from news59ef73a3-6aad-4838-8bd7-15f373cd4cba
    Message: Message 1 from usa59ef73a3-6aad-4838-8bd7-15f373cd4cba
    Message: Message 2 from news59ef73a3-6aad-4838-8bd7-15f373cd4cba
    Message: Message 2 from usa59ef73a3-6aad-4838-8bd7-15f373cd4cba
    Message: Message 3 from news59ef73a3-6aad-4838-8bd7-15f373cd4cba
    ...
    

Now we will examine the code for each of these programs. In each section, we will discuss only the code that must be added to the skeleton shown in Section 5.1, “Creating and Closing Sessions”.