sqs.setQueueAttributes(new SetQueueAttributesRequest(sqsQueueUrl, newAttrs)); SubscribeResult subscribeResult = sns.subscribe(snsTopicArn, "sqs", sqsQueueArn); return subscribeResult.getSubscriptionArn();
private void setupQueueAndTopic() { String randomSeed = UUID.randomUUID().toString(); String queueName = "glacier-archive-transfer-" + randomSeed; String topicName = "glacier-archive-transfer-" + randomSeed; queueUrl = sqs.createQueue(new CreateQueueRequest(queueName)).getQueueUrl(); topicArn = sns.createTopic(new CreateTopicRequest(topicName)).getTopicArn(); String queueARN = sqs.getQueueAttributes(new GetQueueAttributesRequest(queueUrl).withAttributeNames("QueueArn")).getAttributes().get("QueueArn"); Policy sqsPolicy = new Policy().withStatements( new Statement(Effect.Allow) .withPrincipals(Principal.AllUsers) .withActions(SQSActions.SendMessage) .withResources(new Resource(queueARN)) .withConditions(ConditionFactory.newSourceArnCondition(topicArn))); sqs.setQueueAttributes(new SetQueueAttributesRequest(queueUrl, newAttributes("Policy", sqsPolicy.toJson()))); sns.subscribe(new SubscribeRequest(topicArn, "sqs", queueARN)); }
sns.subscribe(new SubscribeRequest() .withEndpoint(sqsQueueArn) .withProtocol("sqs")
/** * Returns the subscription arn resulting from subscribing the queueARN to the topicARN */ public static String subscribeToTopic(AmazonSNS amazonSNS, ARN topicARN, ARN queueARN) { return amazonSNS.subscribe(topicARN.getArn(), "sqs", queueARN.getArn()).getSubscriptionArn(); }
String snsTopicArn = sns.createTopic(SNS_TOPIC).getTopicArn(); String sqsArn = sqs.getQueueAttributes(sqsUrl, Arrays.asList("QueueArn")).getAttributes().get("QueueArn"); String sqsSubscriptionArn = sns.subscribe(snsTopicArn, "sqs", sqsArn).getSubscriptionArn(); Policy allowSnsToPostToSqsPolicy = new Policy("allow sns " + snsTopicArn + " to send to queue", Arrays.asList( new Statement(Statement.Effect.Allow) )); sqs.setQueueAttributes(new SetQueueAttributesRequest().withQueueUrl(sqsUrl).addAttributesEntry("Policy", allowSnsToPostToSqsPolicy.toJson())); String sqsSubscriptionArn = sns.subscribe(snsTopicArn, "sqs", sqsArn).getSubscriptionArn(); sns.publish(snsTopicArn, "Hello world"); ReceiveMessageResult receiveResp = sqs.receiveMessage(new ReceiveMessageRequest(sqsUrl).withWaitTimeSeconds(10));
@Override public String subscribeSnsToQueue(String snsTopic, String queueName) { try { String queueUrl = sqs.getQueueUrl(queueName).getQueueUrl(); LOG.info("Subscribing SNS topic {} to the queue: {}", snsTopic, queueUrl); String queueArn = sqs.getQueueAttributes(queueUrl, ImmutableList.of("QueueArn")).getAttributes().get("QueueArn"); Policy policy = new Policy().withStatements( new Statement(Statement.Effect.Allow) .withActions(SQSActions.SendMessage) .withPrincipals(Principal.All) .withResources(new Resource(queueArn)) .withConditions(ConditionFactory.newSourceArnCondition(snsTopic))); sqs.setQueueAttributes(new SetQueueAttributesRequest() .withQueueUrl(queueUrl) .addAttributesEntry(QueueAttributeName.Policy.toString(), policy.toJson()) ); String snsSubscriptionArn = sns.subscribe(snsTopic, "sqs", queueArn).getSubscriptionArn(); LOG.info("Subscribed SNS to dedicated SQS queue. subscriptionArn: {}", snsSubscriptionArn); return snsSubscriptionArn; } catch (AmazonClientException e) { LOG.info("Failed to subscribe to subscribe SNS to SQS queue", e); throw new RuntimeException(e); } }
private static String ensureTopicExists(AmazonSNS amazonSNS, ARN topicARN, List<String> allAccountIds, ARN queueARN) { topicARN.arn = amazonSNS.createTopic(topicARN.name).getTopicArn(); amazonSNS.setTopicAttributes( new SetTopicAttributesRequest() .withTopicArn(topicARN.arn) .withAttributeName("Policy") .withAttributeValue(buildSNSPolicy(topicARN, allAccountIds).toJson()) ); amazonSNS.subscribe(topicARN.arn, "sqs", queueARN.arn); return topicARN.arn; }
/** * Ensure that the topic exists and has a policy granting all accounts permission to publish messages to it */ private static String ensureTopicExists(AmazonSNS amazonSNS, ARN topicARN, List<String> allAccountIds, ARN queueARN) { topicARN.arn = amazonSNS.createTopic(topicARN.name).getTopicArn(); amazonSNS.setTopicAttributes( new SetTopicAttributesRequest() .withTopicArn(topicARN.arn) .withAttributeName("Policy") .withAttributeValue(buildSNSPolicy(topicARN, allAccountIds).toJson()) ); amazonSNS.subscribe(topicARN.arn, "sqs", queueARN.arn); return topicARN.arn; }
private void setupQueueAndTopic() { String randomSeed = UUID.randomUUID().toString(); String queueName = "glacier-archive-transfer-" + randomSeed; String topicName = "glacier-archive-transfer-" + randomSeed; queueUrl = sqs.createQueue(new CreateQueueRequest(queueName)).getQueueUrl(); topicArn = sns.createTopic(new CreateTopicRequest(topicName)).getTopicArn(); String queueARN = sqs.getQueueAttributes(new GetQueueAttributesRequest(queueUrl).withAttributeNames("QueueArn")).getAttributes().get("QueueArn"); Policy sqsPolicy = new Policy().withStatements( new Statement(Effect.Allow) .withPrincipals(Principal.AllUsers) .withActions(SQSActions.SendMessage) .withResources(new Resource(queueARN)) .withConditions(ConditionFactory.newSourceArnCondition(topicArn))); sqs.setQueueAttributes(new SetQueueAttributesRequest(queueUrl, newAttributes("Policy", sqsPolicy.toJson()))); sns.subscribe(new SubscribeRequest(topicArn, "sqs", queueARN)); }
@Override public String subscribe(NevadoTopic topic, NevadoQueue topicEndpoint) throws JMSException { String subscriptionArn; try { SQSQueue queue = getSQSQueue((NevadoDestination) topicEndpoint); String sqsArn = queue.getQueueARN(); String snsArn = getTopicARN(topic); queue.setPolicy(getPolicy(snsArn, sqsArn)); subscriptionArn = _amazonSNS.subscribe(new SubscribeRequest().withTopicArn(getTopicARN(topic)) .withProtocol("sqs").withEndpoint(sqsArn)).getSubscriptionArn(); } catch (AmazonClientException e) { throw handleAWSException("Unable to subscripe to topic " + topic, e); } return subscriptionArn; }
sns.subscribe(new SubscribeRequest() .withEndpoint(sqsQueueArn) .withProtocol("sqs")