4.4.1. Running the Publish-Subscribe Examples

4.4.1. Running the Publish-Subscribe Examples

The example programs discussed in this section are found in /usr/share/doc/rhm-0.2/python/pubsub. 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 separate windows, start one or more topic subscribers by running topic_subscriber.py, as follows:

    $ python topic_subscriber.py
    

    You will see output similar to this:

    Queues created - please start the topic producer
    Subscribing local queue 'local_news' to news-53408183-fcee-4b92-950b-90abb297e739'
    Subscribing local queue 'local_weather' to weather-53408183-fcee-4b92-950b-90abb297e739'
    Subscribing local queue 'local_usa' to usa-53408183-fcee-4b92-950b-90abb297e739'
    Subscribing local queue 'local_europe' to europe-53408183-fcee-4b92-950b-90abb297e739'
    Messages on 'news' queue:
    

    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.

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

    $ python topic_publisher.py
    

    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.

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

    Messages on 'news' queue:
    usa.news 0
    usa.news 1
    usa.news 2
    usa.news 3
    usa.news 4
    europe.news 0
    europe.news 1
    europe.news 2
    europe.news 3
    europe.news 4
    That's all, folks!
    Messages on 'weather' queue:
    usa.weather 0
    usa.weather 1
    usa.weather 2
    usa.weather 3
    usa.weather 4
    europe.weather 0
    europe.weather 1
    europe.weather 2
    europe.weather 3
    europe.weather 4
    That's all, folks!
    Messages on 'usa' queue:
    usa.news 0
    usa.news 1
    usa.news 2
    usa.news 3
    usa.news 4
    usa.weather 0
    usa.weather 1
    usa.weather 2
    usa.weather 3
    usa.weather 4
    That's all, folks!
    Messages on 'europe' queue:
    europe.news 0
    europe.news 1
    europe.news 2
    europe.news 3
    europe.news 4
    europe.weather 0
    europe.weather 1
    europe.weather 2
    europe.weather 3
    europe.weather 4
    That's all, folks!
    

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 4.1, “Creating and Closing Sessions”.