Amazon Simple Queue Service
Developer Guide

The AWS Documentation website is getting a new look!
Try it now and let us know what you think. Switch to the new look >>

You can return to the original look by selecting English in the language selector above.

Working Java Example for FIFO Queues

Learn about Amazon SQS FIFO queue functionality using the provided Maven prerequisites and example Java code.


Add the aws-java-sdk-sqs.jar package to your Java class path. The following example shows this dependency in a Maven project pom.xml file.

<dependencies> <dependency> <groupId>com.amazonaws</groupId> <artifactId>aws-java-sdk-sqs</artifactId> <version><replaceable>LATEST</replaceable></version> </dependency> </dependencies>

The following example Java code creates a queue and sends, receives, and deletes a message.

/* * Copyright 2010-2019, Inc. or its affiliates. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"). * You may not use this file except in compliance with the License. * A copy of the License is located at * * * * or in the "license" file accompanying this file. This file is distributed * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either * express or implied. See the License for the specific language governing * permissions and limitations under the License. * */ import com.amazonaws.AmazonClientException; import com.amazonaws.AmazonServiceException; import; import; import*; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; public class SQSFIFOJavaClientExample { public static void main(String[] args) { /* * Create a new instance of the builder with all defaults (credentials * and region) set automatically. For more information, see * Creating Service Clients in the AWS SDK for Java Developer Guide. */ final AmazonSQS sqs = AmazonSQSClientBuilder.defaultClient(); System.out.println("==========================================="); System.out.println("Getting Started with Amazon SQS FIFO Queues"); System.out.println("===========================================\n"); try { // Create a FIFO queue. System.out.println("Creating a new Amazon SQS FIFO queue called " + "MyFifoQueue.fifo.\n"); final Map<String, String> attributes = new HashMap<>(); // A FIFO queue must have the FifoQueue attribute set to true. attributes.put("FifoQueue", "true"); /* * If the user doesn't provide a MessageDeduplicationId, generate a * MessageDeduplicationId based on the content. */ attributes.put("ContentBasedDeduplication", "true"); // The FIFO queue name must end with the .fifo suffix. final CreateQueueRequest createQueueRequest = new CreateQueueRequest("MyFifoQueue.fifo") .withAttributes(attributes); final String myQueueUrl = sqs.createQueue(createQueueRequest).getQueueUrl(); // List all queues. System.out.println("Listing all queues in your account.\n"); for (final String queueUrl : sqs.listQueues().getQueueUrls()) { System.out.println(" QueueUrl: " + queueUrl); } System.out.println(); // Send a message. System.out.println("Sending a message to MyFifoQueue.fifo.\n"); final SendMessageRequest sendMessageRequest = new SendMessageRequest(myQueueUrl, "This is my message text."); /* * When you send messages to a FIFO queue, you must provide a * non-empty MessageGroupId. */ sendMessageRequest.setMessageGroupId("messageGroup1"); // Uncomment the following to provide the MessageDeduplicationId //sendMessageRequest.setMessageDeduplicationId("1"); final SendMessageResult sendMessageResult = sqs .sendMessage(sendMessageRequest); final String sequenceNumber = sendMessageResult.getSequenceNumber(); final String messageId = sendMessageResult.getMessageId(); System.out.println("SendMessage succeed with messageId " + messageId + ", sequence number " + sequenceNumber + "\n"); // Receive messages. System.out.println("Receiving messages from MyFifoQueue.fifo.\n"); final ReceiveMessageRequest receiveMessageRequest = new ReceiveMessageRequest(myQueueUrl); // Uncomment the following to provide the ReceiveRequestDeduplicationId //receiveMessageRequest.setReceiveRequestAttemptId("1"); final List<Message> messages = sqs.receiveMessage(receiveMessageRequest) .getMessages(); for (final Message message : messages) { System.out.println("Message"); System.out.println(" MessageId: " + message.getMessageId()); System.out.println(" ReceiptHandle: " + message.getReceiptHandle()); System.out.println(" MD5OfBody: " + message.getMD5OfBody()); System.out.println(" Body: " + message.getBody()); for (final Entry<String, String> entry : message.getAttributes() .entrySet()) { System.out.println("Attribute"); System.out.println(" Name: " + entry.getKey()); System.out.println(" Value: " + entry.getValue()); } } System.out.println(); // Delete the message. System.out.println("Deleting the message.\n"); final String messageReceiptHandle = messages.get(0).getReceiptHandle(); sqs.deleteMessage(new DeleteMessageRequest(myQueueUrl, messageReceiptHandle)); // Delete the queue. System.out.println("Deleting the queue.\n"); sqs.deleteQueue(new DeleteQueueRequest(myQueueUrl)); } catch (final AmazonServiceException ase) { System.out.println("Caught an AmazonServiceException, which means " + "your request made it to Amazon SQS, but was " + "rejected with an error response for some reason."); System.out.println("Error Message: " + ase.getMessage()); System.out.println("HTTP Status Code: " + ase.getStatusCode()); System.out.println("AWS Error Code: " + ase.getErrorCode()); System.out.println("Error Type: " + ase.getErrorType()); System.out.println("Request ID: " + ase.getRequestId()); } catch (final AmazonClientException ace) { System.out.println("Caught an AmazonClientException, which means " + "the client encountered a serious internal problem while " + "trying to communicate with Amazon SQS, such as not " + "being able to access the network."); System.out.println("Error Message: " + ace.getMessage()); } } }