4.6.2. Declaring an XML Exchange, Declaring and Binding a Queue

4.6.2. Declaring an XML Exchange, Declaring and Binding a Queue

Now we will declare an XML exchange named xml, a queue named message_queue, and a binding that routes messages based on an XQuery.

An XML Exchange differs from a direct exchange in two significant ways. The first is that there is no predeclared XML exchange, because it is not an exchange type defined in the AMQP specification. Therefore, you have to declare an XML exchange, whose type is "xml":

session.exchange_declare(exchange="xml", type="xml")

The second difference is that an XML Exchange uses an XQuery to determine whether to route the message, based on the XML content of the message or message properties, as shown in the following code:

session.queue_declare(queue="message_queue")

binding = {}
binding["xquery"] = """
   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 """
                      
session.exchange_bind(exchange="xml", queue="message_queue", binding_key="weather", arguments=binding)