Java Code Examples for com.amazonaws.services.sqs.model.ReceiveMessageResult#getMessages()

The following examples show how to use com.amazonaws.services.sqs.model.ReceiveMessageResult#getMessages() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AWSSQSClient.java    From camel-kafka-connector with Apache License 2.0 6 votes vote down vote up
public void receive(String queue, Predicate<List<Message>> predicate) {
    final String queueUrl = getQueue(queue);

    LOG.debug("Consuming messages from {}", queueUrl);

    final ReceiveMessageRequest request = new ReceiveMessageRequest(queueUrl)
            .withWaitTimeSeconds(maxWaitTime)
            .withMaxNumberOfMessages(maxNumberOfMessages);

    while (true) {
        ReceiveMessageResult result = sqs.receiveMessage(request);

        List<Message> messages = result.getMessages();

        if (!predicate.test(messages)) {
            return;
        }
    }
}
 
Example 2
Source File: SqsClient.java    From kafka-connect-sqs with Apache License 2.0 6 votes vote down vote up
/**
 * Receive messages from the SQS queue.
 *
 * @param url             SQS queue url.
 * @param maxMessages     Maximum number of messages to receive for this call.
 * @param waitTimeSeconds Time to wait, in seconds, for messages to arrive.
 * @return Collection of messages received.
 */
public List<Message> receive(final String url, final int maxMessages, final int waitTimeSeconds) {
  log.debug(".receive:queue={}, max={}, wait={}", url, maxMessages, waitTimeSeconds);

  Guard.verifyValidUrl(url);
  Guard.verifyNonNegative(waitTimeSeconds, "sqs.wait.time.seconds");
  Guard.verifyInRange(maxMessages, 0, 10, "sqs.max.messages");
  if (!isValidState()) {
    throw new IllegalStateException("AmazonSQS client is not initialized");
  }

  //
  // Receive messages from queue
  //
  final ReceiveMessageRequest receiveMessageRequest = new ReceiveMessageRequest(url)
      .withMaxNumberOfMessages(maxMessages).withWaitTimeSeconds(waitTimeSeconds).withAttributeNames("");
  final ReceiveMessageResult result = client.receiveMessage(receiveMessageRequest);
  final List<Message> messages = result.getMessages();

  log.debug(".receive:{} messages, url={}, rc={}", messages.size(), url,
      result.getSdkHttpMetadata().getHttpStatusCode());

  return messages;
}
 
Example 3
Source File: SqsUnboundedReader.java    From beam with Apache License 2.0 6 votes vote down vote up
private void pull() {
  final ReceiveMessageRequest receiveMessageRequest =
      new ReceiveMessageRequest(source.getRead().queueUrl());

  receiveMessageRequest.setMaxNumberOfMessages(MAX_NUMBER_OF_MESSAGES);
  receiveMessageRequest.setAttributeNames(
      Arrays.asList(MessageSystemAttributeName.SentTimestamp.toString()));
  final ReceiveMessageResult receiveMessageResult =
      source.getSqs().receiveMessage(receiveMessageRequest);

  final List<Message> messages = receiveMessageResult.getMessages();

  if (messages == null || messages.isEmpty()) {
    return;
  }

  for (Message message : messages) {
    messagesNotYetRead.add(message);
  }
}
 
Example 4
Source File: HerdApiClientOperations.java    From herd with Apache License 2.0 6 votes vote down vote up
/**
 * Gets BusinessObjectDataKey from SQS message
 *
 * @param client the AWS SQS client
 * @param queueUrl the AWS SQS queue url
 *
 * @return BusinessObjectDataKey
 * @throws IOException if fails to retrieve BusinessObjectDataKey from SQS message
 * @throws ApiException if fails to make API call
 */
@Retryable(value = ApiException.class, backoff = @Backoff(delay = 2000, multiplier = 2))
BusinessObjectDataKey getBdataKeySqs(AmazonSQS client, String queueUrl) throws IOException, ApiException
{
    ReceiveMessageRequest receiveMessageRequest =
        new ReceiveMessageRequest().withMaxNumberOfMessages(MAX_NUM_MESSAGES).withQueueUrl(queueUrl).withWaitTimeSeconds(WAIT_TIME_SECS)
            .withVisibilityTimeout(VISIBILITY_TIMEOUT_SECS);

    LOGGER.info("Checking queue");
    ReceiveMessageResult receiveMessageResult = client.receiveMessage(receiveMessageRequest);
    if (receiveMessageResult != null && receiveMessageResult.getMessages() != null && receiveMessageResult.getMessages().size() > 0)
    {
        List<Message> sqsMessageList = receiveMessageResult.getMessages();

        LOGGER.info("Scanning {} messages for {} and {}", sqsMessageList.size(), SEARCH_KEYWORD_1, SEARCH_KEYWORD_2);
        // Get message type BUS_OBJCT_DATA_STTS_CHG
        for (Message sqsMessage : sqsMessageList)
        {
            String receivedMessageBody = sqsMessage.getBody();
            if (receivedMessageBody.contains(SEARCH_KEYWORD_1) && receivedMessageBody.contains(SEARCH_KEYWORD_2))
            {
                LOGGER.info("Received Message: {}", receivedMessageBody);
                return mapJsontoBdataKey(receivedMessageBody).getBusinessObjectDataKey();
            }
        }
    }
    throw new ApiException("No SQS message found in queue: " + queueUrl);
}
 
Example 5
Source File: AmazonSQSStubTest.java    From aws-java-sdk-stubs with Apache License 2.0 6 votes vote down vote up
@Test
public void sendAndReceiveMessage() {
  final String queueName = "bizo";
  final String messageBody = "hi everybody";

  final CreateQueueRequest createQueueRequest = new CreateQueueRequest().withQueueName(queueName);
  sqs.createQueue(createQueueRequest);

  final GetQueueUrlRequest getQueueUrlRequest = new GetQueueUrlRequest().withQueueName(queueName);
  final GetQueueUrlResult getQueueUrlResult = sqs.getQueueUrl(getQueueUrlRequest);
  final String queueUrl = getQueueUrlResult.getQueueUrl();

  final SendMessageRequest sendMessageRequest =
    new SendMessageRequest().withQueueUrl(queueUrl).withMessageBody(messageBody);
  sqs.sendMessage(sendMessageRequest);

  final int maxNumberOfMessages = 10;

  final ReceiveMessageRequest receiveMessageRequest =
    new ReceiveMessageRequest().withQueueUrl(queueUrl).withMaxNumberOfMessages(maxNumberOfMessages);
  final ReceiveMessageResult receiveMessageResult = sqs.receiveMessage(receiveMessageRequest);
  final List<Message> messages = receiveMessageResult.getMessages();

  assertThat(messages.size(), equalTo(1));
  assertThat(messages.get(0).getBody(), equalTo(messageBody));
}
 
Example 6
Source File: SqsIOTest.java    From beam with Apache License 2.0 5 votes vote down vote up
@Test
public void testWrite() {
  final AmazonSQS client = embeddedSqsRestServer.getClient();
  final String queueUrl = embeddedSqsRestServer.getQueueUrl();

  List<SendMessageRequest> messages = new ArrayList<>();
  for (int i = 0; i < 100; i++) {
    final SendMessageRequest request = new SendMessageRequest(queueUrl, "This is a test " + i);
    messages.add(request);
  }
  pipeline.apply(Create.of(messages)).apply(SqsIO.write());
  pipeline.run().waitUntilFinish();

  List<String> received = new ArrayList<>();
  while (received.size() < 100) {
    final ReceiveMessageResult receiveMessageResult = client.receiveMessage(queueUrl);

    if (receiveMessageResult.getMessages() != null) {
      for (Message message : receiveMessageResult.getMessages()) {
        received.add(message.getBody());
      }
    }
  }
  assertEquals(100, received.size());
  for (int i = 0; i < 100; i++) {
    received.contains("This is a test " + i);
  }
}
 
Example 7
Source File: AmazonSQSStubTest.java    From aws-java-sdk-stubs with Apache License 2.0 5 votes vote down vote up
@Test
public void deleteMessageSucceedsWithValidReceiptHandle() {
  final String queueName = "bizo";
  final String messageBody = "hi everybody";

  final CreateQueueRequest createQueueRequest = new CreateQueueRequest().withQueueName(queueName);
  sqs.createQueue(createQueueRequest);

  final GetQueueUrlRequest getQueueUrlRequest = new GetQueueUrlRequest().withQueueName(queueName);
  final GetQueueUrlResult getQueueUrlResult = sqs.getQueueUrl(getQueueUrlRequest);
  final String queueUrl = getQueueUrlResult.getQueueUrl();

  final SendMessageRequest sendMessageRequest =
    new SendMessageRequest().withQueueUrl(queueUrl).withMessageBody(messageBody);
  sqs.sendMessage(sendMessageRequest);

  final int maxNumberOfMessages = 10;

  final ReceiveMessageRequest receiveMessageRequest =
    new ReceiveMessageRequest().withQueueUrl(queueUrl).withMaxNumberOfMessages(maxNumberOfMessages);
  final ReceiveMessageResult receiveMessageResult = sqs.receiveMessage(receiveMessageRequest);
  final List<Message> messages = receiveMessageResult.getMessages();
  assertThat(messages.size(), equalTo(1));

  final String receiptHandle = messages.get(0).getReceiptHandle();
  final DeleteMessageRequest deleteMessageRequest =
    new DeleteMessageRequest().withQueueUrl(queueUrl).withReceiptHandle(receiptHandle);

  try {
    sqs.deleteMessage(deleteMessageRequest);
  } catch (ReceiptHandleIsInvalidException e) {
    fail("ReceiptHandleIsInvalidException was thrown");
  }
}
 
Example 8
Source File: AmazonSQSStubTest.java    From aws-java-sdk-stubs with Apache License 2.0 5 votes vote down vote up
@Test(expected = ReceiptHandleIsInvalidException.class)
public void deleteMessageFailsWithInvalidReceiptHandle() {
  final String queueName = "bizo";
  final String messageBody = "hi everybody";

  final CreateQueueRequest createQueueRequest = new CreateQueueRequest().withQueueName(queueName);
  sqs.createQueue(createQueueRequest);

  final GetQueueUrlRequest getQueueUrlRequest = new GetQueueUrlRequest().withQueueName(queueName);
  final GetQueueUrlResult getQueueUrlResult = sqs.getQueueUrl(getQueueUrlRequest);
  final String queueUrl = getQueueUrlResult.getQueueUrl();

  final SendMessageRequest sendMessageRequest =
    new SendMessageRequest().withQueueUrl(queueUrl).withMessageBody(messageBody);
  sqs.sendMessage(sendMessageRequest);

  final ReceiveMessageRequest receiveMessageRequest = new ReceiveMessageRequest().withQueueUrl(queueUrl);
  final ReceiveMessageResult receiveMessageResult = sqs.receiveMessage(receiveMessageRequest);
  final List<Message> messages = receiveMessageResult.getMessages();
  assertThat(messages.size(), equalTo(1));

  final String receiptHandle = "bizo";
  final DeleteMessageRequest deleteMessageRequest =
    new DeleteMessageRequest().withQueueUrl(queueUrl).withReceiptHandle(receiptHandle);

  sqs.deleteMessage(deleteMessageRequest);
}
 
Example 9
Source File: TemporarySQSQueue.java    From front50 with Apache License 2.0 5 votes vote down vote up
List<Message> fetchMessages() {
  ReceiveMessageResult receiveMessageResult =
      amazonSQS.receiveMessage(
          new ReceiveMessageRequest(temporaryQueue.sqsQueueUrl)
              .withMaxNumberOfMessages(10)
              .withWaitTimeSeconds(1));

  return receiveMessageResult.getMessages();
}
 
Example 10
Source File: SqsManager.java    From aws-cloudtrail-processing-library with Apache License 2.0 5 votes vote down vote up
/**
 * Poll SQS queue for incoming messages, filter them, and return a list of SQS Messages.
 *
 * @return a list of SQS messages.
 */
public List<Message> pollQueue() {
    boolean success = false;
    ProgressStatus pollQueueStatus = new ProgressStatus(ProgressState.pollQueue, new BasicPollQueueInfo(0, success));
    final Object reportObject = progressReporter.reportStart(pollQueueStatus);

    ReceiveMessageRequest request = new ReceiveMessageRequest().withAttributeNames(ALL_ATTRIBUTES);
    request.setQueueUrl(config.getSqsUrl());
    request.setVisibilityTimeout(config.getVisibilityTimeout());
    request.setMaxNumberOfMessages(DEFAULT_SQS_MESSAGE_SIZE_LIMIT);
    request.setWaitTimeSeconds(DEFAULT_WAIT_TIME_SECONDS);

    List<Message> sqsMessages = new ArrayList<Message>();
    try {

        ReceiveMessageResult result = sqsClient.receiveMessage(request);
        sqsMessages = result.getMessages();
        logger.info("Polled " + sqsMessages.size() + " sqs messages from " + config.getSqsUrl());

        success = true;
    } catch (AmazonServiceException e) {
        LibraryUtils.handleException(exceptionHandler, pollQueueStatus, e, "Failed to poll sqs message.");

    } finally {
        LibraryUtils.endToProcess(progressReporter, success, pollQueueStatus, reportObject);
    }
    return sqsMessages;

}
 
Example 11
Source File: AmazonSQSStubTest.java    From aws-java-sdk-stubs with Apache License 2.0 4 votes vote down vote up
@Test
public void receiveMessageReturnsMaxNumberOfMessages() {
  final String queueName = "bizo";
  final String baseMessageBody = "hi everybody";

  final CreateQueueRequest createQueueRequest = new CreateQueueRequest().withQueueName(queueName);
  sqs.createQueue(createQueueRequest);

  final GetQueueUrlRequest getQueueUrlRequest = new GetQueueUrlRequest().withQueueName(queueName);
  final GetQueueUrlResult getQueueUrlResult = sqs.getQueueUrl(getQueueUrlRequest);
  final String queueUrl = getQueueUrlResult.getQueueUrl();

  final List<String> sentMessageBodies = new ArrayList<String>();
  final int numMessages = 7;
  for (int i = 0; i < numMessages; i++) {
    sentMessageBodies.add(baseMessageBody + " " + i);
  }

  for (String messageBody : sentMessageBodies) {
    final SendMessageRequest sendMessageRequest =
      new SendMessageRequest().withQueueUrl(queueUrl).withMessageBody(messageBody);
    sqs.sendMessage(sendMessageRequest);
  }

  final int maxNumberOfMessages = 5;

  final List<String> expectedMessageBodies = sentMessageBodies.subList(0, maxNumberOfMessages);

  final ReceiveMessageRequest receiveMessageRequest =
    new ReceiveMessageRequest().withQueueUrl(queueUrl).withMaxNumberOfMessages(maxNumberOfMessages);

  final ReceiveMessageResult receiveMessageResult = sqs.receiveMessage(receiveMessageRequest);
  final List<Message> messages = receiveMessageResult.getMessages();
  assertThat(messages.size(), equalTo(maxNumberOfMessages));

  final List<String> actualMessageBodies = new ArrayList<String>();
  for (Message m : messages) {
    actualMessageBodies.add(m.getBody());
  }
  assertThat(actualMessageBodies, equalTo(expectedMessageBodies));
}
 
Example 12
Source File: AmazonSQSStubTest.java    From aws-java-sdk-stubs with Apache License 2.0 4 votes vote down vote up
@Test
public void receiveMessageOnlyReturnsRequestedAttributes() {
  final String queueName = "bizo";
  final String messageBody = "hi everybody";

  final CreateQueueRequest createQueueRequest = new CreateQueueRequest().withQueueName(queueName);
  sqs.createQueue(createQueueRequest);

  final GetQueueUrlRequest getQueueUrlRequest = new GetQueueUrlRequest().withQueueName(queueName);
  final GetQueueUrlResult getQueueUrlResult = sqs.getQueueUrl(getQueueUrlRequest);
  final String queueUrl = getQueueUrlResult.getQueueUrl();

  final Map<String, MessageAttributeValue> messageAttributes = new HashMap<String, MessageAttributeValue>();
  messageAttributes.put("key A", new MessageAttributeValue().withDataType("String").withStringValue("value A"));
  messageAttributes.put("key B", new MessageAttributeValue().withDataType("String").withStringValue("value B"));
  messageAttributes.put("key C", new MessageAttributeValue().withDataType("String").withStringValue("value C"));
  messageAttributes.put("key D", new MessageAttributeValue().withDataType("String").withStringValue("value D"));
  messageAttributes.put("key E", new MessageAttributeValue().withDataType("String").withStringValue("value E"));
  final SendMessageRequest sendMessageRequest =
    new SendMessageRequest()
      .withQueueUrl(queueUrl)
      .withMessageBody(messageBody)
      .withMessageAttributes(messageAttributes);
  sqs.sendMessage(sendMessageRequest);

  final int maxNumberOfMessages = 10;

  final ReceiveMessageRequest receiveMessageRequest =
    new ReceiveMessageRequest()
      .withQueueUrl(queueUrl)
      .withMaxNumberOfMessages(maxNumberOfMessages)
      .withMessageAttributeNames("key B", "key E");
  final ReceiveMessageResult receiveMessageResult = sqs.receiveMessage(receiveMessageRequest);
  final List<Message> messages = receiveMessageResult.getMessages();

  assertThat(messages.size(), equalTo(1));

  Message m = messages.get(0);
  assertThat(m.getBody(), equalTo(messageBody));

  final Map<String, MessageAttributeValue> expectedMessageAttributes =
    new HashMap<String, MessageAttributeValue>(messageAttributes);
  expectedMessageAttributes.keySet().retainAll(Arrays.asList("key B", "key E"));
  assertThat(m.getMessageAttributes(), equalTo(expectedMessageAttributes));
}
 
Example 13
Source File: AmazonSQSStubTest.java    From aws-java-sdk-stubs with Apache License 2.0 4 votes vote down vote up
@Test
public void returnMessage() {
  final String queueName = "bizo";

  final CreateQueueRequest createQueueRequest = new CreateQueueRequest().withQueueName(queueName);
  sqs.createQueue(createQueueRequest);

  final GetQueueUrlRequest getQueueUrlRequest = new GetQueueUrlRequest().withQueueName(queueName);
  final GetQueueUrlResult getQueueUrlResult = sqs.getQueueUrl(getQueueUrlRequest);
  final String queueUrl = getQueueUrlResult.getQueueUrl();

  final SendMessageRequest sendMessageRequest1 =
    new SendMessageRequest().withQueueUrl(queueUrl).withMessageBody("hi everybody 1");
  sqs.sendMessage(sendMessageRequest1);

  final SendMessageRequest sendMessageRequest2 =
    new SendMessageRequest().withQueueUrl(queueUrl).withMessageBody("hi everybody 2");
  sqs.sendMessage(sendMessageRequest2);

  final SendMessageRequest sendMessageRequest3 =
    new SendMessageRequest().withQueueUrl(queueUrl).withMessageBody("hi everybody 3");
  sqs.sendMessage(sendMessageRequest3);

  final int maxNumberOfMessages = 10;

  final ReceiveMessageRequest receiveMessageRequest1 =
    new ReceiveMessageRequest().withQueueUrl(queueUrl).withMaxNumberOfMessages(maxNumberOfMessages);
  final ReceiveMessageResult receiveMessageResult1 = sqs.receiveMessage(receiveMessageRequest1);
  final List<Message> messages1 = receiveMessageResult1.getMessages();

  final SendMessageRequest sendMessageRequest4 =
    new SendMessageRequest().withQueueUrl(queueUrl).withMessageBody("hi everybody 4");
  sqs.sendMessage(sendMessageRequest4);

  // "hi everybody 2" should be at the top of the queue after being returned.
  sqs.returnMessage(queueUrl, messages1.get(1));

  final ReceiveMessageRequest receiveMessageRequest2 =
    new ReceiveMessageRequest().withQueueUrl(queueUrl).withMaxNumberOfMessages(maxNumberOfMessages);
  final ReceiveMessageResult receiveMessageResult2 = sqs.receiveMessage(receiveMessageRequest2);
  final List<Message> messages2 = receiveMessageResult2.getMessages();

  final List<String> expectedMessageBodies =
    Arrays.asList(sendMessageRequest2.getMessageBody(), sendMessageRequest4.getMessageBody());

  final List<String> actualMessageBodies = new ArrayList<String>();
  for (Message m : messages2) {
    actualMessageBodies.add(m.getBody());
  }

  assertThat(actualMessageBodies, equalTo(expectedMessageBodies));
}
 
Example 14
Source File: CloudtrailSQSClient.java    From graylog-plugin-aws with Apache License 2.0 4 votes vote down vote up
public List<CloudtrailSNSNotification> getNotifications() {

        LOG.debug("Fetching SQS CloudTrail notifications.");

        List<CloudtrailSNSNotification> notifications = Lists.newArrayList();

        ReceiveMessageRequest request = new ReceiveMessageRequest(queueName);
        request.setMaxNumberOfMessages(10);
        ReceiveMessageResult result = sqs.receiveMessage(request);

        LOG.debug("Received [{}] SQS CloudTrail notifications.", result.getMessages().size());
        CloudtrailSNSNotificationParser parser = new CloudtrailSNSNotificationParser(objectMapper);
        LOG.debug("Finished parsing notifications.");

        for (Message message : result.getMessages()) {
            notifications.addAll(parser.parse(message));
        }

        LOG.debug("Returning notifications.");

        return notifications;
    }
 
Example 15
Source File: AlertJanitor.java    From s3mper with Apache License 2.0 4 votes vote down vote up
private List<Message> pull(String queue, int max) {
    ReceiveMessageResult result = sqs.receiveMessage(new ReceiveMessageRequest(queue).withMaxNumberOfMessages(max));
    
    return result.getMessages();
}