Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions,
see Getting Started with Amazon Web Services in China
(PDF).
Sending message attributes to an
Amazon SQS queue
You can include structured metadata (such as timestamps, geospatial data, signatures, and identifiers) with messages using message attributes. For more information, see Amazon SQS message attributes.
Before you run the example code, make sure that you have set your Amazon credentials. For
more information, see Set up Amazon Credentials and Region for Development
in the Amazon SDK for Java 2.x Developer Guide.
Defining attributes
To define an attribute for a message, add the following code, which uses the
MessageAttributeValue
data type. For more information, see Message attribute components and Message attribute data types.
The Amazon SDK for Java automatically calculates the message body and message attribute
checksums and compares them with the data that Amazon SQS returns. For more information, see
the Amazon SDK for Java 2.x Developer Guide and
Calculating the MD5
message digest for message attributes for other
programming languages.
- String
-
This example defines a String
attribute named
Name
with the value Jane
.
final Map<String, MessageAttributeValue> messageAttributes = new HashMap<>();
messageAttributes.put("Name", new MessageAttributeValue()
.withDataType("String")
.withStringValue("Jane"));
- Number
-
This example defines a Number
attribute named
AccurateWeight
with the value
230.000000000000000001
.
final Map<String, MessageAttributeValue> messageAttributes = new HashMap<>();
messageAttributes.put("AccurateWeight", new MessageAttributeValue()
.withDataType("Number")
.withStringValue("230.000000000000000001"));
- Binary
-
This example defines a Binary
attribute named
ByteArray
with the value of an uninitialized 10-byte
array.
final Map<String, MessageAttributeValue> messageAttributes = new HashMap<>();
messageAttributes.put("ByteArray", new MessageAttributeValue()
.withDataType("Binary")
.withBinaryValue(ByteBuffer.wrap(new byte[10])));
- String (custom)
-
This example defines the custom attribute String.EmployeeId
named EmployeeId
with the value ABC123456
.
final Map<String, MessageAttributeValue> messageAttributes = new HashMap<>();
messageAttributes.put("EmployeeId", new MessageAttributeValue()
.withDataType("String.EmployeeId")
.withStringValue("ABC123456"));
- Number (custom)
-
This example defines the custom attribute Number.AccountId
named AccountId
with the value 000123456
.
final Map<String, MessageAttributeValue> messageAttributes = new HashMap<>();
messageAttributes.put("AccountId", new MessageAttributeValue()
.withDataType("Number.AccountId")
.withStringValue("000123456"));
Because the base data type is Number
, the ReceiveMessage
method returns
123456
.
- Binary (custom)
-
This example defines the custom attribute Binary.JPEG
named
ApplicationIcon
with the value of an uninitialized 10-byte
array.
final Map<String, MessageAttributeValue> messageAttributes = new HashMap<>();
messageAttributes.put("ApplicationIcon", new MessageAttributeValue()
.withDataType("Binary.JPEG")
.withBinaryValue(ByteBuffer.wrap(new byte[10])));
Sending a message with attributes
This example adds the attributes to the SendMessageRequest
before sending
the message.
// Send a message with an attribute.
final SendMessageRequest sendMessageRequest = new SendMessageRequest();
sendMessageRequest.withMessageBody("This is my message text.");
sendMessageRequest.withQueueUrl(myQueueUrl);
sendMessageRequest.withMessageAttributes(messageAttributes);
sqs.sendMessage(sendMessageRequest);
If you send a message to a First-In-First-Out (FIFO) queue, make sure that the
sendMessage
method executes after you provide
the message group ID.
If you use the SendMessageBatch
method instead of SendMessage
, you must
specify message attributes for each message in the batch.