com.amazonaws.services.sns.model.SubscribeRequest Java Examples

The following examples show how to use com.amazonaws.services.sns.model.SubscribeRequest. 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: SnsTopicResourceTest.java    From Cheddar with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldSubscribe_withSqsQueueResource() {
    // Given
    final SqsQueueResource mockSqsQueueResource = mock(SqsQueueResource.class);
    final String queueArn = randomString();
    when(mockSqsQueueResource.queueArn()).thenReturn(queueArn);

    // When
    snsTopicResource.subscribe(mockSqsQueueResource);

    // Then
    final ArgumentCaptor<SubscribeRequest> captor = ArgumentCaptor.forClass(SubscribeRequest.class);
    verify(mockAmazonSnsClient).subscribe(captor.capture());
    final SubscribeRequest subscribeRequest = captor.getValue();
    assertEquals(topicArn, subscribeRequest.getTopicArn());
    assertEquals("sqs", subscribeRequest.getProtocol());
    assertEquals(queueArn, subscribeRequest.getEndpoint());
}
 
Example #2
Source File: SnsTopicResourceTest.java    From Cheddar with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldThrowException_onAmazonClientExceptionFromSubscribe() {
    // Given
    final SqsQueueResource mockSqsQueueResource = mock(SqsQueueResource.class);
    final String queueArn = randomString();
    when(mockSqsQueueResource.queueArn()).thenReturn(queueArn);
    when(mockAmazonSnsClient.subscribe(any(SubscribeRequest.class))).thenThrow(AmazonClientException.class);

    // When
    AmazonClientException thrownException = null;
    try {
        snsTopicResource.subscribe(mockSqsQueueResource);
    } catch (final AmazonClientException e) {
        thrownException = e;
    }

    // Then
    assertNotNull(thrownException);
}
 
Example #3
Source File: AwsGlacierInventoryRetriever.java    From core with GNU General Public License v3.0 6 votes vote down vote up
/**
 * For retrieving vault inventory. For initializing SNS for determining when
 * job completed. Does nothing if member snsTopicName is null. Sets members
 * snsTopicARN and snsSubscriptionARN.
 */
   void setupSNS() {
	// If no snsTopicName setup then simply return
	if (snsTopicName == null)
		return;

	CreateTopicRequest request = new CreateTopicRequest()
			.withName(snsTopicName);
	CreateTopicResult result = snsClient.createTopic(request);
	snsTopicARN = result.getTopicArn();

	SubscribeRequest request2 = new SubscribeRequest()
			.withTopicArn(snsTopicARN).withEndpoint(sqsQueueARN)
			.withProtocol("sqs");
	SubscribeResult result2 = snsClient.subscribe(request2);

	snsSubscriptionARN = result2.getSubscriptionArn();
}
 
Example #4
Source File: TopicImpl.java    From aws-sdk-java-resources with Apache License 2.0 5 votes vote down vote up
@Override
public Subscription subscribe(SubscribeRequest request,
        ResultCapture<SubscribeResult> extractor) {

    ActionResult result = resource.performAction("Subscribe", request,
            extractor);

    if (result == null) return null;
    return new SubscriptionImpl(result.getResource());
}
 
Example #5
Source File: TopicImpl.java    From aws-sdk-java-resources with Apache License 2.0 5 votes vote down vote up
@Override
public Subscription subscribe(String endpoint, String protocol,
        ResultCapture<SubscribeResult> extractor) {

    SubscribeRequest request = new SubscribeRequest()
        .withEndpoint(endpoint)
        .withProtocol(protocol);
    return subscribe(request, extractor);
}
 
Example #6
Source File: SnsExecutor.java    From spring-integration-aws with MIT License 5 votes vote down vote up
private void processUrlSubscription(Subscription urlSubscription) {

		String snsUrlSubscriptionArn = null;
		for (Subscription subscription : client.listSubscriptions()
				.getSubscriptions()) {
			if (subscription.getTopicArn().equals(topicArn)
					&& subscription.getProtocol().equals(
							urlSubscription.getProtocol())
					&& subscription.getEndpoint().contains(
							urlSubscription.getEndpoint())) {
				if (!subscription.getSubscriptionArn().equals(
						"PendingConfirmation")) {
					snsUrlSubscriptionArn = subscription.getSubscriptionArn();
					break;
				}
			}
		}
		if (snsUrlSubscriptionArn == null) {

			SubscribeRequest request = new SubscribeRequest(topicArn,
					urlSubscription.getProtocol(),
					urlSubscription.getEndpoint());
			SubscribeResult result = client.subscribe(request);
			snsUrlSubscriptionArn = result.getSubscriptionArn();
			log.info("Subscribed URL to SNS with subscription ARN: "
					+ snsUrlSubscriptionArn);
		} else {
			log.info("Already subscribed with ARN: " + snsUrlSubscriptionArn);
		}
	}
 
Example #7
Source File: SnsMessageBroker.java    From xyz-hub with Apache License 2.0 4 votes vote down vote up
private void subscribeOwnNode(Handler<AsyncResult<Void>> callback) {
  logger.info("Subscribing the NODE=" + Node.OWN_INSTANCE.getUrl());

  final String subscriptionErrorMsg = "The Node could not be subscribed as AdminMessage listener. "
      + "No AdminMessages will be received by this node.";
  if (OWN_NODE_MESSAGING_URL == null)
    throw new NullPointerException("No messaging node URL provided. " + subscriptionErrorMsg);

  //First check whether there is an existing subscription for the own endpoint
  loadSubscriptions(subscriptionsResult -> {
    if (subscriptionsResult.succeeded()) {
      oldSubscriptions = subscriptionsResult.result();
      logger.info("Subscriptions have been loaded [" + oldSubscriptions.size() + "] for NODE=" + Node.OWN_INSTANCE.getUrl());
      /*
      Check whether a subscription to the own node's endpoint already exists.
      (could happen by accident when this node re-uses an IP from a previously running node)
      */
      if (oldSubscriptions.stream().noneMatch(subscription -> OWN_NODE_MESSAGING_URL.equals(subscription.getEndpoint()))) {
        logger
            .info("Current node is not subscribed yet, subscribing NODE=" + Node.OWN_INSTANCE.getUrl() + " into TOPIC_ARN=" + TOPIC_ARN);
        SNS_CLIENT
            .subscribeAsync(TOPIC_ARN, SNS_HTTP_PROTOCOL, OWN_NODE_MESSAGING_URL, new AsyncHandler<SubscribeRequest, SubscribeResult>() {
              @Override
              public void onError(Exception e) {
                logger.error(subscriptionErrorMsg, e);
                callback.handle(Future.failedFuture(e));
              }

              @Override
              public void onSuccess(SubscribeRequest request, SubscribeResult subscribeResult) {
                logger.info("Subscription succeeded for NODE=" + Node.OWN_INSTANCE.getUrl() + " into TOPIC_ARN=" + TOPIC_ARN);
                callback.handle(Future.succeededFuture());
              }
            });
      }
    }
    else {
      logger.error(subscriptionErrorMsg, subscriptionsResult.cause());
    }
  });
}
 
Example #8
Source File: Sns.java    From eb-java-scorekeep with Apache License 2.0 4 votes vote down vote up
public static void createSubscription() {
  String topicarn = System.getenv("NOTIFICATION_TOPIC");
  String emailaddress = System.getenv("NOTIFICATION_EMAIL");
  SubscribeRequest subRequest = new SubscribeRequest(topicarn, "email", emailaddress);
  snsclient.subscribe(subRequest);
}
 
Example #9
Source File: TopicImpl.java    From aws-sdk-java-resources with Apache License 2.0 4 votes vote down vote up
@Override
public Subscription subscribe(SubscribeRequest request) {
    return subscribe(request, null);
}
 
Example #10
Source File: SnsExecutor.java    From spring-integration-aws with MIT License 4 votes vote down vote up
private void processSqsSubscription(Subscription sqsSubscription) {
	Assert.state(sqsExecutorMap != null,
			"'sqsExecutorMap' must not be null");

	SqsExecutor sqsExecutor = null;
	String endpointValue = sqsSubscription.getEndpoint();
	if (sqsExecutorMap.containsKey(endpointValue)) {
		sqsExecutor = sqsExecutorMap.get(endpointValue);
		sqsSubscription.setEndpoint(sqsExecutor.getQueueArn());
	} else {
		// endpointValue is the queue-arn
		sqsSubscription.setEndpoint(endpointValue);
	}

	String snsSqsSubscriptionArn = null;
	for (Subscription subscription : client.listSubscriptions()
			.getSubscriptions()) {
		if (subscription.getTopicArn().equals(topicArn)
				&& subscription.getProtocol().equals(
						sqsSubscription.getProtocol())
				&& subscription.getEndpoint().equals(
						sqsSubscription.getEndpoint())) {
			snsSqsSubscriptionArn = subscription.getSubscriptionArn();
			break;
		}
	}
	if (snsSqsSubscriptionArn == null) {
		SubscribeRequest request = new SubscribeRequest(topicArn,
				sqsSubscription.getProtocol(),
				sqsSubscription.getEndpoint());
		SubscribeResult result = client.subscribe(request);
		snsSqsSubscriptionArn = result.getSubscriptionArn();
		log.info("Subscribed SQS to SNS with subscription ARN: "
				+ snsSqsSubscriptionArn);
	} else {
		log.info("Already subscribed with ARN: " + snsSqsSubscriptionArn);
	}
	if (sqsExecutor != null) {
		sqsExecutor.addSnsPublishPolicy(topicName, topicArn);
	}
}
 
Example #11
Source File: SnsTopicResource.java    From Cheddar with Apache License 2.0 2 votes vote down vote up
/**
 * Adds an AWS SQS subscription to the AWS SNS topic.
 * @param sqsQueueResource {@link SqsQueueResource} representative of AWS SQS queue subscribing to the AWS SNS
 *            topic.
 * @throws AmazonClientException
 */
public void subscribe(final SqsQueueResource sqsQueueResource) throws AmazonClientException {
    amazonSnsClient.subscribe(new SubscribeRequest(topicArn, "sqs", sqsQueueResource.queueArn()));
}
 
Example #12
Source File: Topic.java    From aws-sdk-java-resources with Apache License 2.0 2 votes vote down vote up
/**
 * Performs the <code>Subscribe</code> action.
 *
 * <p>
 * The following request parameters will be populated from the data of this
 * <code>Topic</code> resource, and any conflicting parameter value set in
 * the request will be overridden:
 * <ul>
 *   <li>
 *     <b><code>TopicArn</code></b>
 *         - mapped from the <code>Arn</code> identifier.
 *   </li>
 * </ul>
 *
 * <p>
 *
 * @return The <code>Subscription</code> resource object associated with the
 *         result of this action.
 * @see SubscribeRequest
 */
com.amazonaws.resources.sns.Subscription subscribe(SubscribeRequest request)
        ;
 
Example #13
Source File: Topic.java    From aws-sdk-java-resources with Apache License 2.0 2 votes vote down vote up
/**
 * Performs the <code>Subscribe</code> action and use a ResultCapture to
 * retrieve the low-level client response.
 *
 * <p>
 * The following request parameters will be populated from the data of this
 * <code>Topic</code> resource, and any conflicting parameter value set in
 * the request will be overridden:
 * <ul>
 *   <li>
 *     <b><code>TopicArn</code></b>
 *         - mapped from the <code>Arn</code> identifier.
 *   </li>
 * </ul>
 *
 * <p>
 *
 * @return The <code>Subscription</code> resource object associated with the
 *         result of this action.
 * @see SubscribeRequest
 */
com.amazonaws.resources.sns.Subscription subscribe(SubscribeRequest request,
        ResultCapture<SubscribeResult> extractor);