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

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

Introduction

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

Prototype

SetQueueAttributesResult setQueueAttributes(SetQueueAttributesRequest setQueueAttributesRequest);

Source Link

Document

Sets the value of one or more queue attributes.

Usage

From source file:aws.example.sqs.DeadLetterQueues.java

License:Open Source License

public static void main(String[] args) {
    if (args.length != 2) {
        System.out.println("Usage: DeadLetterQueues <src_queue_name> <dl_queue_name>");
        System.exit(1);// w w  w.  ja v  a2 s. c om
    }

    String src_queue_name = args[0];
    String dl_queue_name = args[1];

    final AmazonSQS sqs = AmazonSQSClientBuilder.defaultClient();

    // Create source queue
    try {
        sqs.createQueue(src_queue_name);
    } catch (AmazonSQSException e) {
        if (!e.getErrorCode().equals("QueueAlreadyExists")) {
            throw e;
        }
    }

    // Create dead-letter queue
    try {
        sqs.createQueue(dl_queue_name);
    } catch (AmazonSQSException e) {
        if (!e.getErrorCode().equals("QueueAlreadyExists")) {
            throw e;
        }
    }

    // Get dead-letter queue ARN
    String dl_queue_url = sqs.getQueueUrl(dl_queue_name).getQueueUrl();

    GetQueueAttributesResult queue_attrs = sqs
            .getQueueAttributes(new GetQueueAttributesRequest(dl_queue_url).withAttributeNames("QueueArn"));

    String dl_queue_arn = queue_attrs.getAttributes().get("QueueArn");

    // Set dead letter queue with redrive policy on source queue.
    String src_queue_url = sqs.getQueueUrl(src_queue_name).getQueueUrl();

    SetQueueAttributesRequest request = new SetQueueAttributesRequest().withQueueUrl(src_queue_url)
            .addAttributesEntry("RedrivePolicy",
                    "{\"maxReceiveCount\":\"5\", \"deadLetterTargetArn\":\"" + dl_queue_arn + "\"}");

    sqs.setQueueAttributes(request);
}

From source file:aws.example.sqs.LongPolling.java

License:Open Source License

public static void main(String[] args) {
    final String USAGE = "To run this example, supply the name of a queue to create and\n"
            + "queue url of an existing queue.\n\n"
            + "Ex: LongPolling <unique-queue-name> <existing-queue-url>\n";

    if (args.length != 2) {
        System.out.println(USAGE);
        System.exit(1);/*ww w  .jav a2 s . c om*/
    }

    String queue_name = args[0];
    String queue_url = args[1];

    final AmazonSQS sqs = AmazonSQSClientBuilder.defaultClient();

    // Enable long polling when creating a queue
    CreateQueueRequest create_request = new CreateQueueRequest().withQueueName(queue_name)
            .addAttributesEntry("ReceiveMessageWaitTimeSeconds", "20");

    try {
        sqs.createQueue(create_request);
    } catch (AmazonSQSException e) {
        if (!e.getErrorCode().equals("QueueAlreadyExists")) {
            throw e;
        }
    }

    // Enable long polling on an existing queue
    SetQueueAttributesRequest set_attrs_request = new SetQueueAttributesRequest().withQueueUrl(queue_url)
            .addAttributesEntry("ReceiveMessageWaitTimeSeconds", "20");
    sqs.setQueueAttributes(set_attrs_request);

    // Enable long polling on a message receipt
    ReceiveMessageRequest receive_request = new ReceiveMessageRequest().withQueueUrl(queue_url)
            .withWaitTimeSeconds(20);
    sqs.receiveMessage(receive_request);
}

From source file:io.konig.maven.CreateAwsSqsQueueAction.java

License:Apache License

public AwsDeployment from(String path) throws Exception {
    String cfTemplatePresent = System.getProperty("cfTemplatePresent");
    if (cfTemplatePresent == null || cfTemplatePresent.equals("N")) {
        try {/*from www  . j a  v a  2  s  . c o  m*/
            File file = deployment.file(path);
            ObjectMapper mapper = new ObjectMapper();
            S3Bucket bucket = mapper.readValue(file, S3Bucket.class);
            deployment.verifyAWSCredentials();

            QueueConfiguration queueConfig = bucket.getNotificationConfiguration().getQueueConfiguration();

            if (queueConfig != null && queueConfig.getQueue() != null) {
                String accountId = "";
                if (System.getProperty("aws-account-id") != null) {
                    accountId = System.getProperty("aws-account-id");
                }

                Queue queue = queueConfig.getQueue();
                Regions regions = Regions.fromName(queue.getRegion());
                AmazonSQS sqs = AmazonSQSClientBuilder.standard().withCredentials(deployment.getCredential())
                        .withRegion(regions).build();
                AmazonSNS sns = AmazonSNSClientBuilder.standard().withCredentials(deployment.getCredential())
                        .withRegion(regions).build();

                CreateQueueResult result = sqs.createQueue(queue.getResourceName());

                String topicArn = StringUtils.replaceOnce(
                        bucket.getNotificationConfiguration().getTopicConfiguration().getTopicArn(),
                        "${aws-account-id}", accountId);
                String queueArn = StringUtils.replaceOnce(
                        bucket.getNotificationConfiguration().getQueueConfiguration().getQueueArn(),
                        "${aws-account-id}", accountId);

                deployment.setResponse("Queue  " + queueArn + " is created");

                Policy policy = new Policy()
                        .withStatements(new Statement(Effect.Allow).withPrincipals(Principal.AllUsers)
                                .withActions(SQSActions.SendMessage).withResources(new Resource(queueArn))
                                .withConditions(ConditionFactory.newSourceArnCondition(topicArn)));

                Map<String, String> queueAttributes = new HashMap<String, String>();
                queueAttributes.put(QueueAttributeName.Policy.toString(), policy.toJson());

                deployment.setResponse("Queue Policy Configured : " + policy.toJson());

                sqs.setQueueAttributes(new SetQueueAttributesRequest(result.getQueueUrl(), queueAttributes));

                Topics.subscribeQueue(sns, sqs, topicArn, result.getQueueUrl());

                deployment.setResponse(
                        "Subscription is created : Topic [" + topicArn + "], Queue [" + queueArn + "]");
            } else {
                deployment.setResponse("Queue Configuration Failed");
            }

        } catch (Exception e) {
            throw e;
        }
    } else {
        deployment.setResponse("Queue will be created through cloud formation template");
    }
    return deployment;
}

From source file:org.apache.camel.component.aws.sqs.SqsEndpoint.java

License:Apache License

private void updateQueueAttributes(AmazonSQS client) {
    SetQueueAttributesRequest request = new SetQueueAttributesRequest();
    request.setQueueUrl(queueUrl);/*from  w w  w . ja va2s . c  o m*/
    if (getConfiguration().getDefaultVisibilityTimeout() != null) {
        request.getAttributes().put(QueueAttributeName.VisibilityTimeout.name(),
                String.valueOf(getConfiguration().getDefaultVisibilityTimeout()));
    }
    if (getConfiguration().getMaximumMessageSize() != null) {
        request.getAttributes().put(QueueAttributeName.MaximumMessageSize.name(),
                String.valueOf(getConfiguration().getMaximumMessageSize()));
    }
    if (getConfiguration().getMessageRetentionPeriod() != null) {
        request.getAttributes().put(QueueAttributeName.MessageRetentionPeriod.name(),
                String.valueOf(getConfiguration().getMessageRetentionPeriod()));
    }
    if (getConfiguration().getPolicy() != null) {
        request.getAttributes().put(QueueAttributeName.Policy.name(),
                String.valueOf(getConfiguration().getPolicy()));
    }
    if (getConfiguration().getReceiveMessageWaitTimeSeconds() != null) {
        request.getAttributes().put(QueueAttributeName.ReceiveMessageWaitTimeSeconds.name(),
                String.valueOf(getConfiguration().getReceiveMessageWaitTimeSeconds()));
    }
    if (!request.getAttributes().isEmpty()) {
        LOG.trace("Updating queue '{}' with the provided queue attributes...", configuration.getQueueName());
        client.setQueueAttributes(request);
        LOG.trace("Queue '{}' updated and available at {}'", configuration.getQueueName(), queueUrl);
    }
}