Amazon Simple Queue Service
开发人员指南
AWS 文档中描述的 AWS 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 Amazon AWS 入门

实际可用的FIFO队列 Java 示例

使用提供的 Maven 先决条件和示例 Java 代码了解 Amazon SQS FIFO队列功能。

先决条件

aws-java-sdk-sqs.jar 程序包添加到 Java 类路径中。以下示例说明了 Maven 项目的 pom.xml 文件中的这一依赖关系。

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

SQSFIFOJavaClientExample.java

以下示例 Java 代码将创建队列并发送、接收和删除消息。

/* * Copyright 2010-2018 Amazon.com, 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 * * http://www.amazonaws.cn/apache2.0 * * 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 com.amazonaws.services.sqs.AmazonSQS; import com.amazonaws.services.sqs.AmazonSQSClientBuilder; import com.amazonaws.services.sqs.model.*; 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()); } } }