Message

The Message class is used to create messages that you intend to publish to RabbitMQ and is created when a message is received by RabbitMQ by a consumer or as the result of a Queue.get() request.

API Documentation

class rabbitpy.Message(channel, body_value, properties=None, auto_id=False, opinionated=False)[source]

Created by both rabbitpy internally when a message is delivered or returned from RabbitMQ and by implementing applications, the Message class is used to publish a message to and access and respond to a message from RabbitMQ.

When specifying properties for a message, pass in a dict of key value items that match the AMQP Basic.Properties specification with a small caveat.

Due to an overlap in the AMQP specification and the Python keyword type, the type property is referred to as message_type.

The following is a list of the available properties:

  • app_id
  • content_type
  • content_encoding
  • correlation_id
  • delivery_mode
  • expiration
  • headers
  • message_id
  • message_type
  • priority
  • reply_to
  • timestamp
  • user_id

Automated features

When passing in the body value, if it is a dict or list, it will automatically be JSON serialized and the content type application/json will be set on the message properties.

When publishing a message to RabbitMQ, if the opinionated value is True and no message_id value was passed in as a property, a UUID will be generated and specified as a property of the message.

Additionally, if opinionated is True and the timestamp property is not specified when passing in properties, the current Unix epoch value will be set in the message properties.

Note

As of 0.21.0 auto_id is deprecated in favor of

opinionated and it will be removed in a future version. As of 0.22.0 opinionated is defaulted to False.

Parameters:
  • channel (rabbitpy.channel.Channel) – The channel object for the message object to act upon
  • body_value (str|bytes|unicode|memoryview|dict|json) – The message body
  • properties (dict) – A dictionary of message properties
  • auto_id (bool) – Add a message id if no properties were passed in.
  • opinionated (bool) – Automatically populate properties if True
Raises:

KeyError – Raised when an invalid property is passed in

ack(all_previous=False)[source]

Acknowledge receipt of the message to RabbitMQ. Will raise an ActionException if the message was not received from a broker.

Raises:ActionException
delivery_tag

Return the delivery tag for a message that was delivered or gotten from RabbitMQ.

Return type:int or None
exchange

Return the source exchange for a message that was delivered or gotten from RabbitMQ.

Return type:string or None
json()[source]

Deserialize the message body if it is JSON, returning the value.

Return type:any
nack(requeue=False, all_previous=False)[source]

Negatively acknowledge receipt of the message to RabbitMQ. Will raise an ActionException if the message was not received from a broker.

Parameters:
  • requeue (bool) – Requeue the message
  • all_previous (bool) – Nack all previous unacked messages up to and including this one
Raises:

ActionException

pprint(properties=False)[source]

Print a formatted representation of the message.

Parameters:properties (bool) – Include properties in the representation
publish(exchange, routing_key='', mandatory=False, immediate=False)[source]

Publish the message to the exchange with the specified routing key.

In Python 2 if the message is a unicode value it will be converted to a str using str.encode('UTF-8'). If you do not want the auto-conversion to take place, set the body to a str or bytes value prior to publishing.

In Python 3 if the message is a str value it will be converted to a bytes value using bytes(value.encode('UTF-8')). If you do not want the auto-conversion to take place, set the body to a bytes value prior to publishing.

Parameters:
  • exchange (str or rabbitpy.Exchange) – The exchange to publish the message to
  • routing_key (str) – The routing key to use
  • mandatory (bool) – Requires the message is published
  • immediate (bool) – Request immediate delivery
Returns:

bool or None

Raises:

rabbitpy.exceptions.MessageReturnedException

redelivered

Indicates if this message may have been delivered before (but not acknowledged)”

Return type:bool or None
reject(requeue=False)[source]

Reject receipt of the message to RabbitMQ. Will raise an ActionException if the message was not received from a broker.

Parameters:requeue (bool) – Requeue the message
Raises:ActionException
routing_key

Return the routing_key for a message that was delivered or gotten from RabbitMQ.

Return type:int or None