@Override public PublishResult retriableCall() throws Exception { PublishRequest publishRequest = new PublishRequest(topic_arn, msg) .withMessageAttributes(messageAttributes); return snsClient.publish(publishRequest); } }.call();
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)); }
public void shutdown() { try { sqs.deleteQueue(new DeleteQueueRequest(queueUrl)); } catch (Exception e) { log.warn("Unable to delete queue: " + queueUrl, e); } try { sns.deleteTopic(new DeleteTopicRequest(topicArn)); } catch (Exception e) { log.warn("Unable to delete topic: " + topicArn, e); } }
AmazonSQS sqs = region.createClient(AmazonSQSClient.class, credentialsChain, clientConfiguration); String sqsUrl = sqs.createQueue(new CreateQueueRequest(SQS_NAME)).getQueueUrl(); 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)); for (Message message: receiveResp.getMessages()) { sns.deleteTopic(snsTopicArn); sqs.deleteQueue(sqsUrl);
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; }
sqs.setQueueAttributes(new SetQueueAttributesRequest(sqsQueueUrl, newAttrs)); SubscribeResult subscribeResult = sns.subscribe(snsTopicArn, "sqs", sqsQueueArn); return subscribeResult.getSubscriptionArn();
platformApplicationRequest.setName(applicationName); platformApplicationRequest.setPlatform(platform.name()); return snsClient.createPlatformApplication(platformApplicationRequest); return snsClient.createPlatformEndpoint(platformEndpointRequest); DeletePlatformApplicationRequest request = new DeletePlatformApplicationRequest(); request.setPlatformApplicationArn(applicationArn); snsClient.deletePlatformApplication(request); return snsClient.publish(publishRequest);
@Override public String get() { // This will create the topic if it doesn't exist or return the existing topic if it does. CreateTopicResult topic = _amazonSNS.createTopic(topicName); return topic.getTopicArn(); } });
GetTopicAttributesResult topicAttributes = snsClient_.getTopicAttributes(topicArn); CreateTopicResult createTopicResult = snsClient_.createTopic(createTopicRequest);
/** * Deletes the given topic and shuts down the given sns client if they are not null. * * @param topicArn the the Amazon Resource Name (ARN) of the topic to delete * @param snsClient the Simple Notification Service (SNS) client to shutdown */ public static void deleteTopicAndShutdown(final String topicArn, final AmazonSNS snsClient) { if (topicArn != null && snsClient != null) { snsClient.deleteTopic(topicArn); snsClient.shutdown(); } } }
ListSubscriptionsByTopicResult topicSubscriptions = snsClient_.listSubscriptionsByTopic(topicArn); List<Subscription> subscriptions = topicSubscriptions.getSubscriptions(); snsClient_.deleteTopic(topicArn); log_.info("Deleted topic " + topicName);
ListSubscriptionsByTopicResult subscriptionResult = snsClient_.listSubscriptionsByTopic(getTopicARN(topicName)); snsClient_.unsubscribe(subscription.getSubscriptionArn());
@Override public void unsubscribe(String snsSubscriptionArn) { try { LOG.info("Cleaning up SNS subscription {}.", snsSubscriptionArn); sns.unsubscribe(snsSubscriptionArn); } catch (AmazonClientException e) { LOG.info("Failed to unsubscribe SNS from SQS queue", e); throw new RuntimeException(e); } } }
@Teardown public void tearDown() { if (producer != null) { producer.shutdown(); producer = null; } }
/** * 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; }
sns.subscribe(new SubscribeRequest() .withEndpoint(sqsQueueArn) .withProtocol("sqs")
/** * Creates a new sns test topic. * * @param snsClient the Simple Notification Service (SNS) client * @return the Amazon Resource Name (ARN) of the created sns topic */ public static String createTestTopic(final AmazonSNS snsClient) { final String queueName = "jvm-sdk-test-topic-" + UUID.randomUUID(); final CreateTopicResult queueCreationResult = snsClient.createTopic(queueName); return queueCreationResult.getTopicArn(); }
@Override public void unsubscribe(NevadoTopic topic) throws JMSException { if (topic == null) { throw new NullPointerException(); } if (topic.getSubscriptionArn() == null) { throw new IllegalArgumentException("Topic doesn't have a subscription"); } try { _amazonSNS.unsubscribe(new UnsubscribeRequest().withSubscriptionArn(topic.getSubscriptionArn())); } catch (AmazonClientException e) { throw handleAWSException("Unable to subscribe topic " + topic + " with sub ARN " + topic.getSubscriptionArn(), e); } }
@Override public void stop() { snsClient_.shutdown(); for(SnsPublisher publisher : publishers_) { publisher.close(); } }
/** * Publishes message to associated SNS topic. * * @param message Message to publish (will always be serialized to JSON, even if it's plain string). * @return Operation results. * @throws JsonProcessingException When message could not be serialized. */ public PublishResult publish(Object message) throws JsonProcessingException { return this.sns.publish(this.topicArn, this.objectMapper.writeValueAsString(message)); }