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:shnakkydoodle.queueing.provider.aws.AwsProvider.java

License:Open Source License

/**
 * Dequeue all messages//from ww w .  j  av  a  2  s . c o  m
 * 
 * @param queuename
 * @param message
 * @return all the messages
 */
@Override
public ArrayList<String> dequeueAll(String queueName) throws Exception {

    ArrayList<String> data = new ArrayList<String>();

    AmazonSQS sqs = new AmazonSQSClient(credentials);
    sqs.setRegion(Region.EU_Ireland.toAWSRegion());
    sqs.createQueue(queueName);
    CreateQueueRequest createQueueRequest = new CreateQueueRequest(queueName);
    String queueUrl = sqs.createQueue(createQueueRequest).getQueueUrl();
    ReceiveMessageRequest receiveMessageRequest = new ReceiveMessageRequest(queueUrl);
    for (Message message : sqs.receiveMessage(receiveMessageRequest).getMessages()) {
        data.add(message.getBody());
        sqs.deleteMessage(new DeleteMessageRequest(queueUrl, message.getReceiptHandle()));
    }
    return data;
}

From source file:smartthings.brave.sqs.AmazonSQSRule.java

License:Apache License

public AmazonSQSRule start(int httpPort) {
    if (server == null) {
        server = SQSRestServerBuilder.withPort(httpPort).withSQSLimits(SQSLimits.Strict()).start();
        server.waitUntilStarted();//from  w  ww.ja  v  a2s.co m
    }

    if (client == null) {
        client = new AmazonSQSClient(new BasicAWSCredentials("x", "x"));
        client.setEndpoint(String.format("http://localhost:%d", httpPort));
        queueUrl = client.createQueue("test").getQueueUrl();
    }
    return this;
}

From source file:sqs_examples.SimpleQueueServiceSample.java

License:Open Source License

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

    /*/*from ww  w. j  av  a2 s  .com*/
     * The ProfileCredentialsProvider will return your [default]
     * credential profile by reading from the credentials file located at
     * (/Users/pmacharl/.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/pmacharl/.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 messageReceiptHandle = messages.get(0).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:test1.SimpleQueueServiceSample.java

License:Open Source License

public static AmazonSQS init() {
    AWSCredentials credentials = null;// ww  w .  jav a  2 s.c o m
    try {
        credentials = new ProfileCredentialsProvider("default").getCredentials();
    } catch (Exception e) {
        throw new AmazonClientException(e);
    }

    AmazonSQS sqs = new AmazonSQSClient(credentials);
    Region usEast1 = Region.getRegion(Regions.US_EAST_1);
    sqs.setRegion(usEast1);
    return sqs;
}

From source file:twitter.SimpleQueueServiceSample.java

License:Open Source License

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

    /*/*from   w  w w  . j  a v  a  2 s .  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);
    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();
        System.out.println("queueUrl is:" + myQueueUrl);
        // 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");
        String id = "2";
        String tweet = "I am hungry";
        String workRequest = "{" + "  \"id\": \"" + id + "\"," + "  \"tweet\": \"" + tweet + "\"" + "}";
        sqs.sendMessage(new SendMessageRequest().withQueueUrl(myQueueUrl).withMessageBody(workRequest));

        //   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();
        Thread.sleep(5000);
        String id2 = "3";
        String t = "yes,it is";
        String workRequest1 = "{" + "  \"id\": \"" + id2 + "\"," + "  \"tweet\": \"" + t + "\"" + "}";
        sqs.sendMessage(new SendMessageRequest().withQueueUrl(myQueueUrl).withMessageBody(workRequest1));
        ReceiveMessageRequest receiveMessageRequest1 = new ReceiveMessageRequest(myQueueUrl);
        List<Message> messages = sqs.receiveMessage(receiveMessageRequest1).getMessages();
        System.out.println("!!!!!" + messages.size());

        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:util.SQSManager.java

License:Open Source License

private void createQueue(String name) throws Exception {
    /*/* w  w w.  j  a  va 2  s .com*/
     * The ProfileCredentialsProvider will return your [default]
     * credential profile by reading from the credentials file located at
     * ().
     */
    AWSCredentials credentials = null;
    try {
        PropertiesManager properitesManager = PropertiesManager.getInstance();
        credentials = new BasicAWSCredentials(properitesManager.getAccessKey(),
                properitesManager.getSecretKey());
        sqs = new AmazonSQSClient(credentials);
        Region apSouthEast = Region.getRegion(Regions.fromName(properitesManager.getRegion()));//Region in Singapore
        sqs.setRegion(apSouthEast);
    } 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);
    }

    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 " + name + ".\n");
        CreateQueueRequest createQueueRequest = new CreateQueueRequest(name);
        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("Create queue successfully");
    } 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());
        throw ase;
    } 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());
        throw ace;
    }
}

From source file:zipkin.junit.aws.AmazonSQSRule.java

License:Apache License

public AmazonSQSRule start(int httpPort) {
    if (server == null) {
        server = SQSRestServerBuilder.withPort(httpPort).withSQSLimits(SQSLimits.Strict()).start();
        server.waitUntilStarted();/*from  w w w .  j  a v  a  2s .  c  o m*/
    }

    if (client == null) {
        client = new AmazonSQSClient(new BasicAWSCredentials("x", "x"));
        client.setEndpoint(String.format("http://localhost:%d", httpPort));
        queueUrl = client.createQueue("zipkin").getQueueUrl();
    }
    return this;
}