Search in sources :

Example 1 with AmazonSQSException

use of com.amazonaws.services.sqs.model.AmazonSQSException 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 2 with AmazonSQSException

use of com.amazonaws.services.sqs.model.AmazonSQSException 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)

Example 3 with AmazonSQSException

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

the class DeadLetterQueues method main.

public static void main(String[] args) {
    if (args.length != 2) {
        System.out.println("Usage: DeadLetterQueues <src_queue_name> <dl_queue_name>");
        System.exit(1);
    }
    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);
}
Also used : GetQueueAttributesResult(com.amazonaws.services.sqs.model.GetQueueAttributesResult) GetQueueAttributesRequest(com.amazonaws.services.sqs.model.GetQueueAttributesRequest) AmazonSQS(com.amazonaws.services.sqs.AmazonSQS) SetQueueAttributesRequest(com.amazonaws.services.sqs.model.SetQueueAttributesRequest) AmazonSQSException(com.amazonaws.services.sqs.model.AmazonSQSException)

Example 4 with AmazonSQSException

use of com.amazonaws.services.sqs.model.AmazonSQSException 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)

Example 5 with AmazonSQSException

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

the class SendReceiveMessages method main.

public static void main(String[] args) {
    final AmazonSQS sqs = AmazonSQSClientBuilder.defaultClient();
    try {
        CreateQueueResult create_result = sqs.createQueue(QUEUE_NAME);
    } catch (AmazonSQSException e) {
        if (!e.getErrorCode().equals("QueueAlreadyExists")) {
            throw e;
        }
    }
    String queueUrl = sqs.getQueueUrl(QUEUE_NAME).getQueueUrl();
    SendMessageRequest send_msg_request = new SendMessageRequest().withQueueUrl(queueUrl).withMessageBody("hello world").withDelaySeconds(5);
    sqs.sendMessage(send_msg_request);
    // Send multiple messages to the queue
    SendMessageBatchRequest send_batch_request = new SendMessageBatchRequest().withQueueUrl(queueUrl).withEntries(new SendMessageBatchRequestEntry("msg_1", "Hello from message 1"), new SendMessageBatchRequestEntry("msg_2", "Hello from message 2").withDelaySeconds(10));
    sqs.sendMessageBatch(send_batch_request);
    // receive messages from the queue
    List<Message> messages = sqs.receiveMessage(queueUrl).getMessages();
    // delete messages from the queue
    for (Message m : messages) {
        sqs.deleteMessage(queueUrl, m.getReceiptHandle());
    }
}
Also used : SendMessageBatchRequest(com.amazonaws.services.sqs.model.SendMessageBatchRequest) Message(com.amazonaws.services.sqs.model.Message) AmazonSQS(com.amazonaws.services.sqs.AmazonSQS) CreateQueueResult(com.amazonaws.services.sqs.model.CreateQueueResult) SendMessageBatchRequestEntry(com.amazonaws.services.sqs.model.SendMessageBatchRequestEntry) AmazonSQSException(com.amazonaws.services.sqs.model.AmazonSQSException) SendMessageRequest(com.amazonaws.services.sqs.model.SendMessageRequest)

Aggregations

AmazonSQS (com.amazonaws.services.sqs.AmazonSQS)5 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 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