Example usage for com.amazonaws.services.sqs.model GetQueueAttributesResult getAttributes

List of usage examples for com.amazonaws.services.sqs.model GetQueueAttributesResult getAttributes

Introduction

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

Prototype


public java.util.Map<String, String> getAttributes() 

Source Link

Document

A map of attributes to their respective values.

Usage

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 www. j a v a 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.leverno.ysbos.archive.example.AmazonGlacierDownloadInventoryWithSQSPolling.java

License:Open Source License

private static void setupSQS() {
    CreateQueueRequest request = new CreateQueueRequest().withQueueName(sqsQueueName);
    CreateQueueResult result = sqsClient.createQueue(request);
    sqsQueueURL = result.getQueueUrl();// w ww. j  av  a2  s  . c o  m

    GetQueueAttributesRequest qRequest = new GetQueueAttributesRequest().withQueueUrl(sqsQueueURL)
            .withAttributeNames("QueueArn");

    GetQueueAttributesResult qResult = sqsClient.getQueueAttributes(qRequest);
    sqsQueueARN = qResult.getAttributes().get("QueueArn");

    Policy sqsPolicy = new Policy()
            .withStatements(new Statement(Effect.Allow).withPrincipals(Principal.AllUsers)
                    .withActions(SQSActions.SendMessage).withResources(new Resource(sqsQueueARN)));
    Map<String, String> queueAttributes = new HashMap<String, String>();
    queueAttributes.put("Policy", sqsPolicy.toJson());
    sqsClient.setQueueAttributes(new SetQueueAttributesRequest(sqsQueueURL, queueAttributes));

}

From source file:com.netflix.conductor.contribs.queue.sqs.SQSObservableQueue.java

License:Apache License

@Override
public long size() {
    GetQueueAttributesResult attributes = client.getQueueAttributes(queueURL,
            Arrays.asList("ApproximateNumberOfMessages"));
    String sizeAsStr = attributes.getAttributes().get("ApproximateNumberOfMessages");
    try {/*  ww w.ja va  2 s  .  c  om*/
        return Long.parseLong(sizeAsStr);
    } catch (Exception e) {
        return -1;
    }
}

From source file:com.netflix.conductor.contribs.queue.sqs.SQSObservableQueue.java

License:Apache License

String getQueueARN() {
    GetQueueAttributesResult response = client.getQueueAttributes(queueURL, Arrays.asList("QueueArn"));
    return response.getAttributes().get("QueueArn");
}

From source file:com.pocketdealhunter.HotDealsMessagesUtil.java

License:Open Source License

private String getQueueArn(String queueUrl) {
    try {//from   w ww .  ja  va  2 s.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.smoketurner.pipeline.application.core.AmazonSQSIterator.java

License:Apache License

/**
 * Return the approximate number of visible messages in an SQS queue.
 *
 * @param client//from   w  w w  .  ja v a 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.apache.usergrid.apm.service.ApplicationServiceImpl.java

License:Apache License

/**
 * /* w  ww.  ja  va2s. c  om*/
 * @param applicationId
 */
public void createSQSQueue(String orgAppName) {
    log.info("Creating Queue for App : " + orgAppName);
    CreateQueueRequest createQueueRequest = new CreateQueueRequest();
    createQueueRequest.setQueueName(AWSUtil.formQueueName(orgAppName));

    try {
        sqsClient.createQueue(createQueueRequest);

        //Need to do this to get QueueArn to apply right policy on that

        GetQueueAttributesRequest attributesRequest = new GetQueueAttributesRequest()
                .withQueueUrl(AWSUtil.formFullQueueUrl(orgAppName)).withAttributeNames("QueueArn");

        GetQueueAttributesResult attributesResult = sqsClient.getQueueAttributes(attributesRequest);
        String queueArn = attributesResult.getAttributes().get("QueueArn");

        SetQueueAttributesRequest setQueueAttributesRequest = new SetQueueAttributesRequest();
        Map<String, String> queueAttributes = new HashMap<String, String>();
        //Increasing the max size of SQS messages.
        queueAttributes.put("MaximumMessageSize", "65536");
        //Apply IP address white list
        String sqsPolicy = AWSUtil.getSQSIPAddressWhiteListPolicy(queueArn);
        log.info("For  queue " + queueArn + " with policy  json " + sqsPolicy);
        queueAttributes.put("Policy", sqsPolicy);
        setQueueAttributesRequest.setAttributes(queueAttributes);
        setQueueAttributesRequest.setQueueUrl(AWSUtil.formFullQueueUrl(orgAppName));
        sqsClient.setQueueAttributes(setQueueAttributesRequest);

    } catch (AmazonServiceException ase) {
        log.error("Problem creating queue in sqs");
        log.error(ase);
    } catch (AmazonClientException ace) {
        log.error(ace);
    }
}

From source file:org.apache.usergrid.apm.service.ApplicationServiceImpl.java

License:Apache License

public AccessKey createAuthorizedAppPrinciple(Long applicationId, String orgAppName) {
    CreateUserRequest createUserRequest = new CreateUserRequest();

    createUserRequest.setUserName(APP_PRINCIPLE_USER_PREFIX + "_" + orgAppName);

    createUserRequest.setRequestCredentials(awsCredentials);

    try {// ww w.  j  ava  2s.  c  o m
        CreateUserResult createUserResult = identityManagementClient.createUser(createUserRequest);
        log.info("cloud user id for app with " + orgAppName + " created with "
                + createUserResult.getUser().getUserName());
        CreateAccessKeyRequest accessKeyRequest = new CreateAccessKeyRequest();

        accessKeyRequest.setUserName(createUserResult.getUser().getUserName());

        CreateAccessKeyResult accessKeyResult = identityManagementClient.createAccessKey(accessKeyRequest);

        //Create policy of queue

        GetQueueAttributesRequest attributesRequest = new GetQueueAttributesRequest();

        log.info("Going to secure sqs queue : " + AWSUtil.formFullQueueUrl(orgAppName));

        attributesRequest.setQueueUrl(AWSUtil.formFullQueueUrl(orgAppName));

        List<String> attributeNames = new ArrayList<String>();
        attributeNames.add("QueueArn");
        attributesRequest.setAttributeNames(attributeNames);

        GetQueueAttributesResult attributesResult = sqsClient.getQueueAttributes(attributesRequest);

        String queueArn = attributesResult.getAttributes().get("QueueArn");

        String policy = POLICY_DOCUMENT_TEMPLATE.replace("QUEUE_ARN", queueArn);

        String formattedPolicy = String.format(POLICY_DOCUMENT_TEMPLATE, queueArn);
        log.info("Applying authorization for following AWS resources" + formattedPolicy);

        PutUserPolicyRequest policyRequest = new PutUserPolicyRequest();

        policyRequest.setPolicyName(POLICY_NAME);

        policyRequest.setPolicyDocument(formattedPolicy);

        policyRequest.setUserName(createUserResult.getUser().getUserName());

        identityManagementClient.putUserPolicy(policyRequest);
        log.info("User policy for queue " + queueArn + " was set");

        return accessKeyResult.getAccessKey();
    } catch (EntityAlreadyExistsException e) {

        log.error("This should not happen in production. Swallowing the error fow now " + e.getMessage());
        log.error(e);
        return null;
    }
}

From source file:org.apache.usergrid.apm.service.MetricsInjestionServiceSQSImpl.java

License:Apache License

protected List<ReceiveMessageResult> getClientDataForApp(String fullAppName) {

    ArrayList<ReceiveMessageResult> messageResults = new ArrayList<ReceiveMessageResult>(
            MAX_NUMBER_OF_REQUEST_TO_PROCESS);

    try {/*from w ww.j  a va 2  s.c  om*/
        GetQueueAttributesRequest queueAttributesRequest = new GetQueueAttributesRequest();

        ArrayList<String> attributeNames = new ArrayList<String>(2);

        attributeNames.add("ApproximateNumberOfMessages");
        attributeNames.add("LastModifiedTimestamp");
        //attributeNames.add("All");

        queueAttributesRequest.setAttributeNames(attributeNames);
        String qUrl = AWSUtil.formFullQueueUrl(fullAppName);
        log.info("Getting APM data from SQS queue" + qUrl);
        queueAttributesRequest.setQueueUrl(qUrl);

        GetQueueAttributesResult queueAttributeResult = sqsClient.getQueueAttributes(queueAttributesRequest);

        String numMessagesString = queueAttributeResult.getAttributes().get("ApproximateNumberOfMessages");

        //Calculate number of "ReceiveMessage" requests need to be made.
        //TODO might need to use AsyncClient in the future to make multiple requests

        int numMessages = Integer.parseInt(numMessagesString);

        log.info("Num Messages to Process " + numMessages + " messages");

        if (numMessages > 0) {
            int receiveMessages = 0;
            int receiveRequest = 0;
            int lastNumberOfRecievedMessages = 1;

            while ((receiveMessages < numMessages) && (receiveRequest < MAX_NUMBER_OF_REQUEST_TO_PROCESS)
                    && (lastNumberOfRecievedMessages != 0)) {
                ReceiveMessageRequest receiveMessageRequest = new ReceiveMessageRequest();
                receiveMessageRequest.setMaxNumberOfMessages(MAX_NUMBER_OF_MESSAGES);
                receiveMessageRequest.setQueueUrl(qUrl);

                ArrayList<String> requestMessageAttributeNames = new ArrayList<String>(1);
                requestMessageAttributeNames.add("All");

                receiveMessageRequest.setAttributeNames(requestMessageAttributeNames);

                try {
                    ReceiveMessageResult receiveMessageResult = sqsClient.receiveMessage(receiveMessageRequest);
                    log.info("For application " + fullAppName + " Received "
                            + receiveMessageResult.getMessages().size() + " messages.");
                    receiveMessages += receiveMessageResult.getMessages().size();
                    //check if any of these messages have been downloaded already. Since SQS is distributed and injestor
                    //could have failed before deleting particular message, we check for message read count to 3. In odd
                    //case, some messages could get processed at most 3 times.
                    List<Message> messages = receiveMessageResult.getMessages();
                    String receiveCount = null;
                    Message m = null;
                    for (Iterator<Message> iter = messages.iterator(); iter.hasNext();) {
                        m = iter.next();
                        receiveCount = m.getAttributes().get("ApproximateReceiveCount");
                        if (receiveCount != null && Integer.valueOf(receiveCount) > 3) {
                            log.warn("ReceiveCount of message for app " + fullAppName
                                    + " is greater than 3 so going to delete this message before further processing");
                            sqsClient.deleteMessage(new DeleteMessageRequest(qUrl, m.getReceiptHandle()));
                            iter.remove();
                        }
                    }

                    lastNumberOfRecievedMessages = receiveMessageResult.getMessages().size();
                    if (lastNumberOfRecievedMessages > 0) {
                        messageResults.add(receiveMessageResult);
                        receiveRequest++;
                    }
                    ;
                } catch (Exception ex) {
                    log.error("Problem getting messages for " + fullAppName, ex);
                }
            }
        }
    } catch (AmazonServiceException ce) {
        log.error("Problem pulling message from SQS for " + fullAppName, ce);
    } catch (Exception e) {
        log.error("Problem getting messages for " + fullAppName, e);
    }

    return messageResults;
}

From source file:org.apache.usergrid.persistence.queue.impl.SNSQueueManagerImpl.java

License:Apache License

@Override
public long getQueueDepth() {
    String key = "ApproximateNumberOfMessages";
    try {//  ww w.j  ava  2s .  c  o m
        GetQueueAttributesResult result = sqs.getQueueAttributes(getReadQueue().getUrl(),
                Collections.singletonList(key));
        String depthString = result.getAttributes().get(key);
        return depthString != null ? Long.parseLong(depthString) : 0;
    } catch (Exception e) {
        logger.error("Exception getting queue depth", e);
        return -1;
    }
}