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
, thetype
property is referred to asmessage_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 nomessage_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 thetimestamp
property is not specified when passing inproperties
, the current Unix epoch value will be set in the message properties.Note
As of 0.21.0
auto_id
is deprecated in favor ofopinionated
and it will be removed in a future version. As of 0.22.0opinionated
is defaulted toFalse
.- Parameters:
channel (
rabbitpy.channel.Channel
) – The channel object for the message object to act uponbody_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
- property delivery_tag¶
Return the delivery tag for a message that was delivered or gotten from RabbitMQ.
- Return type:
int or None
- property exchange¶
Return the source exchange for a message that was delivered or gotten from RabbitMQ.
- Return type:
string or None
- 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 astr
usingstr.encode('UTF-8')
. If you do not want the auto-conversion to take place, set the body to astr
orbytes
value prior to publishing.In Python 3 if the message is a
str
value it will be converted to abytes
value usingbytes(value.encode('UTF-8'))
. If you do not want the auto-conversion to take place, set the body to abytes
value prior to publishing.- Parameters:
exchange (str or
rabbitpy.Exchange
) – The exchange to publish the message torouting_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
- property 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
- property routing_key¶
Return the routing_key for a message that was delivered or gotten from RabbitMQ.
- Return type:
int or None