List of usage examples for com.amazonaws.services.sqs AmazonSQS createQueue
CreateQueueResult createQueue(String queueName);
From source file:SimpleQueueService.java
License:Open Source License
public static AmazonSQS push(HashMap<String, Integer> data, String Queuename) throws Exception { /*// www.ja v a 2 s .c om * This credentials provider implementation loads your AWS credentials * from a properties file at the root of your classpath. * * Important: Be sure to fill in your AWS access credentials in the * AwsCredentials.properties file before you try to run this * sample. * http://aws.amazon.com/security-credentials */ AmazonSQS sqs = new AmazonSQSClient(new ClasspathPropertiesFileCredentialsProvider()); /*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"); try { // Create a queue String myQueueUrl = null; System.out.println("Creating/ a new SQS queue called " + Queuename); CreateQueueRequest createQueueRequest = new CreateQueueRequest(Queuename); /** * the AWS API, the call to createQueue, shown in Listing 2, * doesn't necessarily create a new queue every time. If the queue already exists, its handle is returned. In SQS, queues are just URLs; consequently, a queue handle is also just a URL. Note that in the AWS SDK API, * the Queue URL is a String type and not the Java URL type * */ myQueueUrl = sqs.createQueue(createQueueRequest).getQueueUrl(); QueuePath = myQueueUrl; System.out.println(); // Send a message System.out.println("Sending a message to MyQueue.\n"); sqs.sendMessage(new SendMessageRequest(myQueueUrl, data.toString())); return sqs; } 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()); } return sqs; }
From source file:SimpleQueueService.java
License:Open Source License
public static String show() { // it will view the task Queue messages AmazonSQS sqs = new AmazonSQSClient(new ClasspathPropertiesFileCredentialsProvider()); CreateQueueRequest createQueueRequest = new CreateQueueRequest("taskQueue"); String myQueueUrl = sqs.createQueue(createQueueRequest).getQueueUrl(); String taskQueue = null;/*from w ww. ja v a 2 s . co m*/ boolean flag = true; while (flag) { List<Message> msgs = sqs .receiveMessage(new ReceiveMessageRequest(myQueueUrl).withMaxNumberOfMessages(1)).getMessages(); System.out.println(msgs.size()); if (msgs.size() > 0) { Message message = msgs.get(0); System.out.println("The message is " + message.getBody()); taskQueue = message.getBody(); sqs.deleteMessage(new DeleteMessageRequest(myQueueUrl, message.getReceiptHandle())); } else { flag = false; // System.out.println("nothing found, trying again in 30 seconds"); } System.out.println(); } System.out.println(taskQueue); return taskQueue; }
From source file:SimpleQueueService.java
License:Open Source License
public static String executeRemoteWorkers(int nOfTaskToExecute) { // gets the task Queue from SQS and read each message and returns the messgae bode AmazonSQS sqs = new AmazonSQSClient(new ClasspathPropertiesFileCredentialsProvider()); CreateQueueRequest createQueueRequest = new CreateQueueRequest("taskQueue"); String myQueueUrl = sqs.createQueue(createQueueRequest).getQueueUrl(); String taskQueue = null;//from w w w .j a v a 2s. c o m Boolean flag = true; //while(flag){ List<Message> msgs = sqs.receiveMessage(new ReceiveMessageRequest(myQueueUrl).withMaxNumberOfMessages(1)) .getMessages(); System.out.println(msgs.size()); if (msgs.size() > 0) { flag = false; Message message = msgs.get(0); System.out.println("The message is " + message.getBody()); taskQueue = message.getBody(); sqs.deleteMessage(new DeleteMessageRequest(myQueueUrl, message.getReceiptHandle())); //RemoteWorkers.ProcessTask(taskQueue); } else { taskQueue = ""; } System.out.println(); //} return taskQueue; }
From source file:SimpleQueueService.java
License:Open Source License
public static int ResultQueue() { // gets the result Queue from SQS and get the messages size and returns the size AmazonSQS sqs = null; sqs = new AmazonSQSClient(new ClasspathPropertiesFileCredentialsProvider()); CreateQueueRequest createQueueRequest = new CreateQueueRequest("resultQueue"); String myQueueUrl = sqs.createQueue(createQueueRequest).getQueueUrl(); String taskQueue = null;//from w w w . j av a 2 s . c o m boolean flag = true; while (flag) { List<Message> msgs = sqs.receiveMessage(new ReceiveMessageRequest(myQueueUrl)).getMessages(); System.out.println("Result Queue Size " + msgs.size()); int size = msgs.size(); if (size > 0) { flag = false; return msgs.size(); } else { System.out.println("trying after 10 min"); try { Thread.currentThread().sleep(10000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } return 0; }
From source file:AwsSQSDemo.java
License:Open Source License
public static void main(String[] args) throws Exception { /*/*from ww w . j a v a 2 s . 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); } AmazonSQS sqs = new AmazonSQSClient(credentials); 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"); try { // Create a queue System.out.println("Creating a new SQS queue called MyQueue.\n"); CreateQueueRequest createQueueRequest = new CreateQueueRequest("MyQueue"); String 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(); // Send a message System.out.println("Sending a message to MyQueue.\n"); sqs.sendMessage(new SendMessageRequest(myQueueUrl, "This is my message text.")); // Receive messages System.out.println("Receiving messages from MyQueue.\n"); ReceiveMessageRequest receiveMessageRequest = new ReceiveMessageRequest(myQueueUrl); List<Message> messages = sqs.receiveMessage(receiveMessageRequest).getMessages(); for (Message message : messages) { System.out.println(" Message"); System.out.println(" MessageId: " + message.getMessageId()); System.out.println(" ReceiptHandle: " + message.getReceiptHandle()); System.out.println(" MD5OfBody: " + message.getMD5OfBody()); System.out.println(" Body: " + message.getBody()); for (Entry<String, String> entry : message.getAttributes().entrySet()) { System.out.println(" Attribute"); System.out.println(" Name: " + entry.getKey()); System.out.println(" Value: " + entry.getValue()); } } System.out.println(); // Delete a message System.out.println("Deleting a message.\n"); String messageRecieptHandle = messages.get(0).getReceiptHandle(); sqs.deleteMessage(new DeleteMessageRequest(myQueueUrl, messageRecieptHandle)); // Delete a queue System.out.println("Deleting the test queue.\n"); 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()); } }
From source file:SimpleQueueServiceSample.java
License:Open Source License
public static void main(String[] args) throws Exception { /*//from www. j a v a 2 s . com * The ProfileCredentialsProvider will return your [default] * credential profile by reading from the credentials file located at * (/Users/zunzunwang/.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. " + "Please make sure that your credentials file is at the correct " + "location (/Users/zunzunwang/.aws/credentials), and is in valid format.", e); } AmazonSQS sqs = new AmazonSQSClient(credentials); Region usWest2 = Region.getRegion(Regions.EU_CENTRAL_1); sqs.setRegion(usWest2); System.out.println("==========================================="); System.out.println("Getting Started with Amazon SQS"); System.out.println("===========================================\n"); try { // Create a queue System.out.println("Creating a new SQS queue called MyQueue.\n"); CreateQueueRequest createQueueRequest = new CreateQueueRequest("MyQueue"); String 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(); // Send a message System.out.println("Sending a message to MyQueue.\n"); sqs.sendMessage(new SendMessageRequest(myQueueUrl, "This is my message text.")); // Receive messages System.out.println("Receiving messages from MyQueue.\n"); ReceiveMessageRequest receiveMessageRequest = new ReceiveMessageRequest(myQueueUrl); List<Message> messages = sqs.receiveMessage(receiveMessageRequest).getMessages(); for (Message message : messages) { System.out.println(" Message"); System.out.println(" MessageId: " + message.getMessageId()); System.out.println(" ReceiptHandle: " + message.getReceiptHandle()); System.out.println(" MD5OfBody: " + message.getMD5OfBody()); System.out.println(" Body: " + message.getBody()); for (Entry<String, String> entry : message.getAttributes().entrySet()) { System.out.println(" Attribute"); System.out.println(" Name: " + entry.getKey()); System.out.println(" Value: " + entry.getValue()); } } System.out.println(); // Delete a message System.out.println("Deleting a message.\n"); String messageReceiptHandle = messages.get(10).getReceiptHandle(); sqs.deleteMessage(new DeleteMessageRequest(myQueueUrl, messageReceiptHandle)); // Delete a queue System.out.println("Deleting the test queue.\n"); 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()); } }
From source file:TweetSQS.java
License:Open Source License
public static void main(String[] args) throws Exception { /*//from w w w .j a va2s .c om * The ProfileCredentialsProvider will return your [default] * credential profile by reading from the credentials file located at * (). */ AWSCredentials credentials = null; try { credentials = new PropertiesCredentials( TweetSQS.class.getResourceAsStream("AwsCredentials.properties")); } 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/aohong/.aws/credentials), and is in valid format.", e); } AmazonSQS sqs = new AmazonSQSClient(credentials); Region usEast1 = Region.getRegion(Regions.US_EAST_1); sqs.setRegion(usEast1); System.out.println("==========================================="); System.out.println("Getting Started with Amazon SQS"); System.out.println("===========================================\n"); try { // Create a queue System.out.println("Creating a new SQS queue called MyQueue.\n"); CreateQueueRequest createQueueRequest = new CreateQueueRequest("MyQueue"); String 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(); // Send a message System.out.println("Sending a message to MyQueue.\n"); sqs.sendMessage(new SendMessageRequest(myQueueUrl, "This is my first message text.")); // Receive messages System.out.println("Receiving messages from MyQueue.\n"); ReceiveMessageRequest receiveMessageRequest = new ReceiveMessageRequest(myQueueUrl); List<Message> messages = sqs.receiveMessage(receiveMessageRequest).getMessages(); for (Message message : messages) { System.out.println(" Message"); System.out.println(" MessageId: " + message.getMessageId()); System.out.println(" ReceiptHandle: " + message.getReceiptHandle()); System.out.println(" MD5OfBody: " + message.getMD5OfBody()); System.out.println(" Body: " + message.getBody()); for (Entry<String, String> entry : message.getAttributes().entrySet()) { System.out.println(" Attribute"); System.out.println(" Name: " + entry.getKey()); System.out.println(" Value: " + entry.getValue()); } } System.out.println(); // Delete a message System.out.println("Deleting a message.\n"); String messageRecieptHandle = messages.get(0).getReceiptHandle(); sqs.deleteMessage(new DeleteMessageRequest(myQueueUrl, messageRecieptHandle)); // Delete a queue System.out.println("Deleting the test queue.\n"); 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()); } }
From source file:aws.example.sqs.DeadLetterQueues.java
License:Open Source License
public static void main(String[] args) { if (args.length != 2) { System.out.println("Usage: DeadLetterQueues <src_queue_name> <dl_queue_name>"); System.exit(1);//from www .j a va 2s. c om } String src_queue_name = args[0]; String dl_queue_name = args[1]; final AmazonSQS sqs = AmazonSQSClientBuilder.defaultClient(); // Create source queue try { sqs.createQueue(src_queue_name); } catch (AmazonSQSException e) { if (!e.getErrorCode().equals("QueueAlreadyExists")) { throw e; } } // Create dead-letter queue try { sqs.createQueue(dl_queue_name); } catch (AmazonSQSException e) { if (!e.getErrorCode().equals("QueueAlreadyExists")) { throw e; } } // Get dead-letter queue ARN String dl_queue_url = sqs.getQueueUrl(dl_queue_name).getQueueUrl(); GetQueueAttributesResult queue_attrs = sqs .getQueueAttributes(new GetQueueAttributesRequest(dl_queue_url).withAttributeNames("QueueArn")); String dl_queue_arn = queue_attrs.getAttributes().get("QueueArn"); // Set dead letter queue with redrive policy on source queue. String src_queue_url = sqs.getQueueUrl(src_queue_name).getQueueUrl(); SetQueueAttributesRequest request = new SetQueueAttributesRequest().withQueueUrl(src_queue_url) .addAttributesEntry("RedrivePolicy", "{\"maxReceiveCount\":\"5\", \"deadLetterTargetArn\":\"" + dl_queue_arn + "\"}"); sqs.setQueueAttributes(request); }
From source file:aws.example.sqs.LongPolling.java
License:Open Source License
public static void main(String[] args) { final String USAGE = "To run this example, supply the name of a queue to create and\n" + "queue url of an existing queue.\n\n" + "Ex: LongPolling <unique-queue-name> <existing-queue-url>\n"; if (args.length != 2) { System.out.println(USAGE); System.exit(1);//from w w w .java 2 s . com } String queue_name = args[0]; String queue_url = args[1]; final AmazonSQS sqs = AmazonSQSClientBuilder.defaultClient(); // Enable long polling when creating a queue CreateQueueRequest create_request = new CreateQueueRequest().withQueueName(queue_name) .addAttributesEntry("ReceiveMessageWaitTimeSeconds", "20"); try { sqs.createQueue(create_request); } catch (AmazonSQSException e) { if (!e.getErrorCode().equals("QueueAlreadyExists")) { throw e; } } // Enable long polling on an existing queue SetQueueAttributesRequest set_attrs_request = new SetQueueAttributesRequest().withQueueUrl(queue_url) .addAttributesEntry("ReceiveMessageWaitTimeSeconds", "20"); sqs.setQueueAttributes(set_attrs_request); // Enable long polling on a message receipt ReceiveMessageRequest receive_request = new ReceiveMessageRequest().withQueueUrl(queue_url) .withWaitTimeSeconds(20); sqs.receiveMessage(receive_request); }
From source file:aws.example.sqs.SendReceiveMessages.java
License:Open Source License
public static void main(String[] args) { final AmazonSQS sqs = AmazonSQSClientBuilder.defaultClient(); try {/* www. j a va 2s. co m*/ CreateQueueResult create_result = sqs.createQueue(QUEUE_NAME); } catch (AmazonSQSException e) { if (!e.getErrorCode().equals("QueueAlreadyExists")) { throw e; } } String queueUrl = sqs.getQueueUrl(QUEUE_NAME).getQueueUrl(); SendMessageRequest send_msg_request = new SendMessageRequest().withQueueUrl(queueUrl) .withMessageBody("hello world").withDelaySeconds(5); sqs.sendMessage(send_msg_request); // Send multiple messages to the queue SendMessageBatchRequest send_batch_request = new SendMessageBatchRequest().withQueueUrl(queueUrl) .withEntries(new SendMessageBatchRequestEntry("msg_1", "Hello from message 1"), new SendMessageBatchRequestEntry("msg_2", "Hello from message 2").withDelaySeconds(10)); sqs.sendMessageBatch(send_batch_request); // receive messages from the queue List<Message> messages = sqs.receiveMessage(queueUrl).getMessages(); // delete messages from the queue for (Message m : messages) { sqs.deleteMessage(queueUrl, m.getReceiptHandle()); } }