Amazon S3を使用した大量のAmazon SQSメッセージの管理 - Amazon Simple Queue Service

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

Amazon S3を使用した大量のAmazon SQSメッセージの管理

大量のAmazon Simple Queue Service (Amazon SQS)メッセージを管理するには、Amazon Simple Storage Service (Amazon S3)と、Java 用 Amazon SQS拡張クライアントライブラリを使用できます。これは特に、2GB までのサイズのメッセージを保存および処理するのに役立ちます。アプリケーションで、キューを連続して作成して非アクティブな状態のままにするか、大量のデータをキューに保存する必要がない限り、データの保存にAmazon S3を使用することを検討してください。

Amazon SQS Java用拡張クライアントライブラリを使用して、次のことを実行できます。

  • メッセージを常にAmazon S3に保存するか、メッセージのサイズが256KB を超える場合のみ保存するかを指定します。

  • S3 バケットに保存されている単一のメッセージオブジェクトを参照するメッセージを送信します。

  • S3バケットからメッセージオブジェクトを取得する

  • S3バケットからメッセージオブジェクトを削除する。

Java用のAmazon SQS 拡張クライアントライブラリを使用して、のみとAWS SDK for JavaのAmazon S3 を使用するAmazon SQSメッセージを管理するために使用することができます。AWS CLI、Amazon SQSコンソール、Amazon SQS HTTP API、またはその他 AWSSDKを使用してこれを実行することはできません。

SDK for JavaおよびAmazon SQS拡張クライアントライブラリfor Javaには、J2SE Devopment Kit 8.0 以降が必要です。

前提条件

次の例では、AWS Java SDKを使用しています。SDK をインストールしてセットアップするには、「AWS SDK for Java デベロッパーガイド」の「AWS SDK for Java のセットアップ」を参照してください。

サンプルコードを実行する前に、AWSの認証情報を設定します。詳細については、AWS SDK for Java デベロッパーガイドの「ディベロップメントAWS 認証情報とリージョンのセットアップ」を参照してください。

例: Amazon S3を使用して大量の Amazon SQSメッセージを管理する

次のサンプルコードでは、ランダムな名前でAmazon S3バケットを作成し、ライフサイクルルールを追加して、14日後にオブジェクトを完全に削除します。また、名前付きのキューを作成してMyQueueS3 バケットに保存されている256KB を超えるランダムなメッセージをキューに送信します。最後に、コードはメッセージを受信し、そのメッセージに関する情報を返信して、メッセージ、キュー、およびバケットを削除します。

/* * Copyright 2010-2022 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 * * https://aws.amazon.com/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.amazon.sqs.javamessaging.AmazonSQSExtendedClient; import com.amazon.sqs.javamessaging.ExtendedClientConfiguration; import com.amazonaws.services.s3.AmazonS3; import com.amazonaws.services.s3.AmazonS3ClientBuilder; import com.amazonaws.services.s3.model.*; import com.amazonaws.services.sqs.AmazonSQS; import com.amazonaws.services.sqs.AmazonSQSClientBuilder; import com.amazonaws.services.sqs.model.*; import org.joda.time.DateTime; import org.joda.time.format.DateTimeFormat; import java.util.Arrays; import java.util.List; import java.util.UUID; public class SQSExtendedClientExample { // Create an Amazon S3 bucket with a random name. private final static String S3_BUCKET_NAME = UUID.randomUUID() + "-" + DateTimeFormat.forPattern("yyMMdd-hhmmss").print(new DateTime()); 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 AmazonS3 s3 = AmazonS3ClientBuilder.defaultClient(); /* * Set the Amazon S3 bucket name, and then set a lifecycle rule on the * bucket to permanently delete objects 14 days after each object's * creation date. */ final BucketLifecycleConfiguration.Rule expirationRule = new BucketLifecycleConfiguration.Rule(); expirationRule.withExpirationInDays(14).withStatus("Enabled"); final BucketLifecycleConfiguration lifecycleConfig = new BucketLifecycleConfiguration().withRules(expirationRule); // Create the bucket and allow message objects to be stored in the bucket. s3.createBucket(S3_BUCKET_NAME); s3.setBucketLifecycleConfiguration(S3_BUCKET_NAME, lifecycleConfig); System.out.println("Bucket created and configured."); /* * Set the Amazon SQS extended client configuration with large payload * support enabled. */ final ExtendedClientConfiguration extendedClientConfig = new ExtendedClientConfiguration() .withLargePayloadSupportEnabled(s3, S3_BUCKET_NAME); final AmazonSQS sqsExtended = new AmazonSQSExtendedClient(AmazonSQSClientBuilder .defaultClient(), extendedClientConfig); /* * Create a long string of characters for the message object which will * be stored in the bucket. */ int stringLength = 300000; char[] chars = new char[stringLength]; Arrays.fill(chars, 'x'); final String myLongString = new String(chars); // Create a message queue for this example. final String QueueName = "MyQueue" + UUID.randomUUID().toString(); final CreateQueueRequest createQueueRequest = new CreateQueueRequest(QueueName); final String myQueueUrl = sqsExtended .createQueue(createQueueRequest).getQueueUrl(); System.out.println("Queue created."); // Send the message. final SendMessageRequest myMessageRequest = new SendMessageRequest(myQueueUrl, myLongString); sqsExtended.sendMessage(myMessageRequest); System.out.println("Sent the message."); // Receive the message. final ReceiveMessageRequest receiveMessageRequest = new ReceiveMessageRequest(myQueueUrl); List<Message> messages = sqsExtended .receiveMessage(receiveMessageRequest).getMessages(); // Print information about the message. for (Message message : messages) { System.out.println("\nMessage received."); System.out.println(" ID: " + message.getMessageId()); System.out.println(" Receipt handle: " + message.getReceiptHandle()); System.out.println(" Message body (first 5 characters): " + message.getBody().substring(0, 5)); } // Delete the message, the queue, and the bucket. final String messageReceiptHandle = messages.get(0).getReceiptHandle(); sqsExtended.deleteMessage(new DeleteMessageRequest(myQueueUrl, messageReceiptHandle)); System.out.println("Deleted the message."); sqsExtended.deleteQueue(new DeleteQueueRequest(myQueueUrl)); System.out.println("Deleted the queue."); deleteBucketAndAllContents(s3); System.out.println("Deleted the bucket."); } private static void deleteBucketAndAllContents(AmazonS3 client) { ObjectListing objectListing = client.listObjects(S3_BUCKET_NAME); while (true) { for (S3ObjectSummary objectSummary : objectListing .getObjectSummaries()) { client.deleteObject(S3_BUCKET_NAME, objectSummary.getKey()); } if (objectListing.isTruncated()) { objectListing = client.listNextBatchOfObjects(objectListing); } else { break; } } final VersionListing list = client.listVersions( new ListVersionsRequest().withBucketName(S3_BUCKET_NAME)); for (S3VersionSummary s : list.getVersionSummaries()) { client.deleteVersion(S3_BUCKET_NAME, s.getKey(), s.getVersionId()); } client.deleteBucket(S3_BUCKET_NAME); } }