

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

# Java 用の Amazon SNS 拡張クライアントライブラリ
<a name="extended-client-library-java"></a>

## 前提条件
<a name="prereqs-sns-extended-client-library"></a>

[Java 用 Amazon SNS 拡張クライアントライブラリ](https://github.com/awslabs/amazon-sns-java-extended-client-lib)を使用するための前提条件は以下のとおりです。
+  AWS SDK。このページの例では、Java SDK AWS を使用しています。SDK をインストールしてセットアップするには、「 *AWS SDK for Java デベロッパーガイド*[」の AWS 「 SDK for Java のセットアップ](https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/setup-install.html)」を参照してください。
+ 適切な認証情報 AWS アカウント を持つ 。を作成するには AWS アカウント、[AWS ホームページ](https://aws.amazon.com/)に移動し、** AWS アカウントの作成**を選択します。手順に従います。

  認証情報の詳細については、「 *AWS SDK for Java デベロッパーガイド*」の[「開発用の AWS 認証情報とリージョンの設定](https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/setup-credentials.html)」を参照してください。
+ Java 8 以上 
+ Java 用 Amazon SNS 拡張クライアントライブラリ ([Maven](https://maven.apache.org/) からも利用可能) 

## メッセージストレージの設定
<a name="large-message-configure-storage"></a>

Amazon SNS 拡張クライアントライブラリは、メッセージの保存と取得 AWS に のペイロードオフロード Java 共通ライブラリを使用します。以下の Amazon S3 [メッセージストレージオプション](https://github.com/awslabs/amazon-sns-java-extended-client-lib/blob/main/src/main/java/software/amazon/sns/SNSExtendedClientConfiguration.java)を設定できます。
+ **カスタムメッセージサイズのしきい値** – このサイズを超えるペイロードと属性を持つメッセージは、自動的に Amazon S3 に保存されます。
+ **`alwaysThroughS3` フラグ** – この値を `true` に設定すると、すべてのメッセージペイロードが強制的に Amazon S3 に保存されます。例:

  ```
  SNSExtendedClientConfiguration snsExtendedClientConfiguration = new
  SNSExtendedClientConfiguration() .withPayloadSupportEnabled(s3Client, BUCKET_NAME).withAlwaysThroughS3(true);
  ```
+ **カスタム KMS キー** – Amazon S3 バケットのサーバー側の暗号化に使用するキー。
+ **バケット名** – メッセージペイロードを保存するための Amazon S3 バケットの名前。

## 例: Amazon S3 に保存されたペイロードを使用して Amazon SNS にメッセージを発行する
<a name="example-s3-large-payloads"></a>

次のコード例は、以下の操作方法を示しています。
+ サンプルのトピックとキューを作成します。
+ トピックからメッセージを受信するためにキューをサブスクライブします。
+ テストメッセージを発行します。

メッセージペイロードは Amazon S3 に保存され、そのペイロードへのリファレンスが発行されます。メッセージの受信には、Amazon SQS 拡張クライアントが使用されます。

**SDK for Java 1.x**  
 GitHub には、その他のリソースもあります。用例一覧を検索し、[AWS コード例リポジトリ](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/java/example_code/sns#code-examples)での設定と実行の方法を確認してください。
容量の大きなメッセージを発行するには、Amazon SNS Extended Client Library for Java を使用します。送信するメッセージは、実際のメッセージコンテンツを含む Amazon S3 オブジェクトをリファレンスします。  

```
import com.amazon.sqs.javamessaging.AmazonSQSExtendedClient;
import com.amazon.sqs.javamessaging.ExtendedClientConfiguration;
import com.amazonaws.regions.Region;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.services.sns.AmazonSNS;
import com.amazonaws.services.sns.AmazonSNSClientBuilder;
import com.amazonaws.services.sns.model.CreateTopicRequest;
import com.amazonaws.services.sns.model.PublishRequest;
import com.amazonaws.services.sns.model.SetSubscriptionAttributesRequest;
import com.amazonaws.services.sns.util.Topics;
import com.amazonaws.services.sqs.AmazonSQS;
import com.amazonaws.services.sqs.AmazonSQSClientBuilder;
import com.amazonaws.services.sqs.model.CreateQueueRequest;
import com.amazonaws.services.sqs.model.ReceiveMessageResult;
import software.amazon.sns.AmazonSNSExtendedClient;
import software.amazon.sns.SNSExtendedClientConfiguration;

public class Example {

        public static void main(String[] args) {
                final String BUCKET_NAME = "extended-client-bucket";
                final String TOPIC_NAME = "extended-client-topic";
                final String QUEUE_NAME = "extended-client-queue";
                final Regions region = Regions.DEFAULT_REGION;

                // Message threshold controls the maximum message size that will be allowed to
                // be published
                // through SNS using the extended client. Payload of messages exceeding this
                // value will be stored in
                // S3. The default value of this parameter is 256 KB which is the maximum
                // message size in SNS (and SQS).
                final int EXTENDED_STORAGE_MESSAGE_SIZE_THRESHOLD = 32;

                // Initialize SNS, SQS and S3 clients
                final AmazonSNS snsClient = AmazonSNSClientBuilder.standard().withRegion(region).build();
                final AmazonSQS sqsClient = AmazonSQSClientBuilder.standard().withRegion(region).build();
                final AmazonS3 s3Client = AmazonS3ClientBuilder.standard().withRegion(region).build();

                // Create bucket, topic, queue and subscription
                s3Client.createBucket(BUCKET_NAME);
                final String topicArn = snsClient.createTopic(
                                new CreateTopicRequest().withName(TOPIC_NAME)).getTopicArn();
                final String queueUrl = sqsClient.createQueue(
                                new CreateQueueRequest().withQueueName(QUEUE_NAME)).getQueueUrl();
                final String subscriptionArn = Topics.subscribeQueue(
                                snsClient, sqsClient, topicArn, queueUrl);

                // To read message content stored in S3 transparently through SQS extended
                // client,
                // set the RawMessageDelivery subscription attribute to TRUE
                final SetSubscriptionAttributesRequest subscriptionAttributesRequest = new SetSubscriptionAttributesRequest();
                subscriptionAttributesRequest.setSubscriptionArn(subscriptionArn);
                subscriptionAttributesRequest.setAttributeName("RawMessageDelivery");
                subscriptionAttributesRequest.setAttributeValue("TRUE");
                snsClient.setSubscriptionAttributes(subscriptionAttributesRequest);

                // Initialize SNS extended client
                // PayloadSizeThreshold triggers message content storage in S3 when the
                // threshold is exceeded
                // To store all messages content in S3, use AlwaysThroughS3 flag
                final SNSExtendedClientConfiguration snsExtendedClientConfiguration = new SNSExtendedClientConfiguration()
                                .withPayloadSupportEnabled(s3Client, BUCKET_NAME)
                                .withPayloadSizeThreshold(EXTENDED_STORAGE_MESSAGE_SIZE_THRESHOLD);
                final AmazonSNSExtendedClient snsExtendedClient = new AmazonSNSExtendedClient(snsClient,
                                snsExtendedClientConfiguration);

                // Publish message via SNS with storage in S3
                final String message = "This message is stored in S3 as it exceeds the threshold of 32 bytes set above.";
                snsExtendedClient.publish(topicArn, message);

                // Initialize SQS extended client
                final ExtendedClientConfiguration sqsExtendedClientConfiguration = new ExtendedClientConfiguration()
                                .withPayloadSupportEnabled(s3Client, BUCKET_NAME);
                final AmazonSQSExtendedClient sqsExtendedClient = new AmazonSQSExtendedClient(sqsClient,
                                sqsExtendedClientConfiguration);

                // Read the message from the queue
                final ReceiveMessageResult result = sqsExtendedClient.receiveMessage(queueUrl);
                System.out.println("Received message is " + result.getMessages().get(0).getBody());
        }
}
```

## その他のエンドポイントプロトコル
<a name="large-payloads-other-protocols"></a>

Amazon SNS と Amazon SQS ライブラリの両方で、[Payload Offloading Java Common Library for AWS](https://github.com/awslabs/payload-offloading-java-common-lib-for-aws) を使用して、Amazon S3 でメッセージペイロードを保存および取得できます。Java が有効なエンドポイント (Java で実装されている HTTPS エンドポイントなど) は、同じライブラリを使用してメッセージコンテンツを逆リファレンスできます。

のペイロードオフロード Java 共通ライブラリを使用できないエンドポイントでも、Amazon S3 に保存されているペイロードを使用してメッセージを発行 AWS できます。以下は、上記のコード例で発行された Amazon S3 リファレンスの例です。

```
[
  "software.amazon.payloadoffloading.PayloadS3Pointer",
  {
    "s3BucketName": "extended-client-bucket",
    "s3Key": "xxxx-xxxxx-xxxxx-xxxxxx"
  }
]
```