Skip to content

Instantly share code, notes, and snippets.

@skillachie
Created June 27, 2014 03:29
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save skillachie/3c21643a75d643bac169 to your computer and use it in GitHub Desktop.
Save skillachie/3c21643a75d643bac169 to your computer and use it in GitHub Desktop.
RabbitMQ Exchange to Exchange Binding With Persistence Using Queues
#!/usr/bin/env python
import pika
import sys
connection = pika.BlockingConnection(pika.ConnectionParameters(
host='localhost'))
channel = connection.channel()
#Declares the entry exchange to be used by all producers to send messages. Could be external producers as well
channel.exchange_declare(exchange='data_gateway',
exchange_type='fanout',
durable=True,
auto_delete=False)
#Declares the processing exchange to be used.Routes messages to various queues. For internal use only
channel.exchange_declare(exchange='data_distributor',
exchange_type='topic',
durable=True,
auto_delete=False)
#Exchange used to consume and distrubute messages related to trades
channel.exchange_declare(exchange='trade_highway',
exchange_type='fanout',
durable=True,
auto_delete=False)
#Exchange used to consume and distrubute messages related to tickers
channel.exchange_declare(exchange='ticker_highway',
exchange_type='fanout',
durable=True,
auto_delete=False)
#Exchange used to consume and distrubute messages related to orderbook
channel.exchange_declare(exchange='orderbook_highway',
exchange_type='fanout',
durable=True,
auto_delete=False)
#Binds the external/producer facing exchange to the internal exchange
channel.exchange_bind(destination='data_distributor',source='data_gateway')
#Binds and configure routing to correctly distribute messages to their appropriate internali fanout exhcnage
channel.exchange_bind(destination='trade_highway',source='data_distributor',routing_key='*.*.trade')
channel.exchange_bind(destination='ticker_highway',source='data_distributor',routing_key='*.*.ticker')
channel.exchange_bind(destination='orderbook_highway',source='data_distributor',routing_key='*.*.orderbook')
##Create Durable Queues binded to the data_distributor exchange
channel.queue_declare(queue='trades',durable=True)
channel.queue_declare(queue='ticker',durable=True)
channel.queue_declare(queue='orderbook',durable=True)
#Bind queues to exchanges and correct routing key. Allows for messages to be saved when no consumer is present
channel.queue_bind(queue='orderbook',exchange='orderbook_highway',routing_key='*.*.orderbook')
channel.queue_bind(queue='ticker',exchange='ticker_highway',routing_key='*.*.ticker')
channel.queue_bind(queue='trades',exchange='trade_highway',routing_key='*.*.trades')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment