4.2. Writing Direct Applications in Python.

The following programs work together to implement direct messaging using a Direct exchange:
  • declare_queues.py creates a queue on the broker, then exits.
  • direct_producer.py publishes messages to the direct exchange.
  • direct_consumer.py reads messages from the queue.
  • listener.py reads messages from the queue using a listener.

4.2.1. Running the Direct Examples

The example programs discussed in this section are found in /usr/share/doc/rhm-0.2/python/direct. 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. Declare a message queue and bind it to an exchange by running declare_queues.py, as follows:
    $ python declare_queues.py
    This program has no output. After this program has been run, all messages sent to the amq.direct exchange using the routing key routing_key are sent to the queue named message_queue.
  3. Publish a series of messages to the amq.direct exchange by running direct_producer.py, as follows:
    $ python direct_producer.py
    This program has no output; the messages are routed to the message queue, as instructed by the binding.
  4. Read the messages from the message queue using direct_consumer.py or listener.py, as follows:
    $ python direct_consumer.py
    or
    $ python listener.py
    You should see the following output:
    message 0
    message 1
    message 2
    message 3
    message 4
    message 5
    message 6
    message 7
    message 8
    message 9
    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”.