实际可用的 标准 队列的 Java 示例 - Amazon Simple Queue Service
AWS 文档中描述的 AWS 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅中国的 AWS 服务入门

实际可用的 标准 队列的 Java 示例

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

先决条件

以下示例使用的是 AWS Java 开发工具包。要安装和设置开发工具包,请参阅 AWS SDK for Java Developer Guide 中的设置适用于 Java 的 AWS 开发工具包

运行示例代码之前,请配置您的 AWS 凭证。有关更多信息,请参阅 AWS SDK for Java Developer Guide 中的设置用于开发的 AWS 凭证和区域

SQSSimpleJavaClientExample.java

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

/* * Copyright 2010-2019 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.List; import java.util.Map.Entry; /** * This sample demonstrates how to make basic requests to Amazon SQS using the * AWS SDK for Java. * <p> * Prerequisites: You must have a valid Amazon Web Services developer account, * and be signed up to use Amazon SQS. For more information about Amazon SQS, * see http://www.amazonaws.cn/sqs * <p> * Make sure that your credentials are located in ~/.aws/credentials */ public class SQSSimpleJavaClientExample { 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 标准 Queues"); System.out.println("===============================================\n"); try { // Create a queue. System.out.println("Creating a new SQS queue called MyQueue.\n"); final CreateQueueRequest createQueueRequest = new CreateQueueRequest("MyQueue"); 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 MyQueue.\n"); sqs.sendMessage(new SendMessageRequest(myQueueUrl, "This is my message text.")); // Receive messages. System.out.println("Receiving messages from MyQueue.\n"); final ReceiveMessageRequest receiveMessageRequest = new ReceiveMessageRequest(myQueueUrl); 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 messages. System.out.println("Deleting the messages that we received.\n"); for (final Message message : messages) { sqs.deleteMessage(new DeleteMessageRequest(myQueueUrl, message.getReceiptHandle())); } // Delete the queue. System.out.println("Deleting the test 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()); } } }