To track and identify messages, you can specify a message ID for each message on Message Queue for RabbitMQ producers. This article introduces the concept of message ID and describes how to specify a message ID.

What is a message ID?

A message ID is an optional message property of the short string type. In most business scenarios, a message ID is a unique identifier. For example, you must define a unique identifier for each message so that you can track and identify the messages in sales orders and tickets. Message Queue for RabbitMQ servers do not ensure the idempotence of messages. Idempotence ensures that a consumer does not change the result beyond its first iteration when it receives a message multiple times. This way, repeated consumptions do not pose additional negative effects on business systems. To achieve this, you must specify a unique identifier for each message and then implement message idempotence on Message Queue for RabbitMQ consumers. For more information, see Message idempotence.

Configuration method

Specify message IDs in the basic properties on Message Queue for RabbitMQ producers. The following code provides examples on how to specify a message ID:

AMQP.BasicProperties props = new AMQP.BasicProperties.Builder().messageId("messageid").build ();channel. basicPublish("${ExchangeName}", "BindingKey", true, props, ("Message body").getBytes(StandardCharsets.UTF_8));

properties = pika.BasicProperties(app_id='example-publisher', content_type='application/json', 'message_id'='messageid')

$msg = new AMQPMessage($msgBody, ['application_headers'=>$amqpTable,'content_type' => 'text/plain', 'delivery_mode' => 2,'message_id' => 'messageid',]);

err = ch.Publish( "helloExchange", "hello", false, false, amqp.Publishing { ContentType: "text/plain", Body: []byte(body), MessageId: "messageId", })

channel.BasicPublish(exchange: "", routingKey: "hello", basicProperties: null, body: body);