AWS code-library documentation change
Summary
Added code examples for SendMessage operation with message body, delay, and attributes
Security assessment
The changes demonstrate standard message sending functionality without explicit security context or vulnerability fixes.
Diff
diff --git a/code-library/latest/ug/java_2_sqs_code_examples.md index 2c3b4490a..cf05338c0 100644 --- a/code-library/latest/ug/java_2_sqs_code_examples.md +++ b/code-library/latest/ug/java_2_sqs_code_examples.md @@ -482,0 +483,11 @@ There's more on GitHub. Find the complete example and learn how to set up and ru +Two examples of the `SendMessage` operation follow: + + * Send a message with a body and a delay + + * Send a message with a body and message attributes + + + + +Send a message with a body and a delay. + @@ -553,0 +565,54 @@ There's more on GitHub. Find the complete example and learn how to set up and ru +Send a message with a body and message attributes. + + + /** + * <p>This method demonstrates how to add message attributes to a message. + * Each attribute must specify a name, value, and data type. You use a Java Map to supply the attributes. The map's + * key is the attribute name, and you specify the map's entry value using a builder that includes the attribute + * value and data type.</p> + * + * <p>The data type must start with one of "String", "Number" or "Binary". You can optionally + * define a custom extension by using a "." and your extension.</p> + * + * <p>The SQS Developer Guide provides more information on @see <a + * href="https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-message-metadata.html#sqs-message-attributes">message + * attributes</a>.</p> + * + * @param thumbailPath Filesystem path of the image. + * @param queueUrl URL of the SQS queue. + */ + static void sendMessageWithAttributes(Path thumbailPath, String queueUrl) { + Map<String, MessageAttributeValue> messageAttributeMap; + try { + messageAttributeMap = Map.of( + "Name", MessageAttributeValue.builder() + .stringValue("Jane Doe") + .dataType("String").build(), + "Age", MessageAttributeValue.builder() + .stringValue("42") + .dataType("Number.int").build(), + "Image", MessageAttributeValue.builder() + .binaryValue(SdkBytes.fromByteArray(Files.readAllBytes(thumbailPath))) + .dataType("Binary.jpg").build() + ); + } catch (IOException e) { + LOGGER.error("An I/O exception occurred reading thumbnail image: {}", e.getMessage(), e); + throw new RuntimeException(e); + } + + SendMessageRequest request = SendMessageRequest.builder() + .queueUrl(queueUrl) + .messageBody("Hello SQS") + .messageAttributes(messageAttributeMap) + .build(); + try { + SendMessageResponse sendMessageResponse = SQS_CLIENT.sendMessage(request); + LOGGER.info("Message ID: {}", sendMessageResponse.messageId()); + } catch (SqsException e) { + LOGGER.error("Exception occurred sending message: {}", e.getMessage(), e); + throw new RuntimeException(e); + } + } + + + @@ -583,0 +649,56 @@ There's more on GitHub. Find the complete example and learn how to set up and ru +The following code example shows how to use `SetQueueAttributes`. + +**SDK for Java 2.x** + + +###### Note + +There's more on GitHub. Find the complete example and learn how to set up and run in the [AWS Code Examples Repository](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/javav2/example_code/sqs#code-examples). + +Configure an Amazon SQS to use server-side encryption (SSE) using a custom KMS key. + + + public static void addEncryption(String queueName, String kmsMasterKeyAlias) { + SqsClient sqsClient = SqsClient.create(); + + GetQueueUrlRequest urlRequest = GetQueueUrlRequest.builder() + .queueName(queueName) + .build(); + + GetQueueUrlResponse getQueueUrlResponse; + try { + getQueueUrlResponse = sqsClient.getQueueUrl(urlRequest); + } catch (QueueDoesNotExistException e) { + LOGGER.error(e.getMessage(), e); + throw new RuntimeException(e); + } + String queueUrl = getQueueUrlResponse.queueUrl(); + + + Map<QueueAttributeName, String> attributes = Map.of( + QueueAttributeName.KMS_MASTER_KEY_ID, kmsMasterKeyAlias, + QueueAttributeName.KMS_DATA_KEY_REUSE_PERIOD_SECONDS, "140" // Set the data key reuse period to 140 seconds. + ); // This is how long SQS can reuse the data key before requesting a new one from KMS. + + SetQueueAttributesRequest attRequest = SetQueueAttributesRequest.builder() + .queueUrl(queueUrl) + .attributes(attributes) + .build(); + try { + sqsClient.setQueueAttributes(attRequest); + LOGGER.info("The attributes have been applied to {}", queueName); + } catch (InvalidAttributeNameException | InvalidAttributeValueException e) { + LOGGER.error(e.getMessage(), e); + throw new RuntimeException(e); + } finally { + sqsClient.close(); + } + } + + + + * For API details, see [SetQueueAttributes](https://docs.aws.amazon.com/goto/SdkForJavaV2/sqs-2012-11-05/SetQueueAttributes) in _AWS SDK for Java 2.x API Reference_. + + + + @@ -1656,0 +1778,134 @@ There's more on GitHub. Find the complete example and learn how to set up and ru +The following code example shows how to perform tagging operation with Amazon SQS + +**SDK for Java 2.x** + + +###### Note + +There's more on GitHub. Find the complete example and learn how to set up and run in the [AWS Code Examples Repository](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/javav2/example_code/sqs#code-examples). + +The following example creates tags for a queue, lists tags, and removes a tag. + + + + import org.slf4j.Logger; + import org.slf4j.LoggerFactory; + import software.amazon.awssdk.services.sqs.SqsClient; + import software.amazon.awssdk.services.sqs.model.ListQueueTagsResponse; + import software.amazon.awssdk.services.sqs.model.QueueDoesNotExistException; + import software.amazon.awssdk.services.sqs.model.SqsException; + + import java.util.Map; + import java.util.UUID; + + /** + * Before running this Java V2 code example, set up your development environment, including your credentials. For more + * information, see the <a href="https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html">AWS + * SDK for Java Developer Guide</a>. + */ + public class TagExamples { + static final SqsClient sqsClient = SqsClient.create(); + static final String queueName = "TagExamples-queue-" + UUID.randomUUID().toString().replace("-", "").substring(0, 20); + private static final Logger LOGGER = LoggerFactory.getLogger(TagExamples.class); + + public static void main(String[] args) { + final String queueUrl; + try { + queueUrl = sqsClient.createQueue(b -> b.queueName(queueName)).queueUrl(); + LOGGER.info("Queue created. The URL is: {}", queueUrl); + } catch (RuntimeException e) { + LOGGER.error("Program ending because queue was not created."); + throw new RuntimeException(e); + } + try { + addTags(queueUrl); + listTags(queueUrl); + removeTags(queueUrl); + } catch (RuntimeException e) { + LOGGER.error("Program ending because of an error in a method."); + } finally { + try { + sqsClient.deleteQueue(b -> b.queueUrl(queueUrl)); + LOGGER.info("Queue successfully deleted. Program ending."); + sqsClient.close(); + } catch (RuntimeException e) { + LOGGER.error("Program ending."); + } finally { + sqsClient.close(); + } + } + } + + /** This method demonstrates how to use a Java Map to a tag a aueue. + * @param queueUrl The URL of the queue to tag. + */ + public static void addTags(String queueUrl) { + // Build a map of the tags. + final Map<String, String> tagsToAdd = Map.of( + "Team", "Development", + "Priority", "Beta", + "Accounting ID", "456def"); +