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
Message(channel, body_value, properties=None, auto_id=False, opinionated=False)¶
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
typeproperty is referred to as
The following is a list of the available properties:
When passing in the body value, if it is a dict or list, it will automatically be JSON serialized and the content type
application/jsonwill be set on the message properties.
When publishing a message to RabbitMQ, if the opinionated value is
message_idvalue was passed in as a property, a UUID will be generated and specified as a property of the message.
Additionally, if opinionated is
timestampproperty is not specified when passing in
properties, the current Unix epoch value will be set in the message properties.
As of 0.21.0
auto_idis deprecated in favor of
opinionatedand it will be removed in a future version. As of 0.22.0
opinionatedis defaulted to
- 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
KeyError – Raised when an invalid property is passed in
Acknowledge receipt of the message to RabbitMQ. Will raise an ActionException if the message was not received from a broker.
Return the delivery tag for a message that was delivered or gotten from RabbitMQ.
Return type: int or None
Return the source exchange for a message that was delivered or gotten from RabbitMQ.
Return type: string or None
Deserialize the message body if it is JSON, returning the value.
Return type: any
Negatively acknowledge receipt of the message to RabbitMQ. Will raise an ActionException if the message was not received from a broker.
Print a formatted representation of the message.
Parameters: properties (bool) – Include properties in the representation
publish(exchange, routing_key='', mandatory=False, immediate=False)¶
Publish the message to the exchange with the specified routing key.
In Python 2 if the message is a
unicodevalue it will be converted to a
str.encode('UTF-8'). If you do not want the auto-conversion to take place, set the body to a
bytesvalue prior to publishing.
In Python 3 if the message is a
strvalue it will be converted to a
bytes(value.encode('UTF-8')). If you do not want the auto-conversion to take place, set the body to a
bytesvalue prior to publishing.
bool or None
Indicates if this message may have been delivered before (but not acknowledged)”
Return type: bool or None
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