class BusConsumer(object): def __init__(self): self._marshaler = Marshaler() def connect(self): self.connection = pika.BlockingConnection(pika.ConnectionParameters('localhost')) self.channel = self.connection.channel() def add_binding(self, callback, queue_name, exchange, key): self.callback = callback self.queue_name = queue_name self.channel.queue_declare(queue=queue_name, exclusive=False, durable=True) self.channel.queue_bind(queue=queue_name, exchange=exchange, routing_key=key) def on_message(self, channel, method, header, body): body = self._marshaler.unmarshal_message(body) logger.debug('Received new event : %s', body) self.callback(body) self.channel.basic_ack(delivery_tag=method.delivery_tag) def run(self): logger.info('Running...') try: self.channel.basic_consume(self.on_message, self.queue_name) self.channel.start_consuming() except AMQPConnectionError: raise BusConnectionError() def stop(self): if self.connection.is_open: self.channel.stop_consuming() self.connection.close()
def test_unmarshal_message(self): json = '{"error": null, "value": "foobar"}' expected = {'value': 'foobar', 'error': None} marshal = Marshaler({}) result = marshal.unmarshal_message(json) self.assertEquals(result, expected)
def test_unmarshal_message(self): json = '{"error": null, "value": "foobar"}' expected = {'value': 'foobar', 'error': None} marshal = Marshaler({}) result = marshal.unmarshal_message(json) self.assertEquals(result, expected)
class BusConsumer(object): def __init__(self): self._marshaler = Marshaler() def connect(self): self.connection = pika.BlockingConnection( pika.ConnectionParameters('localhost')) self.channel = self.connection.channel() def add_binding(self, callback, queue_name, exchange, key): self.callback = callback self.queue_name = queue_name self.channel.queue_declare(queue=queue_name, exclusive=False, durable=True) self.channel.queue_bind(queue=queue_name, exchange=exchange, routing_key=key) def on_message(self, channel, method, header, body): body = self._marshaler.unmarshal_message(body) logger.debug('Received new event : %s', body) self.callback(body) self.channel.basic_ack(delivery_tag=method.delivery_tag) def run(self): logger.info('Running...') try: self.channel.basic_consume(self.on_message, self.queue_name) self.channel.start_consuming() except AMQPConnectionError: raise BusConnectionError() def stop(self): if self.connection.is_open: self.channel.stop_consuming() self.connection.close()