Example usage for com.amazonaws.services.sqs AmazonSQSClient AmazonSQSClient

List of usage examples for com.amazonaws.services.sqs AmazonSQSClient AmazonSQSClient

Introduction

In this page you can find the example usage for com.amazonaws.services.sqs AmazonSQSClient AmazonSQSClient.

Prototype

AmazonSQSClient(AwsSyncClientParams clientParams) 

Source Link

Document

Constructs a new client to invoke service methods on Amazon SQS using the specified parameters.

Usage

From source file:SimpleQueueServiceMultiThread.java

License:Open Source License

public static void main(String[] args) throws Exception {

    /*//  w  ww  .ja  v  a2  s.  c om
     * 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,clientConfig);
    sqs = new AmazonSQSClient(credentials);

    Region apSouthEast1 = Region.getRegion(Regions.AP_SOUTHEAST_1);
    sqs.setRegion(apSouthEast1);

    System.out.println("===========================================");
    System.out.println("Getting Started with Amazon SQS");
    System.out.println("===========================================\n");

    //Define thread pool size
    ExecutorService executor = Executors.newFixedThreadPool(poolSize);

    //create a list to hold the Future object associated with Callable
    List<Future<Long>> list = new ArrayList<Future<Long>>();

    //Create Callable instance
    Callable<Long> callable = new SimpleQueueServiceMultiThread();

    for (int i = 0; i < threadSize; i++) {

        //submit Callable tasks to be executed by thread pool
        Future<Long> future = executor.submit(callable);

        //add Future to the list, we can get return value using Future
        list.add(future);

    }

    for (Future<Long> fut : list) {

        try {
            // Future.get() waits for task to get completed
            System.out.println("Time difference : " + fut.get());

            long timeDiff = fut.get().longValue();

            totalTime = Long.valueOf(totalTime + timeDiff);

            if (timeDiff >= maxTime) {
                maxTime = timeDiff;
            }

            if (minTime == 0L && timeDiff >= minTime) {
                minTime = timeDiff;
                //}else if (timeDiff <= minTime && timeDiff != 0L) {
            } else if (timeDiff <= minTime) {
                minTime = timeDiff;
            }

        } catch (InterruptedException | ExecutionException e) {

            e.printStackTrace();

        }
    }

    if (totalTime >= 0L) {

        averageTime = totalTime / threadSize;

    }

    System.out.println("Total Time : " + Long.valueOf(totalTime).toString());
    System.out.println("Max Time : " + Long.valueOf(maxTime).toString());
    System.out.println("Min Time : " + Long.valueOf(minTime).toString());
    System.out.println("Average Time : " + Long.valueOf(averageTime).toString());

    //shut down the executor service now
    executor.shutdown();

}

From source file:SimpleQueueServiceMultiThread1.java

License:Open Source License

public static void main(String[] args) throws Exception {

    /*/*from  ww w .j a va 2s .  com*/
     * 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,clientConfig);
    sqs = new AmazonSQSClient(credentials);

    Region apSouthEast1 = Region.getRegion(Regions.AP_SOUTHEAST_1);
    sqs.setRegion(apSouthEast1);

    System.out.println("===========================================");
    System.out.println("Getting Started with Amazon SQS");
    System.out.println("===========================================\n");

    //Define thread pool size
    ExecutorService executor = Executors.newFixedThreadPool(poolSize);

    //create a list to hold the Future object associated with Callable
    List<Future<Long>> list = new ArrayList<Future<Long>>();

    //Create Callable instance
    Callable<Long> callable = new SimpleQueueServiceMultiThread1();

    for (int i = 0; i < threadSize; i++) {

        //submit Callable tasks to be executed by thread pool
        Future<Long> future = executor.submit(callable);

        //add Future to the list, we can get return value using Future
        list.add(future);

    }

    for (Future<Long> fut : list) {

        try {
            // Future.get() waits for task to get completed
            System.out.println("Time difference : " + fut.get());

            long timeDiff = fut.get().longValue();

            totalTime = Long.valueOf(totalTime + timeDiff);

            if (timeDiff >= maxTime) {
                maxTime = timeDiff;
            }

            if (minTime == 0L && timeDiff >= minTime) {
                minTime = timeDiff;
                //}else if (timeDiff <= minTime && timeDiff != 0L) {
            } else if (timeDiff <= minTime) {
                minTime = timeDiff;
            }

        } catch (InterruptedException | ExecutionException e) {

            e.printStackTrace();

        }
    }

    if (totalTime >= 0L) {

        averageTime = totalTime / threadSize;

    }

    System.out.println("Total Time : " + Long.valueOf(totalTime).toString());
    System.out.println("Max Time : " + Long.valueOf(maxTime).toString());
    System.out.println("Min Time : " + Long.valueOf(minTime).toString());
    System.out.println("Average Time : " + Long.valueOf(averageTime).toString());

    //shut down the executor service now
    executor.shutdown();

}

From source file:SimpleQueueServiceMultiThread4.java

License:Open Source License

public static void main(String[] args) throws Exception {

    /*/*from   w w  w .  j a v a  2  s .  co  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,clientConfig);
    sqs = new AmazonSQSClient(credentials);

    Region apSouthEast1 = Region.getRegion(Regions.AP_SOUTHEAST_1);
    sqs.setRegion(apSouthEast1);

    System.out.println("===========================================");
    System.out.println("Getting Started with Amazon SQS");
    System.out.println("===========================================\n");

    //Define thread pool size
    ExecutorService executor = Executors.newFixedThreadPool(poolSize);

    //create a list to hold the Future object associated with Callable
    List<Future<Long>> list = new ArrayList<Future<Long>>();

    //Create Callable instance
    Callable<Long> callable = new SimpleQueueServiceMultiThread4();

    for (int i = 0; i < threadSize; i++) {

        //submit Callable tasks to be executed by thread pool
        Future<Long> future = executor.submit(callable);

        //add Future to the list, we can get return value using Future
        list.add(future);

    }

    for (Future<Long> fut : list) {

        try {
            // Future.get() waits for task to get completed
            System.out.println("Time difference : " + fut.get());

            long timeDiff = fut.get().longValue();

            totalTime = Long.valueOf(totalTime + timeDiff);

            if (timeDiff >= maxTime) {
                maxTime = timeDiff;
            }

            if (minTime == 0L && timeDiff >= minTime) {
                minTime = timeDiff;
                //}else if (timeDiff <= minTime && timeDiff != 0L) {
            } else if (timeDiff <= minTime) {
                minTime = timeDiff;
            }

        } catch (InterruptedException | ExecutionException e) {

            e.printStackTrace();

        }
    }

    if (totalTime >= 0L) {

        averageTime = totalTime / threadSize;

    }

    System.out.println("Total Time : " + Long.valueOf(totalTime).toString());
    System.out.println("Max Time : " + Long.valueOf(maxTime).toString());
    System.out.println("Min Time : " + Long.valueOf(minTime).toString());
    System.out.println("Average Time : " + Long.valueOf(averageTime).toString());

    //shut down the executor service now
    executor.shutdown();

}

From source file:TweetSQS.java

License:Open Source License

public static void main(String[] args) throws Exception {

    /*//from  w w  w .  j  a  v a 2s .c  o m
     * 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:SimpleQueueServiceMultiThread2.java

License:Open Source License

public static void main(String[] args) throws Exception {

    /*//from   www  .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);
    }
    //AmazonSQS sqs = new AmazonSQSClient(credentials,clientConfig);
    sqs = new AmazonSQSClient(credentials);

    Region apSouthEast1 = Region.getRegion(Regions.AP_SOUTHEAST_1);
    sqs.setRegion(apSouthEast1);

    System.out.println("===========================================");
    System.out.println("Getting Started with Amazon SQS");
    System.out.println("===========================================\n");

    //Define thread pool size
    ExecutorService executor = Executors.newFixedThreadPool(poolSize);

    //create a list to hold the Future object associated with Callable
    List<Future<Long>> list = new ArrayList<Future<Long>>();

    //Create Callable instance
    Callable<Long> callable = new SimpleQueueServiceMultiThread2();

    for (int i = 0; i < threadSize; i++) {

        //submit Callable tasks to be executed by thread pool
        Future<Long> future = executor.submit(callable);

        //add Future to the list, we can get return value using Future
        list.add(future);

    }

    for (Future<Long> fut : list) {

        try {
            // Future.get() waits for task to get completed
            System.out.println("Time difference : " + fut.get());

            long timeDiff = fut.get().longValue();

            totalTime = Long.valueOf(totalTime + timeDiff);

            if (timeDiff >= maxTime) {
                maxTime = timeDiff;
            }

            if (minTime == 0L && timeDiff >= minTime) {
                minTime = timeDiff;
                //}else if (timeDiff <= minTime && timeDiff != 0L) {
            } else if (timeDiff <= minTime) {
                minTime = timeDiff;
            }

        } catch (InterruptedException | ExecutionException e) {

            e.printStackTrace();

        }
    }

    if (totalTime >= 0L) {

        averageTime = totalTime / threadSize;

    }

    System.out.println("Total Time : " + Long.valueOf(totalTime).toString());
    System.out.println("Max Time : " + Long.valueOf(maxTime).toString());
    System.out.println("Min Time : " + Long.valueOf(minTime).toString());
    System.out.println("Average Time : " + Long.valueOf(averageTime).toString());

    //shut down the executor service now
    executor.shutdown();

}

From source file:SQS.java

License:Open Source License

public SQS(PropertiesCredentials p) {
    AWSCredentials credentials = p;//from   w w  w.  j a v a  2  s  .  c o  m
    sqs = new AmazonSQSClient(credentials);
    Region usEast1 = Region.getRegion(Regions.US_EAST_1);
    sqs.setRegion(usEast1);
    //System.out.println("Finish initializing sqs service");
}

From source file:SimpleQueueServiceMultiThread3.java

License:Open Source License

public static void main(String[] args) throws Exception {

    /*//from  w w  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,clientConfig);
    sqs = new AmazonSQSClient(credentials);

    Region apSouthEast1 = Region.getRegion(Regions.AP_SOUTHEAST_1);
    sqs.setRegion(apSouthEast1);

    System.out.println("===========================================");
    System.out.println("Getting Started with Amazon SQS");
    System.out.println("===========================================\n");

    //Define thread pool size
    ExecutorService executor = Executors.newFixedThreadPool(poolSize);

    //create a list to hold the Future object associated with Callable
    List<Future<Long>> list = new ArrayList<Future<Long>>();

    //Create Callable instance
    Callable<Long> callable = new SimpleQueueServiceMultiThread3();

    for (int i = 0; i < threadSize; i++) {

        //submit Callable tasks to be executed by thread pool
        Future<Long> future = executor.submit(callable);

        //add Future to the list, we can get return value using Future
        list.add(future);

    }

    for (Future<Long> fut : list) {

        try {
            // Future.get() waits for task to get completed
            System.out.println("Time difference : " + fut.get());

            long timeDiff = fut.get().longValue();

            totalTime = Long.valueOf(totalTime + timeDiff);

            if (timeDiff >= maxTime) {
                maxTime = timeDiff;
            }

            if (minTime == 0L && timeDiff >= minTime) {
                minTime = timeDiff;
                //}else if (timeDiff <= minTime && timeDiff != 0L) {
            } else if (timeDiff <= minTime) {
                minTime = timeDiff;
            }

        } catch (InterruptedException | ExecutionException e) {

            e.printStackTrace();

        }
    }

    if (totalTime >= 0L) {

        averageTime = totalTime / threadSize;

    }

    System.out.println("Total Time : " + Long.valueOf(totalTime).toString());
    System.out.println("Max Time : " + Long.valueOf(maxTime).toString());
    System.out.println("Min Time : " + Long.valueOf(minTime).toString());
    System.out.println("Average Time : " + Long.valueOf(averageTime).toString());

    //shut down the executor service now
    executor.shutdown();

}

From source file:receiveSQS.java

License:Open Source License

public static void main(String[] args) throws Exception {

    /*/*w  ww  .j  ava  2s  .  com*/
     * The ProfileCredentialsProvider will return your [default]
     * credential profile by reading from the credentials file located at
     * ().
     */

    BasicAWSCredentials credentials = new BasicAWSCredentials("AKIAI2ZCFS3NVEENXW5A",
            "tI/GgpSWDF/QrNVhtCRu1G+PX/10A2nJQH+yTOiv");
    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/daniel/.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 {

        // 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();
            // Receive messages
            System.out.println("Receiving messages from MyQueue.\n");
            ReceiveMessageRequest receiveMessageRequest = new ReceiveMessageRequest(queueUrl);
            System.out.println("Message size:");

            //*****************************************************

            //For some reason, we only get one message at a time and it does not loop over.

            //*****************************************************

            //ReceiveMessageResponse receiveMessageResponse = amazonSQSClient.ReceiveMessage(receiveMessageRequest);
            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());
                String[] tweetdata = message.getBody().split("\\|\\|");
                System.out.println(Arrays.toString(tweetdata));
                System.out.println("Deleting a message.\n");
                String messageRecieptHandle = messages.get(0).getReceiptHandle();
                sqs.deleteMessage(new DeleteMessageRequest(queueUrl, messageRecieptHandle));

            }
        }

        System.out.println();
        /*
                    // Delete a message
                
                    // 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:app.SQSMessage.java

License:Open Source License

public void sendmsg(Tweet t, String keyword) throws Exception {

    /*//from   www  .j a va2 s  .c o  m
     * The ProfileCredentialsProvider will return your [default]
     * credential profile by reading from the credentials file located at
     * ().
     */
    credentials = new ProfileCredentialsProvider("default").getCredentials();
    AmazonSQS sqs = new AmazonSQSClient(credentials);
    Region usWest2 = Region.getRegion(Regions.US_EAST_1);
    sqs.setRegion(usWest2);

    System.out.println("===========================================");
    System.out.println("sending tweet to Queue");
    System.out.println("===========================================\n");

    try {

        // Send a message
        System.out.println("Sending a message to TweetQueue.\n");

        JSONObject obj = new JSONObject();

        obj.put("userId", t.getUserId() + "");
        obj.put("lng", t.getLongitude() + "");
        obj.put("lat", t.getLatitude() + "");
        obj.put("text", t.getText());
        // obj.put("time", t.getCreatedTime().toString());
        obj.put("kwd", keyword);

        //String TwtJson = "{\"userId\":\"" + t.getUserId() + "\",\"lng\":\"" + t.getLongitude() + "\",\"lat\":\"" + t.getLatitude() + "\",\"text\":\"" + JSONObject.escape(t.getText()) +  "\",\"time\":\"" + t.getCreatedTime() + "\",\"kwd\":\"" + keyword + "\"}";

        String TwtJson = obj.toString();

        System.out.println(TwtJson);
        sqs.sendMessage(new SendMessageRequest(q_url, TwtJson));

    } 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.sample.SimpleQueueServiceSample.java

License:Open Source License

public static void main(String[] args) throws Exception {
    /*/*ww w.ja  v  a 2s . com*/
     * 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 PropertiesCredentials(
            SimpleQueueServiceSample.class.getResourceAsStream("/AwsCredentials.properties")));

    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());
    }
}