Search in sources :

Example 1 with CreateQueueRequest

use of com.amazonaws.services.sqs.model.CreateQueueRequest in project camel by apache.

the class SqsEndpoint method createQueue.

protected void createQueue(AmazonSQS client) {
    LOG.trace("Queue '{}' doesn't exist. Will create it...", configuration.getQueueName());
    // creates a new queue, or returns the URL of an existing one
    CreateQueueRequest request = new CreateQueueRequest(configuration.getQueueName());
    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 (getConfiguration().getRedrivePolicy() != null) {
        request.getAttributes().put(QueueAttributeName.RedrivePolicy.name(), getConfiguration().getRedrivePolicy());
    }
    LOG.trace("Creating queue [{}] with request [{}]...", configuration.getQueueName(), request);
    CreateQueueResult queueResult = client.createQueue(request);
    queueUrl = queueResult.getQueueUrl();
    LOG.trace("Queue created and available at: {}", queueUrl);
}
Also used : CreateQueueRequest(com.amazonaws.services.sqs.model.CreateQueueRequest) CreateQueueResult(com.amazonaws.services.sqs.model.CreateQueueResult)

Example 2 with CreateQueueRequest

use of com.amazonaws.services.sqs.model.CreateQueueRequest in project glacier-cli by carlossg.

the class Glacier method setupSQS.

// ==============
// Helper methods
// ==============
private QueueConfig setupSQS(String sqsQueueName) {
    QueueConfig config = new QueueConfig();
    CreateQueueRequest request = new CreateQueueRequest().withQueueName(sqsQueueName);
    CreateQueueResult result = sqsClient.createQueue(request);
    config.sqsQueueURL = result.getQueueUrl();
    GetQueueAttributesRequest qRequest = new GetQueueAttributesRequest().withQueueUrl(config.sqsQueueURL).withAttributeNames("QueueArn");
    GetQueueAttributesResult qResult = sqsClient.getQueueAttributes(qRequest);
    config.sqsQueueARN = qResult.getAttributes().get("QueueArn");
    Policy sqsPolicy = new Policy().withStatements(new Statement(Effect.Allow).withPrincipals(Principal.AllUsers).withActions(SQSActions.SendMessage).withResources(new Resource(config.sqsQueueARN)));
    Map<String, String> queueAttributes = new HashMap<String, String>();
    queueAttributes.put("Policy", sqsPolicy.toJson());
    sqsClient.setQueueAttributes(new SetQueueAttributesRequest(config.sqsQueueURL, queueAttributes));
    return config;
}
Also used : Policy(com.amazonaws.auth.policy.Policy) GetQueueAttributesResult(com.amazonaws.services.sqs.model.GetQueueAttributesResult) GetQueueAttributesRequest(com.amazonaws.services.sqs.model.GetQueueAttributesRequest) HashMap(java.util.HashMap) Statement(com.amazonaws.auth.policy.Statement) CreateQueueRequest(com.amazonaws.services.sqs.model.CreateQueueRequest) Resource(com.amazonaws.auth.policy.Resource) CreateQueueResult(com.amazonaws.services.sqs.model.CreateQueueResult) SetQueueAttributesRequest(com.amazonaws.services.sqs.model.SetQueueAttributesRequest)

Example 3 with CreateQueueRequest

use of com.amazonaws.services.sqs.model.CreateQueueRequest in project aws-doc-sdk-examples by awsdocs.

the class UsingQueues method main.

public static void main(String[] args) {
    AmazonSQS sqs = AmazonSQSClientBuilder.defaultClient();
    // Creating a Queue
    CreateQueueRequest create_request = new CreateQueueRequest(QUEUE_NAME).addAttributesEntry("DelaySeconds", "60").addAttributesEntry("MessageRetentionPeriod", "86400");
    try {
        sqs.createQueue(create_request);
    } catch (AmazonSQSException e) {
        if (!e.getErrorCode().equals("QueueAlreadyExists")) {
            throw e;
        }
    }
    // Get the URL for a queue
    String queue_url = sqs.getQueueUrl(QUEUE_NAME).getQueueUrl();
    // Delete the Queue
    sqs.deleteQueue(queue_url);
    sqs.createQueue("Queue1" + new Date().getTime());
    sqs.createQueue("Queue2" + new Date().getTime());
    sqs.createQueue("MyQueue" + new Date().getTime());
    // List your queues
    ListQueuesResult lq_result = sqs.listQueues();
    System.out.println("Your SQS Queue URLs:");
    for (String url : lq_result.getQueueUrls()) {
        System.out.println(url);
    }
    // List queues with filters
    String name_prefix = "Queue";
    lq_result = sqs.listQueues(new ListQueuesRequest(name_prefix));
    System.out.println("Queue URLs with prefix: " + name_prefix);
    for (String url : lq_result.getQueueUrls()) {
        System.out.println(url);
    }
}
Also used : ListQueuesResult(com.amazonaws.services.sqs.model.ListQueuesResult) CreateQueueRequest(com.amazonaws.services.sqs.model.CreateQueueRequest) AmazonSQS(com.amazonaws.services.sqs.AmazonSQS) Date(java.util.Date) ListQueuesRequest(com.amazonaws.services.sqs.model.ListQueuesRequest) AmazonSQSException(com.amazonaws.services.sqs.model.AmazonSQSException)

Example 4 with CreateQueueRequest

use of com.amazonaws.services.sqs.model.CreateQueueRequest in project aws-doc-sdk-examples by awsdocs.

the class LongPolling method main.

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);
    }
    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);
}
Also used : ReceiveMessageRequest(com.amazonaws.services.sqs.model.ReceiveMessageRequest) CreateQueueRequest(com.amazonaws.services.sqs.model.CreateQueueRequest) AmazonSQS(com.amazonaws.services.sqs.AmazonSQS) SetQueueAttributesRequest(com.amazonaws.services.sqs.model.SetQueueAttributesRequest) AmazonSQSException(com.amazonaws.services.sqs.model.AmazonSQSException)

Aggregations

CreateQueueRequest (com.amazonaws.services.sqs.model.CreateQueueRequest)4 AmazonSQS (com.amazonaws.services.sqs.AmazonSQS)2 AmazonSQSException (com.amazonaws.services.sqs.model.AmazonSQSException)2 CreateQueueResult (com.amazonaws.services.sqs.model.CreateQueueResult)2 SetQueueAttributesRequest (com.amazonaws.services.sqs.model.SetQueueAttributesRequest)2 Policy (com.amazonaws.auth.policy.Policy)1 Resource (com.amazonaws.auth.policy.Resource)1 Statement (com.amazonaws.auth.policy.Statement)1 GetQueueAttributesRequest (com.amazonaws.services.sqs.model.GetQueueAttributesRequest)1 GetQueueAttributesResult (com.amazonaws.services.sqs.model.GetQueueAttributesResult)1 ListQueuesRequest (com.amazonaws.services.sqs.model.ListQueuesRequest)1 ListQueuesResult (com.amazonaws.services.sqs.model.ListQueuesResult)1 ReceiveMessageRequest (com.amazonaws.services.sqs.model.ReceiveMessageRequest)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1