List of usage examples for com.amazonaws.services.sns AmazonSNSClient setRegion
@Deprecated public void setRegion(Region region) throws IllegalArgumentException
From source file:awslabs.lab31.Lab31.java
License:Open Source License
public static void main(String[] args) { try {/* w w w. j av a 2s . c o m*/ // Create an SQS client AmazonSQSClient sqsClient = new AmazonSQSClient(new ClasspathPropertiesFileCredentialsProvider()); sqsClient.setRegion(region); // Create an SNS client AmazonSNSClient snsClient = new AmazonSNSClient(new ClasspathPropertiesFileCredentialsProvider()); snsClient.setRegion(region); String queueName = "Notifications"; String topicName = "ClassroomEvent"; // Creating the queue will fail if we've just deleted it and are recreating it // which is a possibility if you're tracking down a code error. If that happens, // pause and retry for up to a minute. System.out.println("Creating " + queueName + " queue."); Boolean retry = true, notified = false; // Create a timeout for 60-seconds from now. Date timeout = new Date(System.currentTimeMillis() + 60000L); String queueUrl = ""; while (retry) { try { queueUrl = labCode.createQueue(sqsClient, queueName); retry = false; } catch (QueueDeletedRecentlyException ex) { if (new Date().before(timeout)) { if (!notified) { System.out.println( "The attempt to recreate the queue failed because the queue was deleted too"); System.out.println("recently. Waiting and retrying for up to 1 minute."); notified = true; } // Timeout hasn't expired yet, so wait and retry in 5 seconds. System.out.print("."); Thread.sleep(5000); } else { System.out.println("Retry timeout expired. Aborting."); throw ex; } } } if (notified) { System.out.println("Recovered."); } System.out.println("URL for new queue: " + queueUrl); // List SQS queues System.out.println("Getting ARN for " + queueName + " queue."); String queueArn = labCode.getQueueArn(sqsClient, queueUrl); System.out.println("ARN for queue: " + queueArn); // Create an SNS topic and get ARN System.out.println("Creating " + topicName + " topic."); String topicArn = labCode.createTopic(snsClient, topicName); System.out.println("New topic ARN: " + topicArn); System.out.println("Granting the notification topic permission to post in the queue."); optionalLabCode.grantNotificationPermission(sqsClient, queueArn, queueUrl, topicArn); System.out.println("Permission granted."); // Create an SNS subscription System.out.println("Creating SNS subscription."); labCode.createSubscription(snsClient, queueArn, topicArn); System.out.println("Subscription created."); // Publish message to topic String messageText = "This is the SNS topic notification body."; String messageSubject = "SNSTopicNotification"; System.out.println("Publishing SNS topic notification."); labCode.publishTopicMessage(snsClient, topicArn, messageSubject, messageText); System.out.println("Notification published."); // Send a message to the "Notifications" queue messageText = "This is the message posted to the queue directly."; System.out.println("Posting message to queue directly."); labCode.postToQueue(sqsClient, queueUrl, messageText); System.out.println("Message posted."); // Read message from queue System.out.println("Reading messages from queue."); List<Message> messages = labCode.readMessages(sqsClient, queueUrl); // We expect two messages here if (messages.size() < 2) { // Try to read again and see if we've picked up the missing message(s). messages.addAll(labCode.readMessages(sqsClient, queueUrl)); if (messages.size() < 2) { System.out .println(">>WARNING<< We didn't receive the expected number of messages. Investigate."); } else { System.out.println(); System.out.println( "============================================================================"); System.out .println("PROBLEM: ReadMessages() had to be called twice to collect all the messages."); System.out.println(" Did you remember to set the MaxNumberOfMessages property in the "); System.out.println(" ReceiveMessageRequest object?"); System.out.println( "============================================================================"); System.out.println(); } } PrintAndRemoveMessagesInResponse(sqsClient, messages, queueUrl); // Locate and delete the SNS subscription System.out.println("Removing provisioned resources."); labCode.deleteSubscriptions(snsClient, topicArn); System.out.println("Subscriptions removed."); // Delete the SNS Topic labCode.deleteTopic(snsClient, topicArn); System.out.println("Topic deleted."); // Locate the previously created queue and delete labCode.deleteQueue(sqsClient, queueUrl); System.out.println("Queue deleted."); } catch (Exception ex) { LabUtility.dumpError(ex); } }
From source file:awslabs.lab41.SolutionCode.java
License:Open Source License
@Override public Boolean appMode_TestSnsAccess(Region region, BasicSessionCredentials credentials) { try {/* www . ja va 2s . c o m*/ AmazonSNSClient snsClient = new AmazonSNSClient(credentials); snsClient.setRegion(region); snsClient.listTopics(new ListTopicsRequest()); return true; } catch (Exception ex) { return false; } }
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/* w w w. ja v a2s . c o 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:io.ignitr.dispatchr.manager.Application.java
License:Apache License
@Autowired @Bean// w w w. j a va 2 s . co m public AmazonSNSClient amazonSNSClient(DeploymentContext deploymentContext) { AmazonSNSClient client = new AmazonSNSClient(new DefaultAWSCredentialsProviderChain()); client.setRegion(Region.getRegion(Regions.fromName(deploymentContext.getRegion()))); return client; }
From source file:jp.classmethod.aws.brian.config.AwsConfiguration.java
License:Apache License
@Bean public AmazonSNS sns() { AmazonSNSClient sns = new AmazonSNSClient(); sns.setRegion(region().getObject()); return sns; }
From source file:org.apache.usergrid.persistence.queue.impl.SNSQueueManagerImpl.java
License:Apache License
/** * The Synchronous SNS client is used for creating topics and subscribing queues. *///from www. ja v a2 s . com private AmazonSNSClient createSNSClient(final Region region) { final UsergridAwsCredentialsProvider ugProvider = new UsergridAwsCredentialsProvider(); final AmazonSNSClient sns = new AmazonSNSClient(ugProvider.getCredentials(), clientConfiguration); sns.setRegion(region); return sns; }
From source file:org.springframework.cloud.aws.messaging.config.annotation.SnsConfiguration.java
License:Apache License
@ConditionalOnMissingAmazonClient(AmazonSNS.class) @Bean//from ww w. j a v a2 s . com public AmazonSNS amazonSNS() { AmazonSNSClient amazonSNSClient; if (this.awsCredentialsProvider != null) { amazonSNSClient = new AmazonSNSClient(this.awsCredentialsProvider); } else { amazonSNSClient = new AmazonSNSClient(); } if (this.regionProvider != null) { amazonSNSClient.setRegion(this.regionProvider.getRegion()); } return amazonSNSClient; }
From source file:shnakkydoodle.notifying.provider.aws.AwsProvider.java
License:Open Source License
/** * Get the SNS Client already initialised * //from w w w.j a va 2s . com * @return */ private AmazonSNSClient getSNSClient() { AmazonSNSClient snsClient = new AmazonSNSClient(this.credentials); snsClient.setRegion(Region.EU_Ireland.toAWSRegion()); return snsClient; }