def __init__(self): _logger.info("Initializing Pika connection") self._connect() channel = self.get_channel() channel.exchange_declare(exchange="internal.edustor", exchange_type="topic", durable=True) channel.close() _logger.info("Pika initialization finished")
def __aexit__(self, exc_type, exc_val, exc_tb): for channel in tuple(self._channels.values()): yield from channel.close() yield from self.close()
def test_context_manager_exit_with_closed_channel(self): with self.obj as channel: self.assertFalse(channel._impl.close.called) channel.close() channel._impl.close.assert_called_with(reply_code=0, reply_text='Normal Shutdown')
import pika from pika import channel params = pika.URLParameters('amqps://*****:*****@beaver.rmq.cloudamqp.com/oljdtgfk') print(params) connection = pika.BlockingConnection(params) channel = connection.channel() channel.queue_declare(queue='admin') def callback(ch,method,properties,body): print('Received in admin') print(body) channel.basic_consume(queue='admin',on_message_callback=callback) print('Started') channel.start_consuming() channel.close()
print >> sys.stderr, "Error: An AMQP Connection Error occured: " + str(acoe.message) except pika.exceptions.AMQPChannelError, ache: print >> sys.stderr, "Error: An AMQP Channel Error occured: " + str(ache.message) except pika.exceptions.ChannelError, ce: print >> sys.stderr, "Error: A channel error occured: " + str(ce.message) except pika.exceptions.AMQPError, ae: print >> sys.stderr, "Error: An AMQP Error occured: " + str(ae.message) except Exception, eee: print >> sys.stderr, "Error: An unexpected exception occured: " + str(eee.message) finally: #TODO: Attempt to gracefully shutdown the connection to the message broker # Closing the channel gracefully if channel is not None: print "" print "Exited pika event loop, closing channel and the message broker" channel.close() print "Channel closed successfully!" # For closing the connection gracefully if message_broker is not None: message_broker.close() print "Message broker closed successfully, Shutting down gracefully!" except Exception, ee: # Add code here to handle the exception, print an error, and exit gracefully print >> sys.stderr, "Error: An unexpected exception occured: " + str(ee.message)
def test_context_manager_exit_with_closed_channel(self): with self.obj as channel: self.assertFalse(channel._impl.close.called) channel.close() channel._impl.close.assert_called_with(reply_code=0, reply_text='Normal Shutdown')