Menu
Amazon Simple Queue Service
Developer Guide

Tutorial: Sending a Message with Attributes to an Amazon SQS Queue

You can include structured metadata (such as timestamps, geospatial data, signatures, and identifiers) with messages using message attributes. In this tutorial you'll learn how to send a message with attributes to an existing queue. For more information, see Amazon SQS Message Attributes.

For a more detailed explanation of sending messages to standard and FIFO queues, see Sending a Message to an Amazon SQS Queue.

AWS Management Console

  1. Sign in to the Amazon SQS console.

  2. From the queue list, select a queue.

  3. From Queue Actions, select Send a Message.

    The Send a Message to QueueName dialog box is displayed.

    The following example shows the Message Group ID and Message Deduplication ID parameters specific to FIFO queues (content-based deduplication is disabled).

  4. To send a message to a FIFO queue, type the Message Body, the Message Group ID MyMessageGroupId1234567890, and the Message Deduplication ID MyMessageDeduplicationId1234567890, and then choose Message Attributes.

  5. Define the message attribute parameters. For more information, see Message Attribute Components and Message Attribute Data Types.

    1. For the message attribute Name type MyMessageAttribute.

    2. For the message attribute data Type, select Number and type byte for the optional custom type.

    3. For the message attribute Value, type 24.

    Choose Add Attribute.

    The attribute is added to the message as Number.byte.

    You can modify the value before sending the message. To delete the attribute, choose .

  6. When you finish adding attributes to the message, choose Send Message.

    Your message is sent and the Send a Message to QueueName dialog box is displayed, showing the attributes of the sent message.

    The following example shows the MD5 of Message Attributes specific to your custom message attribute and the Sequence Number attribute specific to FIFO queues.

  7. Choose Close.

AWS SDK for Java

Before you begin working with the example code, specify your AWS credentials. For more information, see Set up AWS Credentials and Region for Development in the AWS SDK for Java Developer Guide.

To send a message with attributes to a queue

  1. Copy the standard queue example program or the FIFO queue example program.

  2. 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.

    Note

    The AWS SDK for Java automatically calculates the message body and message attribute checksums and compares them with the data which Amazon SQS returns. For more information, see the AWS SDK for Java Developer Guide and Calculating the MD5 Message Digest for Message Attributes for other programming languages.

    StringNumberBinaryString (Custom)Number (Custom)Binary (Custom)
    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 0023456.

    final Map<String, MessageAttributeValue> messageAttributes = new HashMap<>(); messageAttributes.put("AccountId", new MessageAttributeValue() .withDataType("Number.AccountId") .withStringValue("000123456"));

    Note

    Because the base data type is Number, the ReceiveMessage action 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])));
  3. Replace the section of the code that sends the message with the following:

    // 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);

    Important

    If you send a message to a FIFO queue, make sure that the sendMessage method executes after you provide the message group ID.

    If you use the SendMessageBatch action instead of SendMessage, you must specify message attributes for each individual message in the batch.

  4. Compile and run the example.

    The message is sent to the queue. The response includes the following items:

    • The message ID Amazon SQS assigns to the message.

    • An MD5 digest of the message body, used to confirm that Amazon SQS received the message correctly (for more information, see RFC1321).

    • An MD5 digest of the message attributes, used to confirm that Amazon SQS received the message attributes correctly.

    • The request ID that Amazon SQS assigned to your request.