├── requirements.txt ├── basic-producer.py ├── basic-consumer.py ├── robust-consumermixin.py ├── README.md └── robust-consumer.py /requirements.txt: -------------------------------------------------------------------------------- 1 | kombu==4.0.2 2 | -------------------------------------------------------------------------------- /basic-producer.py: -------------------------------------------------------------------------------- 1 | from kombu import Connection, Exchange, Queue, Producer 2 | 3 | rabbit_url = "amqp://localhost:5672/" 4 | 5 | conn = Connection(rabbit_url) 6 | 7 | channel = conn.channel() 8 | 9 | exchange = Exchange("example-exchange", type="direct") 10 | 11 | producer = Producer(exchange=exchange, channel=channel, routing_key="BOB") 12 | 13 | queue = Queue(name="example-queue", exchange=exchange, routing_key="BOB") 14 | queue.maybe_bind(conn) 15 | queue.declare() 16 | 17 | producer.publish("Hello there!") 18 | -------------------------------------------------------------------------------- /basic-consumer.py: -------------------------------------------------------------------------------- 1 | from kombu import Connection, Exchange, Queue, Consumer 2 | 3 | rabbit_url = "amqp://localhost:5672/" 4 | 5 | conn = Connection(rabbit_url) 6 | 7 | exchange = Exchange("example-exchange", type="direct") 8 | 9 | queue = Queue(name="example-queue", exchange=exchange, routing_key="BOB") 10 | 11 | 12 | def process_message(body, message): 13 | print("The body is {}".format(body)) 14 | message.ack() 15 | 16 | 17 | with Consumer(conn, queues=queue, callbacks=[process_message], accept=["text/plain"]): 18 | conn.drain_events(timeout=2) 19 | -------------------------------------------------------------------------------- /robust-consumermixin.py: -------------------------------------------------------------------------------- 1 | from kombu import Connection, Exchange, Queue 2 | from kombu.mixins import ConsumerMixin 3 | 4 | rabbit_url = "amqp://localhost:5672/" 5 | 6 | 7 | class Worker(ConsumerMixin): 8 | def __init__(self, connection, queues): 9 | self.connection = connection 10 | self.queues = queues 11 | 12 | def get_consumers(self, Consumer, channel): 13 | return [Consumer(queues=self.queues, 14 | callbacks=[self.on_message])] 15 | 16 | def on_message(self, body, message): 17 | print('Got message: {0}'.format(body)) 18 | message.ack() 19 | 20 | exchange = Exchange("example-exchange", type="direct") 21 | queues = [Queue("example-queue", exchange, routing_key="BOB")] 22 | 23 | with Connection(rabbit_url, heartbeat=4) as conn: 24 | try: 25 | worker = Worker(conn, queues) 26 | worker.run() 27 | except: 28 | raise 29 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | kombu-examples 2 | ===== 3 | 4 | This repository contains examples of using Python 3 and Kombu to interact with RabbitMQ. I created it to contain code 5 | used in the following articles I have written: 6 | * [Talking to RabbitMQ with Python and Kombu](https://medium.com/@Skablam/talking-to-rabbitmq-with-python-and-kombu-6cbee93b1298#.vrt5fuwmw) 7 | * [Building Robust RabbitMQ Consumers with Python and Kombu: Part 1](https://medium.com/@Skablam/building-robust-rabbitmq-consumers-with-python-and-kombu-part-1-ccd660d17271#.l2n1mtxyi) 8 | * [Building Robust RabbitMQ Consumers with Python and Kombu: Part 2](https://medium.com/@Skablam/building-robust-rabbitmq-consumers-with-python-and-kombu-part-2-e9505f56e12e#.wp8lsoqqf) 9 | 10 | Pre-requisites 11 | -- 12 | 13 | For these example to run it is assumed that RabbitMQ is installed and running locally on port 5672. 14 | 15 | Quick start 16 | -- 17 | 18 | Run: 19 | 20 | ``` 21 | pip install -r requirements.txt 22 | ``` 23 | 24 | Run the basic producer to add a message to a queue called "example-queue": 25 | 26 | ``` 27 | python basic-producer.py 28 | ``` 29 | 30 | Then run the basic consumer that will print out the message to the terminal: 31 | 32 | ``` 33 | python basic-consumer.py 34 | ``` 35 | -------------------------------------------------------------------------------- /robust-consumer.py: -------------------------------------------------------------------------------- 1 | from kombu import Connection, Exchange, Queue, Consumer 2 | import socket 3 | 4 | 5 | rabbit_url = "amqp://localhost:5672/" 6 | 7 | conn = Connection(rabbit_url, heartbeat=10) 8 | 9 | exchange = Exchange("example-exchange", type="direct") 10 | 11 | queue = Queue(name="example-queue", exchange=exchange, routing_key="BOB") 12 | 13 | 14 | def process_message(body, message): 15 | print("The body is {}".format(body)) 16 | message.ack() 17 | 18 | consumer = Consumer(conn, queues=queue, callbacks=[process_message], accept=["text/plain"]) 19 | consumer.consume() 20 | 21 | 22 | def establish_connection(): 23 | revived_connection = conn.clone() 24 | revived_connection.ensure_connection(max_retries=3) 25 | channel = revived_connection.channel() 26 | consumer.revive(channel) 27 | consumer.consume() 28 | return revived_connection 29 | 30 | 31 | def consume(): 32 | new_conn = establish_connection() 33 | while True: 34 | try: 35 | new_conn.drain_events(timeout=2) 36 | except socket.timeout: 37 | new_conn.heartbeat_check() 38 | 39 | 40 | def run(): 41 | while True: 42 | try: 43 | consume() 44 | except conn.connection_errors: 45 | print("connection revived") 46 | 47 | run() 48 | --------------------------------------------------------------------------------