4.6.3. Publishing to an XML Exchange

4.6.3. Publishing to an XML Exchange

Publishing to an XML Exchange is very similar to publishing to a direct exchange — you publish to the exchange using a routing key, which the binding associates with an XQuery:

props = session.delivery_properties(routing_key="weather")

for i in range(10):
  print report(i)
  session.message_transfer(destination="xml", message=Message(props, report(i)))

In the above code, report(i) is a function that creates the XML messages used in this program. Each XML message represents a simplified weather report:


station = ("Raleigh-Durham International Airport (KRDU)", 
           "New Bern, Craven County Regional Airport (KEWN)", 
           "Boone, Watauga County Hospital Heliport (KTNB)",
           "Hatteras, Mitchell Field (KHSE)")
wind_speed_mph = ( 0, 2, 5, 10, 16, 22, 28, 35, 42, 51, 61, 70, 80 )
temperature_f = ( 30, 40, 50, 60, 70, 80, 90, 100 )
dewpoint = ( 35, 40, 45, 50 )

def pick_one(list, i):
  return str( list [ i % len(list)] )

def report(i):
  return ("<weather>" 
           + "<station>" + pick_one(station,i) + "</station>" 
           + "<wind_speed_mph>" + pick_one(wind_speed_mph,i) + "</wind_speed_mph>" 
           + "<temperature_f>" + pick_one(temperature_f,i) + "</temperature_f>"
           + "<dewpoint>" + pick_one(dewpoint,i) + "</dewpoint>" 
        + "</weather>")