software.amazon.awssdk.services.sns.model.PublishRequest Java Examples

The following examples show how to use software.amazon.awssdk.services.sns.model.PublishRequest. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PublishTopic.java    From aws-doc-sdk-examples with Apache License 2.0 6 votes vote down vote up
public static void pubTopic(SnsClient snsClient, String message, String topicArn) {

        try {
            PublishRequest request = PublishRequest.builder()
                .message(message)
                .topicArn(topicArn)
                .build();

            PublishResponse result = snsClient.publish(request);
            System.out.println(result.messageId() + " Message sent. Status was " + result.sdkHttpResponse().statusCode());

         } catch (SnsException e) {
            System.err.println(e.awsErrorDetails().errorMessage());
              System.exit(1);
         }
        //snippet-end:[sns.java2.PublishTopic.main]
    }
 
Example #2
Source File: PublishTextSMS.java    From aws-doc-sdk-examples with Apache License 2.0 6 votes vote down vote up
public static void pubTextSMS(SnsClient snsClient, String message, String phoneNumber) {
    try {
        PublishRequest request = PublishRequest.builder()
            .message(message)
            .phoneNumber(phoneNumber)
            .build();

        PublishResponse result = snsClient.publish(request);

        System.out.println(result.messageId() + " Message sent. Status was " + result.sdkHttpResponse().statusCode());

    } catch (SnsException e) {

    System.err.println(e.awsErrorDetails().errorMessage());
    System.exit(1);
    }

    //snippet-end:[sns.java2.PublishTextSMS.main]
}
 
Example #3
Source File: PublishTextSMS.java    From aws-doc-sdk-examples with Apache License 2.0 6 votes vote down vote up
public void sendMessage(String id) {

        Region region = Region.US_EAST_1;
        SnsClient snsClient = SnsClient.builder()
                .region(region)
                .credentialsProvider(EnvironmentVariableCredentialsProvider.create())
                .build();
        String message = "A new item with ID value "+ id +" was added to the DynamoDB table";
        String phoneNumber="ENTER MOBILE NUMBER"; //Replace with a mobile phone number

        try {
            PublishRequest request = PublishRequest.builder()
                    .message(message)
                    .phoneNumber(phoneNumber)
                    .build();

            PublishResponse result = snsClient.publish(request);

        } catch (SnsException e) {

            System.err.println(e.awsErrorDetails().errorMessage());
            System.exit(1);
        }
    }
 
Example #4
Source File: SnsConnector.java    From smallrye-reactive-messaging with Apache License 2.0 6 votes vote down vote up
/**
 * Send message to the SNS Topic.
 *
 * @param message Message to be sent, must not be {@code null}
 * @return the CompletionStage of sending message.
 */
private CompletionStage<Message<?>> send(Message<?> message, String topic, String snsUrl, boolean mock) {
    SnsClientConfig clientConfig = new SnsClientConfig(snsUrl, mock);
    SnsAsyncClient client = SnsClientManager.get().getAsyncClient(clientConfig);

    CreateTopicRequest topicCreationRequest = CreateTopicRequest.builder().name(topic).build();
    return client.createTopic(topicCreationRequest)
            .thenApply(CreateTopicResponse::topicArn)
            .thenCompose(arn -> client.publish(PublishRequest
                    .builder()
                    .topicArn(arn)
                    .message((String) message.getPayload())
                    .build()))
            .thenApply(resp -> {
                log.successfullySend(resp.messageId());
                return message;
            });
}
 
Example #5
Source File: MockSnsAsyncExceptionClient.java    From beam with Apache License 2.0 5 votes vote down vote up
@Override
public CompletableFuture<PublishResponse> publish(PublishRequest publishRequest) {
  CompletableFuture<PublishResponse> completableFuture = new CompletableFuture<>();
  completableFuture.completeExceptionally(
      new RuntimeException("Error occurred during publish call"));
  return completableFuture;
}
 
Example #6
Source File: SnsIO.java    From beam with Apache License 2.0 5 votes vote down vote up
@ProcessElement
public void processElement(ProcessContext context) throws Exception {
  PublishRequest request =
      (PublishRequest) spec.getPublishRequestFn().apply(context.element());
  Sleeper sleeper = Sleeper.DEFAULT;
  BackOff backoff = retryBackoff.backoff();
  int attempt = 0;
  while (true) {
    attempt++;
    try {
      PublishResponse pr = producer.publish(request);
      context.output(pr);
      break;
    } catch (Exception ex) {
      // Fail right away if there is no retry configuration
      if (spec.getRetryConfiguration() == null
          || !spec.getRetryConfiguration().getRetryPredicate().test(ex)) {
        SNS_WRITE_FAILURES.inc();
        LOG.info("Unable to publish message {} due to {} ", request.message(), ex);
        throw new IOException("Error writing to SNS (no attempt made to retry)", ex);
      }

      if (!BackOffUtils.next(sleeper, backoff)) {
        throw new IOException(
            String.format(
                "Error writing to SNS after %d attempt(s). No more attempts allowed",
                attempt),
            ex);
      } else {
        // Note: this used in test cases to verify behavior
        LOG.warn(String.format(RETRY_ATTEMPT_LOG, attempt), ex);
      }
    }
  }
}
 
Example #7
Source File: SnsClientMockSuccess.java    From beam with Apache License 2.0 5 votes vote down vote up
@Override
public PublishResponse publish(PublishRequest publishRequest) {
  PublishResponse response = Mockito.mock(PublishResponse.class);
  SdkHttpResponse metadata = Mockito.mock(SdkHttpResponse.class);

  Mockito.when(metadata.headers()).thenReturn(new HashMap<>());
  Mockito.when(metadata.statusCode()).thenReturn(200);
  Mockito.when(response.sdkHttpResponse()).thenReturn(metadata);
  Mockito.when(response.messageId()).thenReturn(UUID.randomUUID().toString());

  return response;
}
 
Example #8
Source File: StreamScalingUtils.java    From amazon-kinesis-scaling-utils with Apache License 2.0 4 votes vote down vote up
public static void sendNotification(SnsClient snsClient, String notificationARN, String subject, String message) {
	final PublishRequest req = PublishRequest.builder().topicArn(notificationARN).message(message).subject(subject)
			.build();
	snsClient.publish(req);
}
 
Example #9
Source File: SnsIOTest.java    From beam with Apache License 2.0 4 votes vote down vote up
private static PublishRequest createSampleMessage(String message) {
  return PublishRequest.builder().topicArn(topicArn).message(message).build();
}
 
Example #10
Source File: SnsIOWriteTest.java    From beam with Apache License 2.0 4 votes vote down vote up
private SerializableFunction<String, PublishRequest> createPublishRequestFn() {
  return (input) -> PublishRequest.builder().topicArn(TOPIC).message(input).build();
}
 
Example #11
Source File: SnsClientMockErrors.java    From beam with Apache License 2.0 4 votes vote down vote up
@Override
public PublishResponse publish(PublishRequest publishRequest) {
  throw InternalErrorException.builder().message("Service unavailable").build();
}
 
Example #12
Source File: SnsIO.java    From beam with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("FutureReturnValueIgnored")
@ProcessElement
public void processElement(ProcessContext context) {
  PublishRequest publishRequest = spec.getPublishRequestFn().apply(context.element());
  client.publish(publishRequest).whenComplete(getPublishResponse(context));
}
 
Example #13
Source File: SnsIO.java    From beam with Apache License 2.0 4 votes vote down vote up
/**
 * Specify a function for converting a message into PublishRequest object.
 *
 * @param publishRequestFn publishRequestFn
 */
public WriteAsync<T> withPublishRequestFn(
    SerializableFunction<T, PublishRequest> publishRequestFn) {
  checkNotNull(publishRequestFn, "publishRequestFn cannot be null");
  return builder().setPublishRequestFn(publishRequestFn).build();
}
 
Example #14
Source File: SnsIO.java    From beam with Apache License 2.0 4 votes vote down vote up
abstract Builder<T> setPublishRequestFn(
SerializableFunction<T, PublishRequest> publishRequestFn);
 
Example #15
Source File: SnsIO.java    From beam with Apache License 2.0 4 votes vote down vote up
/** SerializableFunction to create PublishRequest. */
@Nullable
abstract SerializableFunction<T, PublishRequest> getPublishRequestFn();
 
Example #16
Source File: SnsIO.java    From beam with Apache License 2.0 4 votes vote down vote up
abstract Builder<T> setPublishRequestFn(
SerializableFunction<T, PublishRequest> publishRequestFn);
 
Example #17
Source File: SnsIO.java    From beam with Apache License 2.0 4 votes vote down vote up
@Nullable
abstract SerializableFunction<T, PublishRequest> getPublishRequestFn();
 
Example #18
Source File: SnsIO.java    From beam with Apache License 2.0 2 votes vote down vote up
/**
 * Specify a function for converting a message into PublishRequest object, this function is
 * mandatory.
 *
 * @param publishRequestFn publishRequestFn
 */
public Write<T> withPublishRequestFn(SerializableFunction<T, PublishRequest> publishRequestFn) {
  return builder().setPublishRequestFn(publishRequestFn).build();
}