List of usage examples for com.amazonaws.services.sqs AmazonSQSClient AmazonSQSClient
AmazonSQSClient(AwsSyncClientParams clientParams)
From source file:awslabs.lab31.Lab31.java
License:Open Source License
public static void main(String[] args) { try {//from w w w .ja va2 s . 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_TestSqsAccess(Region region, BasicSessionCredentials credentials) { try {/*from w ww. jav a 2 s. c o m*/ AmazonSQSClient sqsClient = new AmazonSQSClient(credentials); sqsClient.setRegion(region); sqsClient.listQueues(new ListQueuesRequest()); return true; } catch (Exception ex) { return false; } }
From source file:Cloud.Tweets.SimpleQueueServiceSample.java
License:Open Source License
public AmazonSQS createSQSs() { System.out.println("helloooooooooooo"); AWSCredentials credentials;/*from w w w .j ava 2 s .c o m*/ try { credentials = new PropertiesCredentials( SimpleQueueServiceSample.class.getResourceAsStream("AwsCredentials.Properties")); System.out.println("hello"); //credentials = new ProfileCredentialsProvider("~/.aws/AwsCredentials.Properties").getCredentials(); } catch (Exception e) { throw new AmazonClientException("Cannot load the credentials from the credential profiles file. " + "Please make sure that your credentials file is at the correct " + "location (/Users/daniel/.aws/credentials), and is in valid format.", e); } AmazonSQS sqs = new AmazonSQSClient(credentials); System.out.println(sqs.toString()); Region usWest2 = Region.getRegion(Regions.US_WEST_2); sqs.setRegion(usWest2); System.out.println("==========================================="); System.out.println("Getting Started with Amazon SQS"); System.out.println("===========================================\n"); // Create a queue System.out.println("Creating a new SQS queue called MyQueue.\n"); CreateQueueRequest createQueueRequest = new CreateQueueRequest("MyQueue"); myQueueUrl = sqs.createQueue(createQueueRequest).getQueueUrl(); // List queues System.out.println("Listing all queues in your account.\n"); for (String queueUrl : sqs.listQueues().getQueueUrls()) { System.out.println(" QueueUrl: " + queueUrl); } System.out.println(); return sqs; }
From source file:cloudworker.RemoteWorker.java
License:Apache License
private static void init() throws Exception { /*/*from ww w.j av a 2s . c o m*/ * The ProfileCredentialsProvider will return your [default] * credential profile by reading from the credentials file located at * (~/.aws/credentials). */ AWSCredentials credentials = null; try { credentials = new ProfileCredentialsProvider().getCredentials(); } catch (Exception e) { throw new AmazonClientException("Cannot load the credentials from the credential profiles file. " + "Please make sure that your credentials file is at the correct " + "location (~/.aws/credentials), and is in valid format.", e); } ec2 = new AmazonEC2Client(credentials); sqs = new AmazonSQSClient(credentials); Region usEast1 = Region.getRegion(Regions.US_EAST_1); ec2.setRegion(usEast1); sqs.setRegion(usEast1); dynamoDB = new DynamoDBService(credentials); }
From source file:com.aipo.aws.sqs.SQS.java
License:Open Source License
public static AmazonSQS getClient() { AWSContext awsContext = AWSContext.get(); if (awsContext == null) { throw new IllegalStateException("AWSContext is not initialized."); }/*from www.j a va2 s .c o m*/ AmazonSQS client = new AmazonSQSClient(awsContext.getAwsCredentials()); String endpoint = awsContext.getSqsEndpoint(); if (endpoint != null && endpoint != "") { client.setEndpoint(endpoint); } return client; }
From source file:com.allogy.amazonaws.elasticbeanstalk.worker.simulator.application.Config.java
License:Apache License
@Bean public AmazonSQS amazonSQS() { return new AmazonSQSClient(awsCredentials()); }
From source file:com.amazon.aws.demo.anonymous.AmazonClientManager.java
License:Open Source License
public synchronized Response validateCredentials() { Response ableToGetToken = Response.SUCCESSFUL; if (AmazonSharedPreferencesWrapper.areCredentialsExpired(this.sharedPreferences)) { Log.i(LOG_TAG, "Credentials were expired."); clearCredentials();/*from www . j av a2 s. c o m*/ AmazonTVMClient tvm = new AmazonTVMClient(this.sharedPreferences, PropertyLoader.getInstance().getTokenVendingMachineURL(), PropertyLoader.getInstance().useSSL()); ableToGetToken = tvm.anonymousRegister(); if (ableToGetToken.requestWasSuccessful()) { ableToGetToken = tvm.getToken(); } } if (ableToGetToken.requestWasSuccessful() && (s3Client == null || sqsClient == null || sdbClient == null || snsClient == null)) { Log.i(LOG_TAG, "Creating New Credentials."); AWSCredentials credentials = AmazonSharedPreferencesWrapper .getCredentialsFromSharedPreferences(this.sharedPreferences); s3Client = new AmazonS3Client(credentials); sqsClient = new AmazonSQSClient(credentials); sdbClient = new AmazonSimpleDBClient(credentials); snsClient = new AmazonSNSClient(credentials); } return ableToGetToken; }
From source file:com.amazon.aws.demo.identity.AmazonClientManager.java
License:Open Source License
public synchronized Response validateCredentials() { Response ableToGetToken = Response.SUCCESSFUL; if (AmazonSharedPreferencesWrapper.areCredentialsExpired(this.sharedPreferences)) { Log.i(LOG_TAG, "Credentials were expired."); clearCredentials();/* w w w. j av a 2 s . co m*/ AmazonTVMClient tvm = new AmazonTVMClient(this.sharedPreferences, PropertyLoader.getInstance().getTokenVendingMachineURL(), PropertyLoader.getInstance().getAppName(), PropertyLoader.getInstance().useSSL()); if (ableToGetToken.requestWasSuccessful()) { ableToGetToken = tvm.getToken(); } } if (ableToGetToken.requestWasSuccessful() && (s3Client == null || sqsClient == null || sdbClient == null || snsClient == null)) { Log.i(LOG_TAG, "Creating New Credentials."); AWSCredentials credentials = AmazonSharedPreferencesWrapper .getCredentialsFromSharedPreferences(this.sharedPreferences); s3Client = new AmazonS3Client(credentials); sqsClient = new AmazonSQSClient(credentials); sdbClient = new AmazonSimpleDBClient(credentials); snsClient = new AmazonSNSClient(credentials); } return ableToGetToken; }
From source file:com.athena.sqs.MessageContext.java
License:Apache License
@PostConstruct public void doConnect() throws Exception { try {//from www. j a va 2 s .co m //AmazonSQSClient sqs = new AmazonSQSAsyncClient(credentials); AmazonSQSClient sqs = new AmazonSQSClient(credentials); sqs.setEndpoint("http://sqs.eu-west-1.amazonaws.com"); logger.debug("Start Connection with Amazon SQS"); this.sqsClient = sqs; logger.debug("Binding Amazon SQS successfully."); } catch (Exception e) { e.printStackTrace(); } }
From source file:com.boundary.aws.sqs.Sample.java
License:Open Source License
public static void main(String[] args) throws Exception { String queueName = "boundary-sqs-demo-queue"; /*/*ww w . jav a 2s . co m*/ * The ProfileCredentialsProvider will return your [default] credential * profile by reading from the credentials file located at * (HOME/.aws/credentials). */ AWSCredentials credentials = null; try { credentials = new ProfileCredentialsProvider("default").getCredentials(); } catch (Exception e) { throw new AmazonClientException("Cannot load the credentials from the credential profiles file. ", e); } AmazonSQS sqs = new AmazonSQSClient(credentials); Region usWest2 = Region.getRegion(Regions.US_WEST_2); sqs.setRegion(usWest2); try { // Create a queue System.out.printf("Creating queue: %s.\n", queueName); CreateQueueRequest createQueueRequest = new CreateQueueRequest(queueName); String myQueueUrl = sqs.createQueue(createQueueRequest).getQueueUrl(); int messageCount = 100; // Send a messages for (int count = 1; count <= messageCount; count++) { System.out.printf("Sending message %3d to %s.\n", count, queueName); sqs.sendMessage(new SendMessageRequest(myQueueUrl, new Date() + ": This is my message text.")); } for (int count = 1; count <= messageCount; count++) { ReceiveMessageRequest receiveMessageRequest = new ReceiveMessageRequest(myQueueUrl); List<Message> messages = sqs.receiveMessage(receiveMessageRequest).getMessages(); for (Message msg : messages) { System.out.printf("Received message: %s queue: %s body: %s\n", msg.getMessageId(), queueName, msg.getBody()); System.out.printf("Deleting message: %s queue: %s\n", msg.getMessageId(), queueName); String messageRecieptHandle = msg.getReceiptHandle(); sqs.deleteMessage(new DeleteMessageRequest(myQueueUrl, messageRecieptHandle)); } } System.out.printf("Deleting queue: %s\n", queueName); sqs.deleteQueue(new DeleteQueueRequest(myQueueUrl)); } catch (AmazonServiceException ase) { System.out.println("Caught an AmazonServiceException, which means your request made it " + "to Amazon SQS, but was rejected with an error response for some reason."); System.out.println("Error Message: " + ase.getMessage()); System.out.println("HTTP Status Code: " + ase.getStatusCode()); System.out.println("AWS Error Code: " + ase.getErrorCode()); System.out.println("Error Type: " + ase.getErrorType()); System.out.println("Request ID: " + ase.getRequestId()); } catch (AmazonClientException ace) { System.out.println("Caught an AmazonClientException, which means the client encountered " + "a serious internal problem while trying to communicate with SQS, such as not " + "being able to access the network."); System.out.println("Error Message: " + ace.getMessage()); } sqs.shutdown(); }