Search in sources :

Example 6 with AmazonSNS

use of com.amazonaws.services.sns.AmazonSNS in project aws-doc-sdk-examples by awsdocs.

the class Example method main.

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());
}
Also used : AmazonS3(com.amazonaws.services.s3.AmazonS3) Regions(com.amazonaws.regions.Regions) AmazonSQS(com.amazonaws.services.sqs.AmazonSQS) CreateTopicRequest(com.amazonaws.services.sns.model.CreateTopicRequest) AmazonSQSExtendedClient(com.amazon.sqs.javamessaging.AmazonSQSExtendedClient) AmazonSNS(com.amazonaws.services.sns.AmazonSNS) AmazonSNSExtendedClient(software.amazon.sns.AmazonSNSExtendedClient) CreateQueueRequest(com.amazonaws.services.sqs.model.CreateQueueRequest) SetSubscriptionAttributesRequest(com.amazonaws.services.sns.model.SetSubscriptionAttributesRequest) SNSExtendedClientConfiguration(software.amazon.sns.SNSExtendedClientConfiguration) ExtendedClientConfiguration(com.amazon.sqs.javamessaging.ExtendedClientConfiguration) SNSExtendedClientConfiguration(software.amazon.sns.SNSExtendedClientConfiguration) ReceiveMessageResult(com.amazonaws.services.sqs.model.ReceiveMessageResult)

Example 7 with AmazonSNS

use of com.amazonaws.services.sns.AmazonSNS in project beam by apache.

the class SnsIOTest method testRetries.

@Test
public void testRetries() throws Throwable {
    thrown.expect(IOException.class);
    thrown.expectMessage("Error writing to SNS");
    thrown.expectMessage("No more attempts allowed");
    final PublishRequest request1 = createSampleMessage("my message that will not be published");
    final TupleTag<PublishResult> results = new TupleTag<>();
    final AmazonSNS amazonSnsErrors = getAmazonSnsMockErrors();
    p.apply(Create.of(request1)).apply(SnsIO.write().withTopicName(topicName).withRetryConfiguration(SnsIO.RetryConfiguration.create(4, standardSeconds(10), millis(1))).withAWSClientsProvider(new Provider(amazonSnsErrors)).withResultOutputTag(results));
    try {
        p.run();
    } catch (final Pipeline.PipelineExecutionException e) {
        // check 3 retries were initiated by inspecting the log before passing on the exception
        snsWriterFnLogs.verifyWarn(MessageFormatter.format(SnsIO.Write.SnsWriterFn.RETRY_ATTEMPT_LOG, 1).getMessage());
        snsWriterFnLogs.verifyWarn(MessageFormatter.format(SnsIO.Write.SnsWriterFn.RETRY_ATTEMPT_LOG, 2).getMessage());
        snsWriterFnLogs.verifyWarn(MessageFormatter.format(SnsIO.Write.SnsWriterFn.RETRY_ATTEMPT_LOG, 3).getMessage());
        throw e.getCause();
    }
}
Also used : PublishResult(com.amazonaws.services.sns.model.PublishResult) TupleTag(org.apache.beam.sdk.values.TupleTag) PublishRequest(com.amazonaws.services.sns.model.PublishRequest) AmazonSNS(com.amazonaws.services.sns.AmazonSNS) TestPipeline(org.apache.beam.sdk.testing.TestPipeline) Pipeline(org.apache.beam.sdk.Pipeline) Test(org.junit.Test)

Example 8 with AmazonSNS

use of com.amazonaws.services.sns.AmazonSNS in project beam by apache.

the class SnsIOTest method testCustomCoder.

@Test
public void testCustomCoder() throws Exception {
    final PublishRequest request1 = createSampleMessage("my_first_message");
    final TupleTag<PublishResult> results = new TupleTag<>();
    final AmazonSNS amazonSnsSuccess = getAmazonSnsMockSuccess();
    final MockCoder mockCoder = new MockCoder();
    final PCollectionTuple snsWrites = p.apply(Create.of(request1)).apply(SnsIO.write().withTopicName(topicName).withAWSClientsProvider(new Provider(amazonSnsSuccess)).withResultOutputTag(results).withCoder(mockCoder));
    final PCollection<Long> publishedResultsSize = snsWrites.get(results).apply(MapElements.into(TypeDescriptors.strings()).via(result -> result.getMessageId())).apply(Count.globally());
    PAssert.that(publishedResultsSize).containsInAnyOrder(ImmutableList.of(1L));
    p.run().waitUntilFinish();
    assertThat(mockCoder.captured).isNotNull();
}
Also used : PublishResult(com.amazonaws.services.sns.model.PublishResult) TupleTag(org.apache.beam.sdk.values.TupleTag) PCollectionTuple(org.apache.beam.sdk.values.PCollectionTuple) PublishRequest(com.amazonaws.services.sns.model.PublishRequest) AmazonSNS(com.amazonaws.services.sns.AmazonSNS) Test(org.junit.Test)

Example 9 with AmazonSNS

use of com.amazonaws.services.sns.AmazonSNS in project beam by apache.

the class SnsIOTest method getAmazonSnsMockSuccess.

private static AmazonSNS getAmazonSnsMockSuccess() {
    final AmazonSNS amazonSNS = Mockito.mock(AmazonSNS.class);
    configureAmazonSnsMock(amazonSNS);
    final PublishResult result = Mockito.mock(PublishResult.class);
    final SdkHttpMetadata metadata = Mockito.mock(SdkHttpMetadata.class);
    Mockito.when(metadata.getHttpHeaders()).thenReturn(new HashMap<>());
    Mockito.when(metadata.getHttpStatusCode()).thenReturn(200);
    Mockito.when(result.getSdkHttpMetadata()).thenReturn(metadata);
    Mockito.when(result.getMessageId()).thenReturn(UUID.randomUUID().toString());
    Mockito.when(amazonSNS.publish(Mockito.any())).thenReturn(result);
    return amazonSNS;
}
Also used : SdkHttpMetadata(com.amazonaws.http.SdkHttpMetadata) PublishResult(com.amazonaws.services.sns.model.PublishResult) AmazonSNS(com.amazonaws.services.sns.AmazonSNS)

Example 10 with AmazonSNS

use of com.amazonaws.services.sns.AmazonSNS in project beam by apache.

the class SnsIOTest method testDataWritesToSNS.

@Test
public void testDataWritesToSNS() {
    final PublishRequest request1 = createSampleMessage("my_first_message");
    final PublishRequest request2 = createSampleMessage("my_second_message");
    final TupleTag<PublishResult> results = new TupleTag<>();
    final AmazonSNS amazonSnsSuccess = getAmazonSnsMockSuccess();
    final PCollectionTuple snsWrites = p.apply(Create.of(request1, request2)).apply(SnsIO.write().withTopicName(topicName).withRetryConfiguration(SnsIO.RetryConfiguration.create(5, org.joda.time.Duration.standardMinutes(1))).withAWSClientsProvider(new Provider(amazonSnsSuccess)).withResultOutputTag(results));
    final PCollection<Long> publishedResultsSize = snsWrites.get(results).apply(Count.globally());
    PAssert.that(publishedResultsSize).containsInAnyOrder(ImmutableList.of(2L));
    p.run().waitUntilFinish();
}
Also used : PublishResult(com.amazonaws.services.sns.model.PublishResult) TupleTag(org.apache.beam.sdk.values.TupleTag) PCollectionTuple(org.apache.beam.sdk.values.PCollectionTuple) PublishRequest(com.amazonaws.services.sns.model.PublishRequest) AmazonSNS(com.amazonaws.services.sns.AmazonSNS) Test(org.junit.Test)

Aggregations

AmazonSNS (com.amazonaws.services.sns.AmazonSNS)13 BasicAWSCredentials (com.amazonaws.auth.BasicAWSCredentials)4 PublishRequest (com.amazonaws.services.sns.model.PublishRequest)4 PublishResult (com.amazonaws.services.sns.model.PublishResult)4 Test (org.junit.Test)4 AWSStaticCredentialsProvider (com.amazonaws.auth.AWSStaticCredentialsProvider)3 TupleTag (org.apache.beam.sdk.values.TupleTag)3 AWSCredentials (com.amazonaws.auth.AWSCredentials)2 PCollectionTuple (org.apache.beam.sdk.values.PCollectionTuple)2 AmazonSQSExtendedClient (com.amazon.sqs.javamessaging.AmazonSQSExtendedClient)1 ExtendedClientConfiguration (com.amazon.sqs.javamessaging.ExtendedClientConfiguration)1 ClientConfiguration (com.amazonaws.ClientConfiguration)1 SdkHttpMetadata (com.amazonaws.http.SdkHttpMetadata)1 Regions (com.amazonaws.regions.Regions)1 AmazonS3 (com.amazonaws.services.s3.AmazonS3)1 AmazonSNSClient (com.amazonaws.services.sns.AmazonSNSClient)1 CreateTopicRequest (com.amazonaws.services.sns.model.CreateTopicRequest)1 CreateTopicResult (com.amazonaws.services.sns.model.CreateTopicResult)1 InternalErrorException (com.amazonaws.services.sns.model.InternalErrorException)1 SetSubscriptionAttributesRequest (com.amazonaws.services.sns.model.SetSubscriptionAttributesRequest)1