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, thetypeproperty is referred to asmessage_type.The following is a list of the available properties:
- app_id
- content_type
- content_encoding
- correlation_id
- delivery_node
- 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/jsonwill be set on the message properties.When publishing a message to RabbitMQ, if the opinionated value is
Trueand nomessage_idvalue was passed in as a property, a UUID will be generated and specified as a property of the message.Additionally, if opinionated is
Trueand thetimestampproperty 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_idis deprecated in favor ofopinionatedand it will be removed in a future version. As of 0.22.0opinionatedis defaulted toFalse.Parameters: - channel (
rabbitpy.channel.Channel) – The channel object for the message object to act upon - or dict or list body_value (str) – 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
-
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: 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)[source]¶ 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 astrusingstr.encode('UTF-8'). If you do not want the auto-conversion to take place, set the body to astrorbytesvalue prior to publishing.In Python 3 if the message is a
strvalue it will be converted to abytesvalue usingbytes(value.encode('UTF-8')). If you do not want the auto-conversion to take place, set the body to abytesvalue 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
Returns: bool or None
Raises: rabbitpy.exceptions.MessageReturnedException
- exchange (str or
-
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