4.11.4. Subscribe to the XML Exchange
The following code subscribes to an XML exchange
myxml
by creating a queue xmlq
and binding it to the exchange with an XQuery.
AMQP 0-10
- Python
rxXML = ssn.receiver("myxmlq; {create:always, link: { x-bindings: [{exchange:myxml, key:weather, arguments:{xquery:'./weather'} }]}}")
AMQP 1.0
- C++
Receiver rxXML = ssn.createReceiver("myxml/weather; {link: {name:myxmlq, filter:{name:myfilter, descriptor:'apache.org:query-filter:string', value:'./weather'}}}");
The XQuery
./weather
will match any messages whose body content has the root XML element <weather>
.
Note the use of the
key
argument for x-bindings
. This ensures that the binding has a unique name, allowing it to be deleted and updated by name, and ensuring that it is not accidentally updated, as might be the case if it were anonymous in the namespace of the exchange.
The following code demonstrates using the XML exchange with a more complex XQuery (using AMQP 0-10 addressing):
- Python
#!/usr/bin/python import sys from qpid.messaging import * conn = Connection("localhost:5672") conn.open() try: ssn = conn.session() tx = ssn.sender("myxml/weather; {create: always, node: {type: topic, x-declare: {exchange: myxml, type: xml}}}") xquerystr = 'let $w := ./weather ' xquerystr += "return $w/station = 'Raleigh-Durham International Airport (KRDU)' " xquerystr += 'and $w/temperature_f > 50 ' xquerystr += 'and $w/temperature_f - $w/dewpoint > 5 ' xquerystr += 'and $w/wind_speed_mph > 7 ' xquerystr += 'and $w/wind_speed_mph < 20' rxaddr = 'myxmlq; {create: always, ' rxaddr += 'link: {x-bindings: [{exchange: myxml, ' rxaddr += 'key: weather, ' rxaddr += 'arguments: {xquery: "' + xquerystr + '"' rxaddr += '}}]}}' rx = ssn.receiver(rxaddr) msgstr = '<weather>' msgstr += '<station>Raleigh-Durham International Airport (KRDU)</station>' msgstr += '<wind_speed_mph>16</wind_speed_mph>' msgstr += '<temperature_f>70</temperature_f>' msgstr += '<dewpoint>35</dewpoint>' msgstr += '</weather>' msg = Message(msgstr) tx.send(msg) rxmsg = rx.fetch(timeout=1) print rxmsg ssn.acknowledge() finally: conn.close()