Example usage for com.amazonaws.services.sqs.model GetQueueAttributesRequest GetQueueAttributesRequest

List of usage examples for com.amazonaws.services.sqs.model GetQueueAttributesRequest GetQueueAttributesRequest

Introduction

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

Prototype

public GetQueueAttributesRequest(String queueUrl) 

Source Link

Document

Constructs a new GetQueueAttributesRequest object.

Usage

From source file:com.pocketdealhunter.HotDealsMessagesUtil.java

License:Open Source License

private String getQueueArn(String queueUrl) {
    try {//from  w w w  .j  av  a  2s  .c o  m
        GetQueueAttributesRequest gqar = new GetQueueAttributesRequest(queueUrl)
                .withAttributeNames(new String[] { "QueueArn" });
        GetQueueAttributesResult result = this.sqsClient.getQueueAttributes(gqar);

        return (String) result.getAttributes().get("QueueArn");
    } catch (Exception exception) {
        System.out.println("Exception = " + exception);
        return null;
    }
}

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

License:Open Source License

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

    /*//w w w  . j a  v a  2s  .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);
    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());
    }
}

From source file:com.smoketurner.pipeline.application.core.AmazonSQSIterator.java

License:Apache License

/**
 * Return the approximate number of visible messages in an SQS queue.
 *
 * @param client/* ww  w.  j  a va 2 s.  c  om*/
 *            SQS client
 * @param queueUrl
 *            Queue URL
 * @return approximate number of visible messages
 */
private int getNumMessages() {
    try {
        final GetQueueAttributesResult result = client.getQueueAttributes(
                new GetQueueAttributesRequest(queueUrl).withAttributeNames(NUM_MESSAGES_KEY));
        final int count = Integer.parseInt(result.getAttributes().getOrDefault(NUM_MESSAGES_KEY, "0"));
        LOGGER.info("Approximately {} messages in queue", count);
        return count;
    } catch (Exception e) {
        LOGGER.error("Unable to get approximate number of messages", e);
    }
    return 0;
}

From source file:org.mule.modules.sqs.SQSConnector.java

License:Open Source License

/**
 * Gets queue attributes. This is provided to expose the underlying functionality.
 * Currently supported attributes are;/* ww  w. j  a  v a  2 s .com*/
 * ApproximateNumberOfMessages
 * CreatedTimestamp
 * LastModifiedTimestamp
 * VisibilityTimeout
 * RequestPayer
 * Policy
 * <p/>
 * {@sample.xml ../../../doc/mule-module-sqs.xml.sample sqs:get-queue-attributes}
 *
 * @param attribute Attribute to get
 * @return a map of attributes and their values
 * @throws AmazonClientException
 *             If any internal errors are encountered inside the client while
 *             attempting to make the request or handle the response.  For example
 *             if a network connection is not available.
 * @throws AmazonServiceException
 *             If an error response is returned by AmazonSQS indicating
 *             either a problem with the data in the request, or a server side issue.
 */
@Processor
@InvalidateConnectionOn(exception = AmazonClientException.class)
public Map<String, String> getQueueAttributes(String attribute) throws AmazonServiceException {
    return msgQueue
            .getQueueAttributes(new GetQueueAttributesRequest(getQueueUrl()).withAttributeNames(attribute))
            .getAttributes();
}

From source file:org.mule.modules.sqs.SQSConnector.java

License:Open Source License

/**
 * Gets the visibility timeout for the queue.
 * <p/>//  ww w .j  a va  2 s.  co m
 * {@sample.xml ../../../doc/mule-module-sqs.xml.sample sqs:get-approximate-number-of-messages}
 *
 * @throws AmazonClientException
 *             If any internal errors are encountered inside the client while
 *             attempting to make the request or handle the response.  For example
 *             if a network connection is not available.
 * @throws AmazonServiceException
 *             If an error response is returned by AmazonSQS indicating
 *             either a problem with the data in the request, or a server side issue.
 * @return the approximate number of messages in the queue
 */
@Processor
@InvalidateConnectionOn(exception = AmazonClientException.class)
public int getApproximateNumberOfMessages() throws AmazonServiceException {
    return Integer.parseInt(msgQueue
            .getQueueAttributes(new GetQueueAttributesRequest(getQueueUrl())
                    .withAttributeNames("ApproximateNumberOfMessages"))
            .getAttributes().get("ApproximateNumberOfMessages"));
}

From source file:org.springframework.cloud.aws.messaging.listener.AbstractMessageListenerContainer.java

License:Apache License

private QueueAttributes queueAttributes(String queue, SqsMessageDeletionPolicy deletionPolicy) {
    String destinationUrl;/*from w ww .  j a  v  a 2s  . c  o  m*/
    try {
        destinationUrl = getDestinationResolver().resolveDestination(queue);
    } catch (DestinationResolutionException e) {
        getLogger().warn(String.format("The queue with name '%s' does not exist.", queue), e);
        return null;
    }

    ReceiveMessageRequest receiveMessageRequest = new ReceiveMessageRequest(destinationUrl)
            .withAttributeNames(RECEIVING_ATTRIBUTES).withMessageAttributeNames(RECEIVING_MESSAGE_ATTRIBUTES);
    if (getMaxNumberOfMessages() != null) {
        receiveMessageRequest.withMaxNumberOfMessages(getMaxNumberOfMessages());
    } else {
        receiveMessageRequest.withMaxNumberOfMessages(DEFAULT_MAX_NUMBER_OF_MESSAGES);
    }

    if (getVisibilityTimeout() != null) {
        receiveMessageRequest.withVisibilityTimeout(getVisibilityTimeout());
    }

    if (getWaitTimeOut() != null) {
        receiveMessageRequest.setWaitTimeSeconds(getWaitTimeOut());
    }

    GetQueueAttributesResult queueAttributes = getAmazonSqs().getQueueAttributes(
            new GetQueueAttributesRequest(destinationUrl).withAttributeNames(QueueAttributeName.RedrivePolicy));
    boolean hasRedrivePolicy = queueAttributes.getAttributes()
            .containsKey(QueueAttributeName.RedrivePolicy.toString());

    return new QueueAttributes(receiveMessageRequest, hasRedrivePolicy, deletionPolicy);
}

From source file:scheduler.SQSService.java

License:Apache License

public int getQueueSize() {
    HashMap<String, String> attributes;

    Collection<String> attributeNames = new ArrayList<String>();
    attributeNames.add("ApproximateNumberOfMessages");

    GetQueueAttributesRequest getAttributesRequest = new GetQueueAttributesRequest(queueUrl)
            .withAttributeNames(attributeNames);
    attributes = (HashMap<String, String>) sqs.getQueueAttributes(getAttributesRequest).getAttributes();

    return Integer.valueOf(attributes.get("ApproximateNumberOfMessages"));

}