Example usage for com.amazonaws.services.sqs AmazonSQS getQueueAttributes

List of usage examples for com.amazonaws.services.sqs AmazonSQS getQueueAttributes

Introduction

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

Prototype

GetQueueAttributesResult getQueueAttributes(GetQueueAttributesRequest getQueueAttributesRequest);

Source Link

Document

Gets attributes for the specified queue.

Usage

From source file:com.easarrive.aws.plugins.common.service.impl.SQSService.java

License:Open Source License

@Override
public Long getQueueApproximateNumberOfMessages(AmazonSQS client, String queueUrl) {
    GetQueueAttributesRequest getQueueAttributesRequest = new GetQueueAttributesRequest(queueUrl);
    getQueueAttributesRequest.withAttributeNames(Constant.SQS.Attribute.APPROXIMATE_NUMBER_OF_MESSAGES);
    GetQueueAttributesResult result = client.getQueueAttributes(getQueueAttributesRequest);
    if (result == null) {
        return null;
    }/*from   w  ww.j a  v  a2s  .  com*/
    Map<String, String> attributeMap = result.getAttributes();
    if (attributeMap == null) {
        return null;
    }
    String value = attributeMap.get(Constant.SQS.Attribute.APPROXIMATE_NUMBER_OF_MESSAGES);
    if (StringUtil.isEmpty(value)) {
        return null;
    }
    if (!value.matches("^\\d+$")) {
        return null;
    }
    return Long.valueOf(value);
}

From source file:com.easarrive.aws.plugins.common.service.impl.SQSService.java

License:Open Source License

@Override
public Long getQueueApproximateNumberOfMessagesNotVisible(AmazonSQS client, String queueUrl) {
    GetQueueAttributesRequest getQueueAttributesRequest = new GetQueueAttributesRequest(queueUrl);
    getQueueAttributesRequest//from  w  ww  .  ja  va2 s.c  om
            .withAttributeNames(Constant.SQS.Attribute.APPROXIMATE_NUMBER_OF_MESSAGES_NOT_VISIBLE);
    GetQueueAttributesResult result = client.getQueueAttributes(getQueueAttributesRequest);
    if (result == null) {
        return null;
    }
    Map<String, String> attributeMap = result.getAttributes();
    if (attributeMap == null) {
        return null;
    }
    String value = attributeMap.get(Constant.SQS.Attribute.APPROXIMATE_NUMBER_OF_MESSAGES_NOT_VISIBLE);
    if (StringUtil.isEmpty(value)) {
        return null;
    }
    if (!value.matches("^\\d+$")) {
        return null;
    }
    return Long.valueOf(value);
}

From source file:com.easarrive.aws.plugins.common.service.impl.SQSService.java

License:Open Source License

@Override
public String getQueueQueueArn(AmazonSQS client, String queueUrl) {
    GetQueueAttributesRequest getQueueAttributesRequest = new GetQueueAttributesRequest(queueUrl);
    getQueueAttributesRequest.withAttributeNames(Constant.SQS.Attribute.QUEUE_ARN);
    GetQueueAttributesResult result = client.getQueueAttributes(getQueueAttributesRequest);
    if (result == null) {
        return null;
    }//w  w  w  . jav  a  2 s.c  o  m
    Map<String, String> attributeMap = result.getAttributes();
    if (attributeMap == null) {
        return null;
    }
    String value = attributeMap.get(Constant.SQS.Attribute.QUEUE_ARN);
    return value;
}

From source file:com.easarrive.aws.plugins.common.service.impl.SQSService.java

License:Open Source License

@Override
public Long getQueueApproximateNumberOfMessagesDelayed(AmazonSQS client, String queueUrl) {
    GetQueueAttributesRequest getQueueAttributesRequest = new GetQueueAttributesRequest(queueUrl);
    getQueueAttributesRequest.withAttributeNames(Constant.SQS.Attribute.APPROXIMATE_NUMBER_OF_MESSAGES_DELAYED);
    GetQueueAttributesResult result = client.getQueueAttributes(getQueueAttributesRequest);
    if (result == null) {
        return null;
    }//from   ww  w .ja va 2 s  . c o m
    Map<String, String> attributeMap = result.getAttributes();
    if (attributeMap == null) {
        return null;
    }
    String value = attributeMap.get(Constant.SQS.Attribute.APPROXIMATE_NUMBER_OF_MESSAGES_DELAYED);
    if (StringUtil.isEmpty(value)) {
        return null;
    }
    if (!value.matches("^\\d+$")) {
        return null;
    }
    return Long.valueOf(value);
}

From source file:com.rodosaenz.samples.aws.sqs.AwsSqsSimpleExample.java

License:Open Source License

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

    /*/*from www.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_EAST_1);
    sqs.setRegion(usWest2);

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

    String queue_name = "com-rodosaenz-examples-aws-sqs";

    try {
        // Create a queue
        System.out.println("Creating a new SQS queue called " + queue_name + ".\n");
        CreateQueueRequest createQueueRequest = new CreateQueueRequest(queue_name);
        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 " + queue_name + ".\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);
        receiveMessageRequest.setMaxNumberOfMessages(1);
        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));

        //Get attributes
        GetQueueAttributesRequest request = new GetQueueAttributesRequest(myQueueUrl).withAttributeNames("All");
        final Map<String, String> attributes = sqs.getQueueAttributes(request).getAttributes();

        System.out.println("  Policy: " + attributes.get("Policy"));
        System.out.println("  MessageRetentionPeriod: " + attributes.get("MessageRetentionPeriod"));
        System.out.println("  MaximumMessageSize: " + attributes.get("MaximumMessageSize"));
        System.out.println("  CreatedTimestamp: " + attributes.get("CreatedTimestamp"));
        System.out.println("  VisibilityTimeout: " + attributes.get("VisibilityTimeout"));
        System.out.println("  QueueArn: " + attributes.get("QueueArn"));
        System.out.println("  ApproximateNumberOfMessages: " + attributes.get("ApproximateNumberOfMessages"));
        System.out.println("  ApproximateNumberOfMessagesNotVisible: "
                + attributes.get("ApproximateNumberOfMessagesNotVisible"));
        System.out.println("  DelaySeconds: " + attributes.get("DelaySeconds"));

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