PubSub Module

The pubsub module provides interface for the rabbitmq client.

It provides classes to create pika clients to connect to rabbitmq broker server, interact with and publish/subscribe to rabbitmq via creating channels, methods to publish, subscribe/consume, stop consuming, start publishing, start connection, stop connection, create channel, close channel, acknowledge delivery by publisher, acknowledge receiving of messages by consumers, send basic ack, basic cancel requests and also add callbacks for various other events.

RabbitMqClient

class rabbitChat.apps.rabbitmq.pubsub.RabbitMqClient(credentials=None, params=None, queue=None)[source]

This is a RabbitMQ Client using the TornadoConnection Adapter that will handle unexpected interactions with RabbitMQ such as channel and connection closures.

If RabbitMQ closes the connection, it will reopen it. You should look at the output, as there are limited reasons why the connection may be closed, which usually are tied to permission related issues or socket timeouts.

If the channel is closed, it will indicate a problem with one of the commands that were issued and that should surface in the output as well.

It alos uses delivery confirmations and illustrates one way to keep track of messages that have been sent and if they’ve been confirmed by RabbitMQ.

__init__(credentials=None, params=None, queue=None)[source]

Create a new instance of the consumer class, passing in the AMQP URL used to connect to RabbitMQ.

Parameters:
  • credentials (pika.credentials.PlainCredentials) – credentials to connect to rabbitmq broker server
  • params (pika.connection.ConnectionParameters) – connection paramaters used to connect with rabbitmq broker server
  • queue (string - random long base64 url safe encoded string) – queue to be created after a channel is established which will be bound to an exchange
connect()[source]

This method connects to RabbitMQ via the Torando Connectoin Adapter, returning the connection handle.

When the connection is established, the on_connection_open method will be invoked by pika.

Returns:Returns a pika connection object which is a tornado connection object to rabbitmq server
Return type:pika.adapters.TornadoConnection
on_connection_opened(connection)[source]

This method is called by pika once the connection to RabbitMQ has been established. It passes the handle to the connection object in case we need it, but in this case, we’ll just mark it unused.

Parameters:connection (pika.adapters.TornadoConnection) – connection object created
add_on_connection_close_callback()[source]

This method adds an on close callback that will be invoked by pika when RabbitMQ closes the connection to the publisher unexpectedly.

on_connection_closed(connection, reply_code, reply_text)[source]

This method is invoked by pika when the connection to RabbitMQ is closed unexpectedly. Since it is unexpected, we will reconnect to RabbitMQ if it disconnects.

Parameters:
  • connection (pika.connection.Connection) – The closed connection obj
  • reply_code (int) – The server provided reply_code if given
  • reply_text (str) – The server provided reply_text if given
reconnect()[source]

Will be invoked by the IOLoop timer if the connection is closed. See the on_connection_closed method.

close_connection()[source]

This method closes the connection to RabbitMQ.

open_channel()[source]

Open a new channel with RabbitMQ by issuing the Channel.Open RPC command. When RabbitMQ responds that the channel is open, the on_channel_open callback will be invoked by pika.

on_channel_open(channel)[source]

This method is invoked by pika when the channel has been opened. The channel object is passed in so we can make use of it.

Since the channel is now open, we’ll declare the exchange to use.

Parameters:channel (pika.channel.Channel) – The channel object
close_channel()[source]

Call to close the channel with RabbitMQ cleanly by issuing the Channel.Close RPC command.

add_on_channel_close_callback()[source]

This method tells pika to call the on_channel_closed method if RabbitMQ unexpectedly closes the channel.

on_channel_closed(channel, reply_code, reply_text)[source]

Invoked by pika when RabbitMQ unexpectedly closes the channel. Channels are usually closed if you attempt to do something that violates the protocol, such as re-declare an exchange or queue with different parameters. In this case, we’ll close the connection to shutdown the object.

Parameters:
  • channel (pika.channel.Channel) – The closed channel
  • reply_code (int) – The numeric reason the channel was closed
  • reply_text (str) – The text reason the channel was closed
setup_exchange()[source]

Setup the exchange on RabbitMQ by invoking the Exchange.Declare RPC command. When it is complete, the on_exchange_declareok method will be invoked by pika.

on_exchange_declareok(frame)[source]

Invoked by pika when RabbitMQ has finished the Exchange.Declare RPC command.

Parameters:frame (pika.Frame.Method) – Exchange.DeclareOk response frame
setup_queue()[source]

Setup the queue on RabbitMQ by invoking the Queue.Declare RPC command. When it is complete, the on_queue_declareok method will be invoked by pika.

on_queue_declareok(method_frame)[source]

Method invoked by pika when the Queue.Declare RPC call made in setup_queue has completed. mand is complete, the on_bindok method will be invoked by pika.

Parameters:method_frame (pika.frame.Method) – The Queue.DeclareOk frame
bind_queue(binding_key)[source]

In this method we will bind the queue and exchange together with the routing key by issuing the Queue.Bind RPC command.

Parameters:binding_key (string) – The routing_key argument
on_bindok(unused_frame)[source]

Invoked by pika when the Queue.Bind method has completed. At this point we will start consuming messages by calling start_consuming which will invoke the needed RPC commands to start the process. It will also set channel for publishing.

Parameters:unused_frame (pika.frame.Method) – The Queue.BindOk response frame
setup_publishing()[source]

In this method we will setup the channel for publishing by making it available for delivery confirmations and publisher confirmations.

enable_delivery_confirmations()[source]

Send the Confirm.Select RPC method to RabbitMQ to enable delivery confirmations on the channel. The only way to turn this off is to close the channel and create a new one.

When the message is confirmed from RabbitMQ, the on_delivery_confirmation method will be invoked passing in a Basic.Ack or Basic.Nack method from RabbitMQ that will indicate which messages it is confirming or rejecting.

on_delivery_confirmation(method_frame)[source]

Invoked by pika when RabbitMQ responds to a Basic.Publish RPC command, passing in either a Basic.Ack or Basic.Nack frame with the delivery tag of the message that was published. The delivery tag is an integer counter indicating the message number that was sent on the channel via Basic.Publish. Here we’re just doing house keeping to keep track of stats and remove message numbers that we expect a delivery confirmation of from the list used to keep track of messages that are pending confirmation.

Parameters:method_frame (pika.frame.Method) – Basic.Ack or Basic.Nack frame
publish(msg, routing_key)[source]

If the class is not stopping, publish a message to RabbitMQ, appending a list of deliveries with the message number that was sent. This list will be used to check for delivery confirmations in the on_delivery_confirmations method.

Once the message has been sent, schedule another message to be sent. The main reason I put scheduling in was just so you can get a good idea of how the process is flowing by slowing down and speeding up the delivery intervals by changing the PUBLISH_INTERVAL constant in the class.

Parameters:
  • msg – Message to be published to Channel
  • routing_key (string) – Routing Key to direct message via the Exchange
Tyep msg:

string

start_consuming()[source]

This method sets up the consumer by first calling add_on_cancel_callback so that the object is notified if RabbitMQ cancels the consumer. It then issues the Basic.Consume RPC command which returns the consumer tag that is used to uniquely identify the consumer with RabbitMQ. We keep the value to use it when we want to cancel consuming. The on_message method is passed in as a callback pika will invoke when a message is fully received.

add_on_cancel_callback()[source]

Add a callback that will be invoked if RabbitMQ cancels the consumer for some reason. If RabbitMQ does cancel the consumer, on_consumer_cancelled will be invoked by pika.

on_consumer_cancelled(method_frame)[source]

Invoked by pika when RabbitMQ sends a Basic.Cancel for a consumer receiving messages.

Parameters:method_frame (pika.frame.Method) – The Basic.Cancel frame
on_message(unused_channel, basic_deliver, properties, body)[source]

Invoked by pika when a message is delivered from RabbitMQ. The channel is passed for your convenience. The basic_deliver object that is passed in carries the exchange, routing key, delivery tag and a redelivered flag for the message. The properties passed in is an instance of BasicProperties with the message properties and the body is the message that was sent.

Parameters:
  • unused_channel (pika.channel.Channel) – The channel object
  • basic_deliver (pika.Spec.Basic.Deliver) – The basic delivery object passed
  • properties (pika.Spec.BasicProperties) – The basic properties used to publish the message
  • body (str|unicode) – The message body
acknowledge_message(delivery_tag)[source]

Acknowledge the message delivery from RabbitMQ by sending a Basic.Ack RPC method for the delivery tag.

Parameters:delivery_tag (int) – The delivery tag from the Basic.Deliver frame
stop_consuming()[source]

Tell RabbitMQ that you would like to stop consuming by sending the Basic.Cancel RPC command.

on_cancelok(unused_frame)[source]

This method is invoked by pika when RabbitMQ acknowledges the cancellation of a consumer. At this point we will close the channel. This will invoke the on_channel_closed method once the channel has been closed, which will in-turn close the connection.

Parameters:unused_frame (pika.frame.Method) – The Basic.CancelOk frame
start()[source]

Run the example consumer by connecting to RabbitMQ and then starting the IOLoop to block and allow the SelectConnection to operate.

stop()[source]

Cleanly shutdown the connection to RabbitMQ by stopping the consumer with RabbitMQ. When RabbitMQ confirms the cancellation, on_cancelok will be invoked by pika, which will then closing the channel and connection. The IOLoop is started again because this method is invoked when CTRL-C is pressed raising a KeyboardInterrupt exception. This exception stops the IOLoop which needs to be running for pika to communicate with RabbitMQ. All of the commands issued prior to starting the IOLoop will be buffered but not processed.

status()[source]

Gives the status of the RabbitMQClient Connection.

Returns:Returns the current status of the connection
Return type:self._status
__class__

alias of type

__delattr__

x.__delattr__(‘name’) <==> del x.name

__dict__ = dict_proxy({'setup_queue': <function setup_queue>, '__module__': 'rabbitChat.apps.rabbitmq.pubsub', 'close_channel': <function close_channel>, 'stop_consuming': <function stop_consuming>, 'add_on_connection_close_callback': <function add_on_connection_close_callback>, 'connect': <function connect>, 'reconnect': <function reconnect>, '__dict__': <attribute '__dict__' of 'RabbitMqClient' objects>, '__weakref__': <attribute '__weakref__' of 'RabbitMqClient' objects>, '__init__': <function __init__>, 'on_consumer_cancelled': <function on_consumer_cancelled>, 'on_connection_closed': <function on_connection_closed>, 'on_message': <function on_message>, 'publish': <function publish>, 'start': <function start>, 'on_channel_open': <function on_channel_open>, 'bind_queue': <function bind_queue>, 'on_queue_declareok': <function on_queue_declareok>, '__doc__': "\n This is a RabbitMQ Client using the TornadoConnection Adapter that will\n handle unexpected interactions with RabbitMQ such as channel and connection closures.\n\n If RabbitMQ closes the connection, it will reopen it. You should\n look at the output, as there are limited reasons why the connection may\n be closed, which usually are tied to permission related issues or\n socket timeouts.\n\n If the channel is closed, it will indicate a problem with one of the\n commands that were issued and that should surface in the output as well.\n\n It alos uses delivery confirmations and illustrates one way to keep track of\n messages that have been sent and if they've been confirmed by RabbitMQ.\n\n ", 'status': <function status>, 'stop': <function stop>, 'on_delivery_confirmation': <function on_delivery_confirmation>, 'add_on_channel_close_callback': <function add_on_channel_close_callback>, 'setup_exchange': <function setup_exchange>, 'on_bindok': <function on_bindok>, 'setup_publishing': <function setup_publishing>, 'enable_delivery_confirmations': <function enable_delivery_confirmations>, 'acknowledge_message': <function acknowledge_message>, 'on_channel_closed': <function on_channel_closed>, 'on_exchange_declareok': <function on_exchange_declareok>, 'add_on_cancel_callback': <function add_on_cancel_callback>, 'open_channel': <function open_channel>, 'on_connection_opened': <function on_connection_opened>, 'close_connection': <function close_connection>, 'on_cancelok': <function on_cancelok>, 'start_consuming': <function start_consuming>})
__format__()

default object formatter

__getattribute__

x.__getattribute__(‘name’) <==> x.name

__hash__
__module__ = 'rabbitChat.apps.rabbitmq.pubsub'
__new__(S, ...) → a new object with type S, a subtype of T
__reduce__()

helper for pickle

__reduce_ex__()

helper for pickle

__repr__
__setattr__

x.__setattr__(‘name’, value) <==> x.name = value

__sizeof__() → int

size of object in memory, in bytes

__str__
__subclasshook__()

Abstract classes can override this to customize issubclass().

This is invoked early on by abc.ABCMeta.__subclasscheck__(). It should return True, False or NotImplemented. If it returns NotImplemented, the normal algorithm is used. Otherwise, it overrides the normal algorithm (and the outcome is cached).

__weakref__

list of weak references to the object (if defined)