Search in sources :

Example 1 with SendMessageBatchRequestEntry

use of com.amazonaws.services.sqs.model.SendMessageBatchRequestEntry 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)1 AmazonSQSException (com.amazonaws.services.sqs.model.AmazonSQSException)1 CreateQueueResult (com.amazonaws.services.sqs.model.CreateQueueResult)1 Message (com.amazonaws.services.sqs.model.Message)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