4.6.1. Running the XML-based Routing Examples
The example programs discussed in this section are found in /usr/share/doc/rhm-0.2/python/xml-exchange. To run these programs, do the following:
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.
Declare an XML exchange and a message queue, then bind the queue to the 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 xml exchange using the routing key weather are sent to the queue named message_queue if they satisfy the conditions specified in the following XQuery, which is used in the binding:
let $w := ./weather
return $w/station = 'Raleigh-Durham International Airport (KRDU)'
and $w/temperature_f > 50
and $w/temperature_f - $w/dewpoint > 5
and $w/wind_speed_mph > 7
and $w/wind_speed_mph < 20
Publish a series of messages to the xml exchange by running xml_producer.py, as follows:
$ python xml_producer.py
The messages are routed to the message queue, as prescribed by the binding. Each message represents a weather report, such as this one:
<weather>
<station>Raleigh-Durham International Airport (KRDU)</station>
<wind_speed_mph>16</wind_speed_mph>
<temperature_f>70</temperature_f>
<dewpoint>35</dewpoint>
</weather>
Read the messages from the message queue using direct_consumer.py or listener.py, as follows:
$ python xml_consumer.py
or
$ python listener.py
You should see the following output:
<weather><station>Raleigh-Durham International Airport (KRDU)</station><wind_speed_mph>16</wind_speed_mph><temperature_f>70</temperature_f><dewpoint>35</dewpoint></weather>
Now we will look at 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”.