0

How do I close a Kombu ConsumerMixin queue connected to a fanout exchange so I don't accumulate data from the Publisher while my Consumer is not active?

I'm using Kombu 3.0.24 (with RabbitMQ) in Python 2.7.

Below is my code for the two classes. I'd like these to be rather generic classes so I can reuse them for direct queues and RPC-like queries/replies.

The problem is, if I stop and restart the consumer, old data is waiting for me in the consumer queue. I assume this is because I need to delete the queue when I stop the consumer, but I can't figure out how. Thanks.

MessageConsumer.py

from kombu.mixins import ConsumerMixin
from kombu import Queue, Exchange, Connection
import logging

class MessageConsumer(ConsumerMixin):

    def __init__(self,
                 broker='amqp://',
                 exchange='mExchange',
                 queue = 'mQueue',
                 type='direct',
                 no_ack=False):
        self.connection = Connection(broker)
        self.mExchange = Exchange(exchange, type=type)
        self.mQueue = Queue(queue, self.mExchange)
        self.mQueue.no_ack = no_ack

    def get_consumers(self, Consumer, channel):
        return [Consumer(queues=self.mQueue,
                         accept=['json'],
                         callbacks=[self.process_task])]

    def process_task(self, body, message):
        logging.debug('RECEIVED: {}'.format(body))

    def stop(self):
        self.should_stop = True
        self.connection.release()


if __name__ == '__main__':

    mMessageConsumer = MessageConsumer(exchange='sensor_data',
                                       queue='rx1_queue',
                                       type='fanout',
                                       no_ack=True)
    try:
        mMessageConsumer.run()
    except KeyboardInterrupt:
        mMessageConsumer.stop()

MessagePublisher.py

from kombu import Queue, Exchange, Connection
from kombu.pools import producers
import logging

class MessagePublisher(object):

    def __init__(self,
                 broker='amqp://',
                 exchange='mExchange',
                 type='direct',
                 no_ack=False):
        self.connection = Connection(broker)
        self.mExchange = Exchange(exchange, type=type)

    def publish(self, message, serializer='json', compression=None):
        with producers[self.connection].acquire(block=True) as producer:
            producer.publish(message,
                             serializer=serializer,
                             compression=compression,
                             exchange=self.mExchange,
                             declare=[self.mExchange]
                             )

    def close(self):
        self.connection.release()

if __name__ == '__main__':
    mMessagePublisher = MessagePublisher(type='fanout',exchange='sensor_data')
    x=0
    while True:
        x += 1
        mMessagePublisher.publish(x)
    mMessagePublisher.close()

If there is a more efficient way for me to code this please suggest it. Most of the examples I found by Googling use older versions of Kombu so it's hard for me to figure out the best implementation with 3.0.24.

proximous
  • 617
  • 1
  • 10
  • 28

1 Answers1

1

Found the solution. I needed to create my queue with "exclusive=True". Then the queue only exists while my program is using it. Specifically:

self.mQueue = Queue(queue, self.mExchange, exclusive=True)
proximous
  • 617
  • 1
  • 10
  • 28