기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.
Java용 확장 클라이언트 라이브러리
필수 조건
다음은 Java용 Amazon SNS 확장 클라이언트 라이브러리
-
AWS SDK.
이 페이지의 예시에서는 AWS Java SDK를 사용합니다. SDK를 설치 및 설정하려면 AWS SDK for Java 개발자 안내서의 Java용 AWS SDK 설정을 참조하세요.
-
그리고 AWS 계정 적절한 자격 증명이 필요합니다.
계정을 AWS 계정생성하려면 AWS 홈 페이지로
이동한 다음 AWS 계정 생성을 선택합니다. 지침을 따릅니다. 자격 증명에 대한 자세한 내용은 AWS SDK for Java 개발자 안내서의 AWS 자격 증명 및 개발 지역 설정을 참조하십시오.
-
Java 8 이상.
-
Java용 Amazon SNS 확장 클라이언트 라이브러리(Maven
에서도 사용 가능).
메시지 스토리지 구성
Amazon SNS 확장 클라이언트 라이브러리는 메시지 저장 및 검색을 AWS 위해 페이로드 오프로딩 Java 공용 라이브러리를 사용합니다. 다음 Amazon S3 메시지 스토리지 옵션
-
사용자 지정 메시지 크기 임계값 – 페이로드 및 속성이 이 크기를 초과하는 메시지는 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에 메시지 게시
다음 코드 예제에서는 다음과 같은 작업을 수행하는 방법을 보여줍니다.
-
샘플 주제 및 대기열을 만듭니다.
-
대기열에서 구독하여 주제의 메시지를 수신합니다.
-
테스트 메시지를 게시합니다.
메시지 페이로드는 Amazon S3에 저장되고 이에 대한 참조가 게시됩니다. Amazon SQS 확장 클라이언트는 메시지를 수신하는 데 사용됩니다.
- Java 1.x용 SDK
-
참고
자세한 내용은 다음과 같습니다. GitHub AWS 코드 예제 리포지토리
에서 전체 예제를 찾고 설정 및 실행하는 방법을 배워보세요. 대용량 메시지를 게시하려면 Java용 Amazon SNS 확장 클라이언트 라이브러리를 사용하세요. 보내는 메시지는 실제 메시지 내용이 포함된 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()); } }
기타 엔드포인트 프로토콜
Amazon SNS 및 Amazon SQS 라이브러리는 모두 AWS용 페이로드 오프로딩 Java 공통 라이브러리
페이로드 오프로딩 Java 공용 라이브러리를 사용할 수 없는 엔드포인트는 Amazon S3에 저장된 페이로드와 함께 메시지를 AWS 게시할 수 있습니다. 다음은 위의 코드 예제에서 게시한 Amazon S3 참조의 예입니다.
[ "software.amazon.payloadoffloading.PayloadS3Pointer", { "s3BucketName": "extended-client-bucket", "s3Key": "xxxx-xxxxx-xxxxx-xxxxxx" } ]