AWS Security ChangesHomeSearch

AWS code-library documentation change

Service: code-library · 2025-04-11 · Documentation low

File: code-library/latest/ug/java_2_sqs_code_examples.md

Summary

Replaced tagging operation example with comprehensive JMS interface usage examples including message sending/receiving patterns, acknowledgment modes, Spring integration, and utility methods

Security assessment

The changes focus on general JMS API usage patterns and message handling mechanics without addressing specific vulnerabilities or security controls. While acknowledgment modes impact message reliability, they don't constitute security features or vulnerability fixes.

Diff

diff --git a/code-library/latest/ug/java_2_sqs_code_examples.md b/code-library/latest/ug/java_2_sqs_code_examples.md
index e9849468d..c553201be 100644
--- a//code-library/latest/ug/java_2_sqs_code_examples.md
+++ b//code-library/latest/ug/java_2_sqs_code_examples.md
@@ -1781 +1781,818 @@ 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
+The following code example shows how to use the Amazon SQS Java Messaging Library to work with the JMS interface.
+
+**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 examples work with standard Amazon SQS queues and include:
+
+  * Sending a text message.
+
+  * Receiving messages synchronously.
+
+  * Receiving messages asynchronously.
+
+  * Receiving messages using CLIENT_ACKNOWLEDGE mode.
+
+  * Receiving messages using the UNORDERED_ACKNOWLEDGE mode.
+
+  * Using Spring to inject dependencies.
+
+  * A utility class that provides common methods used by the other examples.
+
+
+
+
+For more information on using JMS with Amazon SQS, see the [Amazon SQS Developer Guide](https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-java-message-service-jms-client.html). 
+
+Sending a text message.
+    
+    
+        /**
+         * This method establishes a connection to a standard Amazon SQS queue using the Amazon SQS
+         * Java Messaging Library and sends text messages to it. It uses JMS (Java Message Service) API
+         * with automatic acknowledgment mode to ensure reliable message delivery, and automatically
+         * manages all messaging resources.
+         *
+         * @throws JMSException If there is a problem connecting to or sending messages to the queue
+         */
+        public static void doSendTextMessage() throws JMSException {
+            // Create a connection factory.
+            SQSConnectionFactory connectionFactory = new SQSConnectionFactory(
+                    new ProviderConfiguration(),
+                    SqsClient.create()
+            );
+    
+            // Create the connection in a try-with-resources statement so that it's closed automatically.
+            try (SQSConnection connection = connectionFactory.createConnection()) {
+    
+                // Create the queue if needed.
+                SqsJmsExampleUtils.ensureQueueExists(connection, QUEUE_NAME, SqsJmsExampleUtils.QUEUE_VISIBILITY_TIMEOUT);
+    
+                // Create a session that uses the JMS auto-acknowledge mode.
+                Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+                MessageProducer producer = session.createProducer(session.createQueue(QUEUE_NAME));
+    
+                createAndSendMessages(session, producer);
+            } // The connection closes automatically. This also closes the session.
+            LOGGER.info("Connection closed");
+        }
+    
+        /**
+         * This method reads text input from the keyboard and sends each line as a separate message
+         * to a standard Amazon SQS queue using the Amazon SQS Java Messaging Library. It continues
+         * to accept input until the user enters an empty line, using JMS (Java Message Service) API to
+         * handle the message delivery.
+         *
+         * @param session The JMS session used to create messages
+         * @param producer The JMS message producer used to send messages to the queue
+         */
+        private static void createAndSendMessages(Session session, MessageProducer producer) {
+            BufferedReader inputReader = new BufferedReader(
+                    new InputStreamReader(System.in, Charset.defaultCharset()));
+    
+            try {
+                String input;
+                while (true) {
+                    LOGGER.info("Enter message to send (leave empty to exit): ");
+                    input = inputReader.readLine();
+                    if (input == null || input.isEmpty()) break;
+    
+                    TextMessage message = session.createTextMessage(input);
+                    producer.send(message);
+                    LOGGER.info("Send message {}", message.getJMSMessageID());
+                }
+            } catch (EOFException e) {
+                // Just return on EOF
+            } catch (IOException e) {
+                LOGGER.error("Failed reading input: {}", e.getMessage(), e);
+            } catch (JMSException e) {
+                LOGGER.error("Failed sending message: {}", e.getMessage(), e);
+            }
+        }
+    
+    
+
+Receiving messages synchronously.
+    
+    
+        /**
+         * This method receives messages from a standard Amazon SQS queue using the Amazon SQS Java
+         * Messaging Library. It creates a connection to the queue using JMS (Java Message Service),
+         * waits for messages to arrive, and processes them one at a time. The method handles all
+         * necessary setup and cleanup of messaging resources.
+         *
+         * @throws JMSException If there is a problem connecting to or receiving messages from the queue
+         */
+        public static void doReceiveMessageSync() throws JMSException {
+            // Create a connection factory.
+            SQSConnectionFactory connectionFactory = new SQSConnectionFactory(
+                    new ProviderConfiguration(),
+                    SqsClient.create()
+            );
+    
+            // Create a connection.
+            try (SQSConnection connection = connectionFactory.createConnection() ) {
+    
+                // Create the queue if needed.
+                SqsJmsExampleUtils.ensureQueueExists(connection, QUEUE_NAME, SqsJmsExampleUtils.QUEUE_VISIBILITY_TIMEOUT);
+    
+                // Create a session.
+                Session session = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
+                MessageConsumer consumer = session.createConsumer(session.createQueue(QUEUE_NAME));
+    
+                connection.start();
+    
+                receiveMessages(consumer);
+            }  // The connection closes automatically. This also closes the session.
+            LOGGER.info("Connection closed");
+        }
+    
+        /**
+         * This method continuously checks for new messages from a standard Amazon SQS queue using
+         * the Amazon SQS Java Messaging Library. It waits up to 20 seconds for each message, processes
+         * it using JMS (Java Message Service), and confirms receipt. The method stops checking for
+         * messages after 20 seconds of no activity.
+         *
+         * @param consumer The JMS message consumer that receives messages from the queue
+         */
+        private static void receiveMessages(MessageConsumer consumer) {
+            try {
+                while (true) {
+                    LOGGER.info("Waiting for messages...");
+                    // Wait 1 minute for a message
+                    Message message = consumer.receive(Duration.ofSeconds(20).toMillis());
+                    if (message == null) {
+                        LOGGER.info("Shutting down after 20 seconds of silence.");
+                        break;
+                    }
+                    SqsJmsExampleUtils.handleMessage(message);
+                    message.acknowledge();
+                    LOGGER.info("Acknowledged message {}", message.getJMSMessageID());
+                }
+            } catch (JMSException e) {
+                LOGGER.error("Error receiving from SQS: {}", e.getMessage(), e);
+            }
+        }
+    
+    
+
+Receiving messages asynchronously.
+    
+    
+        /**
+         * This method sets up automatic message handling for a standard Amazon SQS queue using the
+         * Amazon SQS Java Messaging Library. It creates a listener that processes messages as soon
+         * as they arrive using JMS (Java Message Service), runs for 5 seconds, then cleans up all
+         * messaging resources.
+         *
+         * @throws JMSException If there is a problem connecting to or receiving messages from the queue
+         */
+        public static void doReceiveMessageAsync() throws JMSException {
+            // Create a connection factory.
+            SQSConnectionFactory connectionFactory = new SQSConnectionFactory(
+                    new ProviderConfiguration(),
+                    SqsClient.create()
+            );
+    
+            // Create a connection.
+            try (SQSConnection connection = connectionFactory.createConnection() ) {
+    
+                // Create the queue if needed.
+                SqsJmsExampleUtils.ensureQueueExists(connection, QUEUE_NAME, SqsJmsExampleUtils.QUEUE_VISIBILITY_TIMEOUT);
+    
+                // Create a session.
+                Session session = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
+    
+                try {
+                    // Create a consumer for the queue.
+                    MessageConsumer consumer = session.createConsumer(session.createQueue(QUEUE_NAME));
+                    // Provide an implementation of the MessageListener interface, which has a single 'onMessage' method.
+                    // We use a lambda expression for the implementation.