Message attributes in Simple Message Queue (formerly MNS) are custom metadata that can be added to messages to support a wide range of application scenarios.
Components
Each message attribute consists of the following parts:
Name: The unique identifier for an attribute. The name is a case-sensitive string of up to 256 characters and must be unique among all attributes for the message.
Type: The data type of the attribute value. Supported types include
String,Number,Boolean, andBinary.Value: The content of the attribute, with a maximum length of 4,096 characters.
NoteFor
Binary, the value should be encoded in Base64.
Data types
The following table describes the four supported attribute types:
Data type | Value | Description |
| String | Stores any valid Unicode text. |
| String | Stores positive or negative numeric values, including integers, floating-point numbers, and scientific notation. Simple Message Queue (formerly MNS) does not validate that the value is a number during transmission but processes it as a string. |
|
| Stores a Boolean value. The value must be the string |
| Base64-encoded string | Stores any binary data, such as compressed data, encrypted data, or images. The data must be Base64-encoded by the SDK or client when sent, and decoded when received. |
All attribute values, including binary data, are transmitted as strings over HTTP-based protocols. To ensure the fidelity of the Binary type, the SDK automatically encodes and decodes the value using Base64. If you do not use the SDK provided by Simple Message Queue (formerly MNS), you must handle the Base64 encoding and decoding for the Binary type yourself.
Limits
Number of attributes
Each message can contain a maximum of 50 message attributes.
Attribute name
Can contain only the following characters:
A-Z,a-z,0-9, underscore (_), hyphen (-), and period (.).Can be up to 256 characters long.
Is case-sensitive.
Must be unique among all attribute names for the message.
Cannot start or end with a period (
.).Cannot contain consecutive periods (
.), such as"example..name".
Attribute value
The length of an attribute value cannot exceed 4,096 characters.
Total message size
The total size of all message attributes, including names, types, and values, is included in the total message size limit in Simple Message Queue (formerly MNS). For more information about the size limit, see Limits.
Examples
Write message attributes
// 1. Create a message object.
Message message = new Message();
message.setMessageBody("This is the message content");
// 2. Create a map to store the message attributes.
Map<String, MessagePropertyValue> userProperties = new HashMap<>();
// 3. Add attributes for each supported type.
// String
userProperties.put("city", new MessagePropertyValue("hangzhou"));
// Number (integer)
userProperties.put("priority", new MessagePropertyValue(10));
// Number (floating-point number)
userProperties.put("temperature", new MessagePropertyValue(-5.5));
// Boolean
userProperties.put("is_vip", new MessagePropertyValue(true));
// Binary
byte[] binaryData = "some-binary-data".getBytes();
userProperties.put("attachment", new MessagePropertyValue(binaryData));
// 4. Set the attributes for the message.
message.setUserProperties(userProperties);Read message attributes
// Assume that 'receivedMessage' is a message object that you have obtained.
// Message receivedMessage = ...;
// 1. Get the attribute map from the message.
Map<String, MessagePropertyValue> userProperties = receivedMessage.getUserProperties();
if (userProperties != null && !userProperties.isEmpty()) {
// 2. Read and parse the attributes of each type.
// Read String
String city = userProperties.get("city").getStringValue();
System.out.println("City (String): " + city);
// Read Number (and parse as Double)
double temperature = Double.parseDouble(userProperties.get("temperature").getStringValue());
System.out.println("Temperature (Number as Double): " + temperature);
// Read Number (and parse as Integer)
int priority = Integer.parseInt(userProperties.get("priority").getStringValue());
System.out.println("Priority (Number as Integer): " + priority);
// Read Boolean (and parse)
boolean isVip = Boolean.parseBoolean(userProperties.get("is_vip").getStringValue());
System.out.println("Is VIP (Boolean): " + isVip);
// Read Binary
byte[] attachment = userProperties.get("attachment").getBinaryValue();
System.out.println("Attachment (Binary): " + new String(attachment));
}Read message attributes at subscription endpoints
Message attributes are supported only for queue and HTTP subscriptions.
Queue subscriptions
When a message is pushed to a Simple Message Queue (formerly MNS) queue subscription endpoint, the message attributes are passed as part of the Message object. You can receive the message in the same way as a normal message, and then access userProperties using the methods that the Message object provides. For an example, see Read message attributes.
HTTP subscriptions
When a message is pushed to an HTTP subscription endpoint, the format of the message attributes depends on the Message Format that you specify when you create the subscription (the NotifyContentFormat property of the subscription):
JSON: If the format is
JSON, theUserPropertiesmessage attributes are included as aJSONobject in the body of the pushed HTTP POST request. Each attribute contains aTypefield and aValuefield.XML: If the format is
XML, theUserPropertiesmessage attributes are included as anXMLelement in the body of the pushed HTTP POST request.SIMPLIFIED: If the format is
SIMPLIFIED, Simple Message Queue (formerly MNS) pushes only the raw message content. Message attributes cannot be passed.
References
For more information about the operations for Simple Message Queue (formerly MNS) subscriptions, see the following topics: