Search in sources :

Example 1 with AmazonSQS

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

the class SqsEndpoint method createClient.

/**
     * Provide the possibility to override this method for an mock implementation
     * @return AmazonSQSClient
     */
AmazonSQS createClient() {
    AmazonSQS client = null;
    ClientConfiguration clientConfiguration = null;
    boolean isClientConfigFound = false;
    if (ObjectHelper.isNotEmpty(configuration.getProxyHost()) && ObjectHelper.isNotEmpty(configuration.getProxyPort())) {
        clientConfiguration = new ClientConfiguration();
        clientConfiguration.setProxyHost(configuration.getProxyHost());
        clientConfiguration.setProxyPort(configuration.getProxyPort());
        isClientConfigFound = true;
    }
    if (configuration.getAccessKey() != null && configuration.getSecretKey() != null) {
        AWSCredentials credentials = new BasicAWSCredentials(configuration.getAccessKey(), configuration.getSecretKey());
        if (isClientConfigFound) {
            client = new AmazonSQSClient(credentials, clientConfiguration);
        } else {
            client = new AmazonSQSClient(credentials);
        }
    } else {
        if (isClientConfigFound) {
            client = new AmazonSQSClient();
        } else {
            client = new AmazonSQSClient(clientConfiguration);
        }
    }
    return client;
}
Also used : AmazonSQS(com.amazonaws.services.sqs.AmazonSQS) AmazonSQSClient(com.amazonaws.services.sqs.AmazonSQSClient) AWSCredentials(com.amazonaws.auth.AWSCredentials) BasicAWSCredentials(com.amazonaws.auth.BasicAWSCredentials) ClientConfiguration(com.amazonaws.ClientConfiguration) BasicAWSCredentials(com.amazonaws.auth.BasicAWSCredentials)

Example 2 with AmazonSQS

use of com.amazonaws.services.sqs.AmazonSQS 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 3 with AmazonSQS

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

the class VisibilityTimeout method changeMessageVisibilityMultiple.

// Change the visibility timeout for multiple messages.
public static void changeMessageVisibilityMultiple(String queue_url, int timeout) {
    AmazonSQS sqs = AmazonSQSClientBuilder.defaultClient();
    List<ChangeMessageVisibilityBatchRequestEntry> entries = new ArrayList<ChangeMessageVisibilityBatchRequestEntry>();
    entries.add(new ChangeMessageVisibilityBatchRequestEntry("unique_id_msg1", sqs.receiveMessage(queue_url).getMessages().get(0).getReceiptHandle()).withVisibilityTimeout(timeout));
    entries.add(new ChangeMessageVisibilityBatchRequestEntry("unique_id_msg2", sqs.receiveMessage(queue_url).getMessages().get(0).getReceiptHandle()).withVisibilityTimeout(timeout + 200));
    sqs.changeMessageVisibilityBatch(queue_url, entries);
}
Also used : ArrayList(java.util.ArrayList) ChangeMessageVisibilityBatchRequestEntry(com.amazonaws.services.sqs.model.ChangeMessageVisibilityBatchRequestEntry) AmazonSQS(com.amazonaws.services.sqs.AmazonSQS)

Example 4 with AmazonSQS

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

the class VisibilityTimeout method changeMessageVisibilitySingle.

// Change the visibility timeout for a single message
public static void changeMessageVisibilitySingle(String queue_url, int timeout) {
    AmazonSQS sqs = AmazonSQSClientBuilder.defaultClient();
    // Get the receipt handle for the first message in the queue.
    String receipt = sqs.receiveMessage(queue_url).getMessages().get(0).getReceiptHandle();
    sqs.changeMessageVisibility(queue_url, receipt, timeout);
}
Also used : AmazonSQS(com.amazonaws.services.sqs.AmazonSQS)

Example 5 with AmazonSQS

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

the class VisibilityTimeout method main.

public static void main(String[] args) {
    final String queue_name = "testQueue" + new Date().getTime();
    AmazonSQS sqs = AmazonSQSClientBuilder.defaultClient();
    // first, create a queue (unless it exists already)
    try {
        CreateQueueResult cq_result = sqs.createQueue(queue_name);
    } catch (AmazonSQSException e) {
        if (!e.getErrorCode().equals("QueueAlreadyExists")) {
            throw e;
        }
    }
    final String queue_url = sqs.getQueueUrl(queue_name).getQueueUrl();
    // Send some messages to the queue
    for (int i = 0; i < 20; i++) {
        sqs.sendMessage(queue_url, "This is message " + i);
    }
    // change visibility timeout (single)
    changeMessageVisibilitySingle(queue_url, 3600);
    // change visibility timeout (multiple)
    changeMessageVisibilityMultiple(queue_url, 2000);
}
Also used : AmazonSQS(com.amazonaws.services.sqs.AmazonSQS) CreateQueueResult(com.amazonaws.services.sqs.model.CreateQueueResult) Date(java.util.Date) AmazonSQSException(com.amazonaws.services.sqs.model.AmazonSQSException)

Aggregations

AmazonSQS (com.amazonaws.services.sqs.AmazonSQS)8 AmazonSQSException (com.amazonaws.services.sqs.model.AmazonSQSException)5 CreateQueueRequest (com.amazonaws.services.sqs.model.CreateQueueRequest)2 CreateQueueResult (com.amazonaws.services.sqs.model.CreateQueueResult)2 SetQueueAttributesRequest (com.amazonaws.services.sqs.model.SetQueueAttributesRequest)2 Date (java.util.Date)2 ClientConfiguration (com.amazonaws.ClientConfiguration)1 AWSCredentials (com.amazonaws.auth.AWSCredentials)1 BasicAWSCredentials (com.amazonaws.auth.BasicAWSCredentials)1 AmazonSQSClient (com.amazonaws.services.sqs.AmazonSQSClient)1 ChangeMessageVisibilityBatchRequestEntry (com.amazonaws.services.sqs.model.ChangeMessageVisibilityBatchRequestEntry)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 Message (com.amazonaws.services.sqs.model.Message)1 ReceiveMessageRequest (com.amazonaws.services.sqs.model.ReceiveMessageRequest)1 SendMessageBatchRequest (com.amazonaws.services.sqs.model.SendMessageBatchRequest)1 SendMessageBatchRequestEntry (com.amazonaws.services.sqs.model.SendMessageBatchRequestEntry)1 SendMessageRequest (com.amazonaws.services.sqs.model.SendMessageRequest)1