List of usage examples for com.amazonaws.services.sns AmazonSNSClient createTopic
@Override
public CreateTopicResult createTopic(String name)
From source file:awslabs.lab31.SolutionCode.java
License:Open Source License
@Override public String createTopic(AmazonSNSClient snsClient, String topicName) { // TODO: Construct a CreateTopicRequest object using the specified topic name. CreateTopicRequest createTopicRequest = new CreateTopicRequest().withName(topicName); // TODO: Submit the request using the createTopic method of the snsClient object. CreateTopicResult createTopicResult = snsClient.createTopic(createTopicRequest); // TODO: Return the topic ARN from the request result. return createTopicResult.getTopicArn(); }
From source file:awslabs.lab31.StudentCode.java
License:Open Source License
/** * Create an SNS topic and return the ARN for the newly created topic. Hint: Use the createTopic() method of the * client object. The ARN for the topic is contained in the response. * // w ww .j a v a 2s . co m * @param snsClient The SNS client object. * @param topicName The name of the topic to create. * @return The ARN for the newly created topic. */ @Override public String createTopic(AmazonSNSClient snsClient, String topicName) { CreateTopicRequest request = new CreateTopicRequest().withName(topicName); CreateTopicResult result = snsClient.createTopic(request); return result.getTopicArn(); }
From source file:com.comcast.cmb.test.tools.CMBTutorial.java
License:Apache License
public static void main(String[] args) { try {//from w ww . java 2s.co m Util.initLog4jTest(); //TODO: set user id and credentials for two distinct users // user "cqs_test_1" //BasicAWSCredentials user1Credentials = new BasicAWSCredentials("<access_key>", "<secret_key>"); BasicAWSCredentials user1Credentials = new BasicAWSCredentials("Z2DVBFRNZ2C2SSXDWS5F", "bH2UQiJkpctBaE3eaDob19fj5J9Q1FVafrZantBp"); // user "cqs_test_2" //String user2Id = "<user_id>"; String user2Id = "389653920093"; //BasicAWSCredentials user2Credentials = new BasicAWSCredentials("<access_key>", "<secret_key>"); BasicAWSCredentials user2Credentials = new BasicAWSCredentials("QL8Q1VOBCSJC5FZ2DMIU", "n6a82gyJZ04Z+Xqp7OgfqPtbbKqVc3UbuOTNrF+7"); // service urls //TODO: add service URLs //String cqsServerUrl = "http://<host>:<port>"; //String cnsServerUrl = "http://<host>:<port>"; String cqsServerUrl = "http://localhost:6059"; String cnsServerUrl = "http://localhost:6061"; // initialize service AmazonSQSClient sqs = new AmazonSQSClient(user1Credentials); sqs.setEndpoint(cqsServerUrl); AmazonSNSClient sns = new AmazonSNSClient(user2Credentials); sns.setEndpoint(cnsServerUrl); // create queue Random randomGenerator = new Random(); String queueName = QUEUE_PREFIX + randomGenerator.nextLong(); HashMap<String, String> attributeParams = new HashMap<String, String>(); CreateQueueRequest createQueueRequest = new CreateQueueRequest(queueName); createQueueRequest.setAttributes(attributeParams); String queueUrl = sqs.createQueue(createQueueRequest).getQueueUrl(); AddPermissionRequest addPermissionRequest = new AddPermissionRequest(); addPermissionRequest.setQueueUrl(queueUrl); addPermissionRequest.setActions(Arrays.asList("SendMessage")); addPermissionRequest.setLabel(UUID.randomUUID().toString()); addPermissionRequest.setAWSAccountIds(Arrays.asList(user2Id)); sqs.addPermission(addPermissionRequest); // create topic String topicName = "TSTT" + randomGenerator.nextLong(); CreateTopicRequest createTopicRequest = new CreateTopicRequest(topicName); CreateTopicResult createTopicResult = sns.createTopic(createTopicRequest); String topicArn = createTopicResult.getTopicArn(); // subscribe and confirm cqs endpoint SubscribeRequest subscribeRequest = new SubscribeRequest(); String queueArn = getArnForQueueUrl(queueUrl); subscribeRequest.setEndpoint(queueArn); subscribeRequest.setProtocol("cqs"); subscribeRequest.setTopicArn(topicArn); SubscribeResult subscribeResult = sns.subscribe(subscribeRequest); String subscriptionArn = subscribeResult.getSubscriptionArn(); if (subscriptionArn.equals("pending confirmation")) { Thread.sleep(500); ReceiveMessageRequest receiveMessageRequest = new ReceiveMessageRequest(); receiveMessageRequest.setQueueUrl(queueUrl); receiveMessageRequest.setMaxNumberOfMessages(1); ReceiveMessageResult receiveMessageResult = sqs.receiveMessage(receiveMessageRequest); List<Message> messages = receiveMessageResult.getMessages(); if (messages != null && messages.size() == 1) { JSONObject o = new JSONObject(messages.get(0).getBody()); if (!o.has("SubscribeURL")) { throw new Exception("message is not a confirmation messsage"); } String subscriptionUrl = o.getString("SubscribeURL"); httpGet(subscriptionUrl); DeleteMessageRequest deleteMessageRequest = new DeleteMessageRequest(); deleteMessageRequest.setReceiptHandle(messages.get(0).getReceiptHandle()); deleteMessageRequest.setQueueUrl(queueUrl); sqs.deleteMessage(deleteMessageRequest); } else { throw new Exception("no confirmation message found"); } } // publish and receive message PublishRequest publishRequest = new PublishRequest(); String messageText = "quamvis sint sub aqua, sub aqua maledicere temptant"; publishRequest.setMessage(messageText); publishRequest.setSubject("unit test message"); publishRequest.setTopicArn(topicArn); sns.publish(publishRequest); Thread.sleep(500); ReceiveMessageRequest receiveMessageRequest = new ReceiveMessageRequest(); receiveMessageRequest.setQueueUrl(queueUrl); receiveMessageRequest.setMaxNumberOfMessages(1); ReceiveMessageResult receiveMessageResult = sqs.receiveMessage(receiveMessageRequest); List<Message> messages = receiveMessageResult.getMessages(); if (messages != null && messages.size() == 1) { String messageBody = messages.get(0).getBody(); if (!messageBody.contains(messageText)) { throw new Exception("message text not found"); } DeleteMessageRequest deleteMessageRequest = new DeleteMessageRequest(); deleteMessageRequest.setReceiptHandle(messages.get(0).getReceiptHandle()); deleteMessageRequest.setQueueUrl(queueUrl); sqs.deleteMessage(deleteMessageRequest); } else { throw new Exception("no messages found"); } // subscribe and confirm http endpoint String id = randomGenerator.nextLong() + ""; String endPointUrl = CMBTestingConstants.HTTP_ENDPOINT_BASE_URL + "recv/" + id; String lastMessageUrl = CMBTestingConstants.HTTP_ENDPOINT_BASE_URL + "info/" + id + "?showLast=true"; subscribeRequest = new SubscribeRequest(); subscribeRequest.setEndpoint(endPointUrl); subscribeRequest.setProtocol("http"); subscribeRequest.setTopicArn(topicArn); subscribeResult = sns.subscribe(subscribeRequest); subscriptionArn = subscribeResult.getSubscriptionArn(); if (subscriptionArn.equals("pending confirmation")) { Thread.sleep(500); String response = httpGet(lastMessageUrl); JSONObject o = new JSONObject(response); if (!o.has("SubscribeURL")) { throw new Exception("message is not a confirmation messsage"); } String subscriptionUrl = o.getString("SubscribeURL"); response = httpGet(subscriptionUrl); } // publish and receive message publishRequest = new PublishRequest(); publishRequest.setMessage(messageText); publishRequest.setSubject("unit test message"); publishRequest.setTopicArn(topicArn); sns.publish(publishRequest); Thread.sleep(500); String response = httpGet(lastMessageUrl); if (response != null && response.length() > 0) { if (!response.contains(messageText)) { throw new Exception("message text not found"); } } else { throw new Exception("no messages found"); } // delete queue and topic DeleteTopicRequest deleteTopicRequest = new DeleteTopicRequest(topicArn); sns.deleteTopic(deleteTopicRequest); sqs.deleteQueue(new DeleteQueueRequest(queueUrl)); System.out.println("OK"); } catch (Exception ex) { ex.printStackTrace(); } }
From source file:com.connexience.server.model.archive.glacier.SetupUtils.java
License:Open Source License
public static String setupSNS(String accessKey, String secretKey, String domainName, String vaultName, String queueARN) {/* w w w .j a v a 2s . c o m*/ String topicARN = null; try { AWSCredentials awsCredentials = new BasicAWSCredentials(accessKey, secretKey); AmazonSNSClient amazonSNSClient = new AmazonSNSClient(awsCredentials); amazonSNSClient.setEndpoint("https://sns." + domainName + ".amazonaws.com/"); String topicName = vaultName + "-inkspot_glacier-topic"; CreateTopicRequest createTopicRequest = new CreateTopicRequest(); createTopicRequest.withName(topicName); CreateTopicResult createTopicResult = amazonSNSClient.createTopic(createTopicRequest); if (createTopicResult != null) { topicARN = createTopicResult.getTopicArn(); SubscribeRequest subscribeRequest = new SubscribeRequest(); subscribeRequest.withTopicArn(topicARN); subscribeRequest.withProtocol("sqs"); subscribeRequest.withEndpoint(queueARN); SubscribeResult subscribeResult = amazonSNSClient.subscribe(subscribeRequest); if (subscribeResult == null) logger.warn("Unable to subscribe topic: \"" + topicName + "\" to queue: \"" + queueARN + "\""); } else logger.warn("Unable to create topic: \"" + topicName + "\""); amazonSNSClient.shutdown(); } catch (AmazonServiceException amazonServiceException) { logger.warn("AmazonServiceException: " + amazonServiceException); logger.debug(amazonServiceException); } catch (IllegalArgumentException illegalArgumentException) { logger.warn("IllegalArgumentException: " + illegalArgumentException); logger.debug(illegalArgumentException); } catch (AmazonClientException amazonClientException) { logger.warn("AmazonClientException: " + amazonClientException); logger.debug(amazonClientException); } catch (Throwable throwable) { logger.warn("Throwable: " + throwable); logger.debug(throwable); } return topicARN; }
From source file:com.dev.appx.sns.SNSMobilePush.java
License:Open Source License
public void createTopic(String topic) throws IOException { //create a new SNS client and set endpoint AmazonSNSClient snsClient = new AmazonSNSClient(new ClasspathPropertiesFileCredentialsProvider()); /*AmazonSNS snsClient = new AmazonSNSClient(new PropertiesCredentials( SNSMobilePush.class/*from w w w .j ava 2 s .co m*/ .getResourceAsStream("AwsCredentials.properties")));*/ snsClient.setRegion(Region.getRegion(Regions.US_EAST_1)); //create a new SNS topic CreateTopicRequest createTopicRequest = new CreateTopicRequest(topic); CreateTopicResult createTopicResult = snsClient.createTopic(createTopicRequest); //print TopicArn System.out.println(createTopicResult); //get request id for CreateTopicRequest from SNS metadata System.out.println("CreateTopicRequest - " + snsClient.getCachedResponseMetadata(createTopicRequest)); }
From source file:shnakkydoodle.notifying.provider.aws.AwsProvider.java
License:Open Source License
/** * Create a notification topic/*from w w w . j a va 2s . co m*/ * * @param name * @paramd escription */ @Override public void insertNotificationTopic(String name, String description) { AmazonSNSClient snsClient = getSNSClient(); CreateTopicRequest createTopicRequest = new CreateTopicRequest(awsName(name)); CreateTopicResult createTopicResult = snsClient.createTopic(createTopicRequest); snsClient.setTopicAttributes(createTopicResult.getTopicArn(), "DisplayName", name); }