To track and identify messages, you can specify a unique ID for each message on the producer client of ApsaraMQ for RabbitMQ. This topic describes the definition of message ID and how to specify a message ID.
What is a message ID?
A message ID is an optional message attribute of the short string type. In most business scenarios, an ID is the unique identifier of a message. You can use message IDs to track and identify messages in scenarios such as order tracking and ticket processing. ApsaraMQ for RabbitMQ brokers do not ensure the idempotence of message consumption. Consumption idempotence means that if a message is sent to a consumer multiple times, the result of multiple consumption operations is the same as the result of one message consumption operation, and the repeated consumption operations do not have negative impacts on the business application. To ensure the idempotence of message consumption, you must specify a unique ID for each message and configure parameters that are required to implement idempotent consumption on your ApsaraMQ for RabbitMQ consumer client. For more information, see Message idempotence.
Configuration method
On the ApsaraMQ for RabbitMQ producer client, configure the message-id
parameter of the Basic.Properties
field. Sample code:
Java
AMQP.BasicProperties props = new AMQP.BasicProperties.Builder().messageId("messageid").build();
channel.basicPublish("${ExchangeName}", "RoutingKey", true, props, ("Message body").getBytes(StandardCharsets.UTF_8));
Python
properties = pika.BasicProperties(app_id='example-publisher', content_type='application/json', message_id='messageid')
PHP
$msg = new AMQPMessage($msgBody, ['application_headers'=>$amqpTable,'content_type' => 'text/plain', 'delivery_mode' => 2,'message_id' => 'messageid',]);
Go
err = ch.Publish( "helloExchange", "hello", false, false, amqp.Publishing { ContentType: "text/plain", Body: []byte(body), MessageId: "messageId", })
Node.js
channel.BasicPublish(exchange: "", routingKey: "hello", basicProperties: null, body: body);