Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

This AWS SNS client library allows to publish messages to SNS that exceed the 256 KB message size limit.

License

NotificationsYou must be signed in to change notification settings

awslabs/amazon-sns-java-extended-client-lib

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

31 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

TheAmazon SNS Extended Client Library for Java enables you to publish messages that are greater than the current SNS limit of 256 KB, up to a maximum of 2 GB.It saves the actual payload in S3 and publishes the reference of the stored S3 object to the topic. Subscribed SQS queues can useAmazon SQS Extended Client Library to dereference and retrieve the payload from S3. Other end-points, such as Lambda, can usePayload Offloading Java Common Library for AWS to dereference and retrieve the payload.

Getting Started

Version 2.x (AWS Java SDKv2)

  <dependency>    <groupId>software.amazon.sns</groupId>    <artifactId>sns-extended-client</artifactId>    <version>2.1.0</version>    <type>jar</type>  </dependency>

Version 1.x (AWS Java SDKv1)

  <dependency>    <groupId>software.amazon.sns</groupId>    <artifactId>sns-extended-client</artifactId>    <version>1.1.2</version>    <type>jar</type>  </dependency>

S3 Message Storage Configuration

The library relies on the Payload Offloading Java Common Library for AWS for message storage and retrieval. The following S3 message storage configuration options are available:

  • Custom message size threshhold: messages with their payload and attribute size exceeding this limit will automatically be stored in S3. It is also possible to force all messages to be stored in S3.
  • Custom KMS key configuration for server-side encryption
  • Bucket name for storing message payloads

Publishing messages to SNS topics and receiving them from SQS subscribers

Below is the code sample that creates a sample topic and queue, subscribes the queue to receive messages from the topic and publishes a test message. The message payload is stored in S3 and the reference to it is published. The SQS Extended Client is used to receive the message.

importcom.amazon.sqs.javamessaging.AmazonSQSExtendedClient;importcom.amazon.sqs.javamessaging.ExtendedClientConfiguration;importsoftware.amazon.awssdk.regions.Region;importsoftware.amazon.awssdk.services.s3.S3Client;importsoftware.amazon.awssdk.services.s3.model.CreateBucketRequest;importsoftware.amazon.awssdk.services.sns.SnsClient;importsoftware.amazon.awssdk.services.sns.model.CreateTopicRequest;importsoftware.amazon.awssdk.services.sns.model.PublishRequest;importsoftware.amazon.awssdk.services.sns.model.SetSubscriptionAttributesRequest;importsoftware.amazon.awssdk.services.sns.model.SubscribeRequest;importsoftware.amazon.awssdk.services.sqs.SqsClient;importsoftware.amazon.awssdk.services.sqs.model.CreateQueueRequest;importsoftware.amazon.awssdk.services.sqs.model.ReceiveMessageRequest;importsoftware.amazon.awssdk.services.sqs.model.ReceiveMessageResponse;importsoftware.amazon.sns.AmazonSNSExtendedClient;importsoftware.amazon.sns.SNSExtendedClientConfiguration;publicclassExample {publicstaticvoidmain(String[]args) {finalStringBUCKET_NAME ="extended-client-bucket";finalStringTOPIC_NAME ="extended-client-topic";finalStringQUEUE_NAME ="extended-client-queue";finalRegionregion =Region.US_WEST_2;//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).finalintEXTENDED_STORAGE_MESSAGE_SIZE_THRESHOLD =32;//Initialize SNS, SQS and S3 clientsfinalSnsClientsnsClient =SnsClient.builder().region(region).build();finalSqsClientsqsClient =SqsClient.builder().region(region).build();finalS3Clients3Client =S3Client.builder().region(region).build();//Create bucket, topic, queue and subscriptions3Client.createBucket(CreateBucketRequest.builder().bucket(BUCKET_NAME).build());finalStringtopicArn =snsClient.createTopic(CreateTopicRequest.builder().name(TOPIC_NAME).build()        ).topicArn();finalStringqueueUrl =sqsClient.createQueue(CreateQueueRequest.builder().queueName(QUEUE_NAME).build()        ).queueUrl();finalStringsubscriptionArn =snsClient.subscribe(SubscribeRequest.builder().topicArn(topicArn).endpoint(queueUrl).build()        ).subscriptionArn();//To read message content stored in S3 transparently through SQS extended client,//set the RawMessageDelivery subscription attribute to TRUEfinalSetSubscriptionAttributesRequestsubscriptionAttributesRequest =SetSubscriptionAttributesRequest.builder()            .subscriptionArn(subscriptionArn)            .attributeName("RawMessageDelivery")            .attributeValue("TRUE")            .build();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 flagfinalSNSExtendedClientConfigurationsnsExtendedClientConfiguration =newSNSExtendedClientConfiguration()            .withPayloadSupportEnabled(s3Client,BUCKET_NAME)            .withPayloadSizeThreshold(EXTENDED_STORAGE_MESSAGE_SIZE_THRESHOLD);finalAmazonSNSExtendedClientsnsExtendedClient =newAmazonSNSExtendedClient(snsClient,snsExtendedClientConfiguration);//Publish message via SNS with storage in S3finalStringmessage ="This message is stored in S3 as it exceeds the threshold of 32 bytes set above.";snsExtendedClient.publish(PublishRequest.builder().topicArn(topicArn).message(message).build());//Initialize SQS extended clientfinalExtendedClientConfigurationsqsExtendedClientConfiguration =newExtendedClientConfiguration()            .withPayloadSupportEnabled(s3Client,BUCKET_NAME);finalAmazonSQSExtendedClientsqsExtendedClient =newAmazonSQSExtendedClient(sqsClient,sqsExtendedClientConfiguration);//Read the message from the queuefinalReceiveMessageResponseresponse =sqsExtendedClient.receiveMessage(ReceiveMessageRequest.builder().queueUrl(queueUrl).build());System.out.println("Received message is " +response.messages().get(0).body());    }}

Output:

Aug 12, 2020 12:42:31 PM software.amazon.payloadoffloading.PayloadStorageConfiguration setPayloadSupportEnabledINFO: Payload support enabled.Aug 12, 2020 12:42:32 PM software.amazon.payloadoffloading.S3BackedPayloadStore storeOriginalPayloadINFO: S3 object created, Bucket name: extended-client-bucket, Object key: 09900296-35fc-4927-91f6-768ecf8dafa4.Aug 12, 2020 12:42:32 PM software.amazon.payloadoffloading.PayloadStorageConfiguration setPayloadSupportEnabledINFO: Payload support enabled.Received message is This message is stored in S3. This message is stored in S3.

Releases

You can download release builds through thereleases section of this project.

Contribution

SeeCONTRIBUTING for more information.

License

This project is licensed under the Apache-2.0 License.

About

This AWS SNS client library allows to publish messages to SNS that exceed the 256 KB message size limit.

Topics

Resources

License

Code of conduct

Security policy

Stars

Watchers

Forks

Packages

No packages published

Contributors11

Languages


[8]ページ先頭

©2009-2025 Movatter.jp