def wrapper(self, *args, **kwargs): client = Connection(self.amqp_url) client.connect() with client.channel() as channel: r = method(self, channel, *args, **kwargs) client.close() return r
class AMQPConnection(object): """ Manage AMQP connections """ connection = None def __init__(self, observer): logger.debug("observer %s" % type(observer)) self.observer = observer self.connection = Connection( os.environ.get('AMQP_URL', 'amqp://*****:*****@localhost/'), heartbeat=100 ) self.connection.on_connect(self.on_connect) self.connection.on_error(self.on_error) def connect(self): logger.debug("connect") self.connection.connect() def close(self): try: self.connection.close() except Exception as err: pass def on_connect(self,connection): self.observer.on_connect(connection) def on_error(self,connection): """No error handling yet !""" pass
def amqp_teardown(): global conn conn.close() conn2 = Connection(AMQP_URL) with conn2.channel() as channel: channel.exchange_delete("unit_test_room") with conn2.channel() as channel: channel.queue_delete(queue='listener1') with conn2.channel() as channel: channel.queue_delete(queue='listener2') conn2.close()
def amqp_teardown(): global conn conn.close() conn2 = Connection(AMQP_URL) with conn2.channel() as channel: channel.exchange_delete("unit_test_room") with conn2.channel() as channel: channel.queue_delete(queue='listener1') with conn2.channel() as channel: channel.queue_delete(queue='listener2') conn2.close()
def tearDown(self): conn = Connection(self.amqp_url) conn.connect() channel = conn.allocate_channel() for queue in self.declared_queues: try: channel.queue_delete(queue=queue) except Exception: channel = conn.allocate_channel() for exchange in self.declared_exchanges: try: channel.exchange_delete(exchange=exchange) except Exception: channel = conn.allocate_channel() conn.close()
def test_broken_ack_on_close(self): client = Connection(self.amqp_url) client.connect() with client.channel() as channel: decl = channel.queue_declare() qname = decl.queue channel.basic_publish(exchange='', routing_key=qname, body='a') r = channel.basic_get(queue=qname) self.assertEquals(r.body, 'a') channel.queue_delete(queue=qname) client.close()
def test_broken_ack_on_close(self): client = Connection(self.amqp_url) client.connect() with client.channel() as channel: decl = channel.queue_declare() qname = decl.queue channel.basic_publish(exchange='', routing_key=qname, body='a') r = channel.basic_get(queue=qname) self.assertEquals(r.body, 'a') channel.queue_delete(queue=qname) client.close()
def test_bug3_wait(self): conn = Connection(self.amqp_url) qname = 'test-bug3-%s' % random.random() with conn.channel() as channel: channel.queue_declare(queue=qname) for i in range(3): channel.basic_publish(routing_key=qname, body='x') q = channel.basic_consume(qname) for i in range(3): msg = q.get() channel.basic_ack(msg.delivery_tag) self.assertEqual(msg.body, 'x') self._epilogue(qname, 0) conn.close()
def test_bug3_wait(self): conn = Connection(self.amqp_url) qname = 'test-bug3-%s' % random.random() with conn.channel() as channel: channel.queue_declare(queue=qname) for i in range(3): channel.basic_publish( routing_key=qname, body='x' ) q = channel.basic_consume(qname) for i in range(3): msg = q.get() channel.basic_ack(msg.delivery_tag) self.assertEqual(msg.body, 'x') self._epilogue(qname, 0) conn.close()
logging.basicConfig() logger = logging.getLogger('test_app') logger.setLevel(logging.DEBUG) conn = Connection( os.environ.get('AMQP_URL', 'amqp://*****:*****@localhost/'), heartbeat=5 ) @conn.on_connect def on_connect(conn): # send some test messages with conn.channel() as channel: channel.basic_publish( exchange='manage', routing_key='all.apps.launch', body='{"application": "apps.pinger.pinger"}', ) gevent.sleep(3.0) conn.close() if __name__ == '__main__': conn.connect() try: conn.join() except KeyboardInterrupt: conn.close()
def test_queue_declare(self): """We can declare an autodelete queue.""" conn = Connection(self.amqp_url) with conn.channel() as channel: resp = channel.queue_declare(auto_delete=True) conn.close()
def test_queue_declare(self): """We can declare an autodelete queue.""" conn = Connection(self.amqp_url) with conn.channel() as channel: resp = channel.queue_declare(auto_delete=True) conn.close()
@conn.on_connect def on_connect(conn): global last_num with conn.channel() as channel: channel.exchange_declare(exchange='example', type='direct', durable=True) channel.confirm_select() while True: channel.basic_publish(exchange='example', routing_key='counter', body=str(last_num), headers={ 'delivery_mode': 2, 'time': str(time.time()) }) print(datetime.datetime.now().strftime('[%H:%M:%S]'), "Published message '%d'" % last_num) last_num += 1 gevent.sleep(1) if __name__ == '__main__': conn.connect() try: conn.join() except KeyboardInterrupt: conn.close()