Last active
August 29, 2015 14:01
-
-
Save nickpresta/c671e9b5f8e1457ce440 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pika | |
import sys | |
import logging | |
logging.basicConfig(level=logging.INFO) | |
def sales_handler(ch, method, properties, body): | |
print " [x] %r:%r" % (method.routing_key, body,) | |
def refund_handler(ch, method, properties, body): | |
print " [x] %r:%r" % (method.routing_key, body,) | |
def chargeback_handler(ch, method, properties, body): | |
print " [x] %r:%r" % (method.routing_key, body,) | |
def catchall_handler(ch, method, properties, body): | |
print " [x] GOT EM" | |
ROUTING_KEY_PREFIX = 'transactions' | |
ROUTING_KEY_HANDLER_MAP = { | |
'{}.*'.format(ROUTING_KEY_PREFIX): catchall_handler, | |
'{}.sales'.format(ROUTING_KEY_PREFIX): sales_handler, | |
'{}.refund'.format(ROUTING_KEY_PREFIX): refund_handler, | |
} | |
def router(ch, method, properties, body): | |
try: | |
handler = ROUTING_KEY_HANDLER_MAP[method.routing_key] | |
except (KeyError, AttributeError): | |
print >> sys.stderr, "Didn't find '{}' in map".format(method.routing_key) | |
return | |
return handler(ch, method, properties, body) | |
connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost')) | |
channel = connection.channel() | |
channel.exchange_declare(exchange=ROUTING_KEY_PREFIX, type='topic') | |
result = channel.queue_declare(exclusive=True) | |
queue_name = result.method.queue | |
for key in ROUTING_KEY_HANDLER_MAP: | |
channel.queue_bind(exchange=ROUTING_KEY_PREFIX, queue=queue_name, routing_key=key) | |
print ' [*] Waiting for transactions. To exit press CTRL+C' | |
channel.basic_consume(router, queue=queue_name, no_ack=True) | |
channel.start_consuming() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment