Search in sources :

Example 6 with AmazonSQS

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

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

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