software.amazon.awssdk.services.sqs.model.ReceiveMessageRequest Java Examples

The following examples show how to use software.amazon.awssdk.services.sqs.model.ReceiveMessageRequest. 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: MessageMD5ChecksumInterceptor.java    From aws-sdk-java-v2 with Apache License 2.0 6 votes vote down vote up
@Override
public void afterExecution(Context.AfterExecution context, ExecutionAttributes executionAttributes) {
    SdkResponse response = context.response();
    SdkRequest originalRequest = context.request();
    if (response != null) {
        if (originalRequest instanceof SendMessageRequest) {
            SendMessageRequest sendMessageRequest = (SendMessageRequest) originalRequest;
            SendMessageResponse sendMessageResult = (SendMessageResponse) response;
            sendMessageOperationMd5Check(sendMessageRequest, sendMessageResult);

        } else if (originalRequest instanceof ReceiveMessageRequest) {
            ReceiveMessageResponse receiveMessageResult = (ReceiveMessageResponse) response;
            receiveMessageResultMd5Check(receiveMessageResult);

        } else if (originalRequest instanceof SendMessageBatchRequest) {
            SendMessageBatchRequest sendMessageBatchRequest = (SendMessageBatchRequest) originalRequest;
            SendMessageBatchResponse sendMessageBatchResult = (SendMessageBatchResponse) response;
            sendMessageBatchOperationMd5Check(sendMessageBatchRequest, sendMessageBatchResult);
        }
    }
}
 
Example #2
Source File: MessageAttributesIntegrationTest.java    From aws-sdk-java-v2 with Apache License 2.0 6 votes vote down vote up
/**
 * Makes sure we don't modify the state of ByteBuffer backed attributes in anyway internally
 * before returning the result to the customer. See https://github.com/aws/aws-sdk-java/pull/459
 * for reference
 */
@Test
public void receiveMessage_WithBinaryAttributeValue_DoesNotChangeStateOfByteBuffer() {
    byte[] bytes = new byte[]{1, 1, 1, 0, 0, 0};
    String byteBufferAttrName = "byte-buffer-attr";
    Map<String, MessageAttributeValue> attrs = ImmutableMap.of(byteBufferAttrName,
                                                               MessageAttributeValue.builder().dataType("Binary").binaryValue(SdkBytes.fromByteArray(bytes)).build());

    sqsAsync.sendMessage(SendMessageRequest.builder().queueUrl(queueUrl).messageBody("test")
            .messageAttributes(attrs)
            .build());
    // Long poll to make sure we get the message back
    List<Message> messages = sqsAsync.receiveMessage(
            ReceiveMessageRequest.builder().queueUrl(queueUrl).messageAttributeNames("All").waitTimeSeconds(20).build()).join()
            .messages();

    ByteBuffer actualByteBuffer = messages.get(0).messageAttributes().get(byteBufferAttrName).binaryValue().asByteBuffer();
    assertEquals(bytes.length, actualByteBuffer.remaining());
}
 
Example #3
Source File: MessageMD5ChecksumInterceptorTest.java    From aws-sdk-java-v2 with Apache License 2.0 6 votes vote down vote up
@Test
public void receiveMessageFailsInvalidBodyChecksum() {
    Message message = Message.builder()
                             .body(messageBody())
                             .messageAttributes(messageAttributes())
                             .md5OfBody(messageBodyChecksum())
                             .md5OfMessageAttributes(messageAttributesChecksum())
                             .build();
    Message badMessage = Message.builder()
                                .body(messageBody())
                                .messageAttributes(messageAttributes())
                                .md5OfBody("bad")
                                .md5OfMessageAttributes(messageAttributesChecksum())
                                .build();

    ReceiveMessageResponse response = ReceiveMessageResponse.builder()
                                                            .messages(message, badMessage)
                                                            .build();

    assertFailure(ReceiveMessageRequest.builder().build(), response);
}
 
Example #4
Source File: MessageMD5ChecksumInterceptorTest.java    From aws-sdk-java-v2 with Apache License 2.0 6 votes vote down vote up
@Test
public void receiveMessageFailsInvalidAttributeChecksum() {
    Message message = Message.builder()
                             .body(messageBody())
                             .messageAttributes(messageAttributes())
                             .md5OfBody(messageBodyChecksum())
                             .md5OfMessageAttributes(messageAttributesChecksum())
                             .build();
    Message badMessage = Message.builder()
                                .body(messageBody())
                                .messageAttributes(messageAttributes())
                                .md5OfBody(messageBodyChecksum())
                                .md5OfMessageAttributes("bad")
                                .build();

    ReceiveMessageResponse response = ReceiveMessageResponse.builder()
                                                            .messages(message, badMessage)
                                                            .build();

    assertFailure(ReceiveMessageRequest.builder().build(), response);
}
 
Example #5
Source File: VisibilityTimeout.java    From aws-doc-sdk-examples with Apache License 2.0 6 votes vote down vote up
public static void changeMessageVisibilitySingle(
        String queueName, int timeout) {
    SqsClient sqs = SqsClient.builder().build();

    // Get the receipt handle for the first message in the queue
    ReceiveMessageRequest receiveRequest = ReceiveMessageRequest.builder()
            .queueUrl(queueName)
            .build();
    String receipt = sqs.receiveMessage(receiveRequest)
            .messages()
            .get(0)
            .receiptHandle();

    ChangeMessageVisibilityRequest visibilityRequest = ChangeMessageVisibilityRequest.builder()
            .queueUrl(queueName)
            .receiptHandle(receipt)
            .visibilityTimeout(timeout)
            .build();
    sqs.changeMessageVisibility(visibilityRequest);
}
 
Example #6
Source File: SqsUnboundedReader.java    From beam with Apache License 2.0 6 votes vote down vote up
private void pull() {
  final ReceiveMessageRequest receiveMessageRequest =
      ReceiveMessageRequest.builder()
          .maxNumberOfMessages(MAX_NUMBER_OF_MESSAGES)
          .attributeNamesWithStrings(
              MessageSystemAttributeName.APPROXIMATE_FIRST_RECEIVE_TIMESTAMP.toString())
          .queueUrl(source.getRead().queueUrl())
          .build();

  final ReceiveMessageResponse receiveMessageResponse =
      source.getSqs().receiveMessage(receiveMessageRequest);

  final List<Message> messages = receiveMessageResponse.messages();

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

  messagesNotYetRead.addAll(messages);
}
 
Example #7
Source File: MessageAttributesIntegrationTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Test
public void receiveMessage_WithAllAttributesRequested_ReturnsAttributes() throws Exception {
    sendTestMessage();

    ReceiveMessageRequest receiveMessageRequest = ReceiveMessageRequest.builder().queueUrl(queueUrl).waitTimeSeconds(5)
            .visibilityTimeout(0).messageAttributeNames("All").build();
    ReceiveMessageResponse receiveMessageResult = sqsAsync.receiveMessage(receiveMessageRequest).join();

    assertFalse(receiveMessageResult.messages().isEmpty());
    Message message = receiveMessageResult.messages().get(0);
    assertEquals(MESSAGE_BODY, message.body());
    assertNotEmpty(message.md5OfBody());
    assertNotEmpty(message.md5OfMessageAttributes());
}
 
Example #8
Source File: MessageAttributesIntegrationTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
/**
 * Tests SQS operations that involve message attributes checksum.
 */
@Test
public void receiveMessage_WithNoAttributesRequested_DoesNotReturnAttributes() throws Exception {
    sendTestMessage();

    ReceiveMessageRequest receiveMessageRequest = ReceiveMessageRequest.builder().queueUrl(queueUrl).waitTimeSeconds(5)
            .visibilityTimeout(0).build();
    ReceiveMessageResponse receiveMessageResult = sqsAsync.receiveMessage(receiveMessageRequest).join();

    assertFalse(receiveMessageResult.messages().isEmpty());
    Message message = receiveMessageResult.messages().get(0);
    assertEquals(MESSAGE_BODY, message.body());
    assertNotEmpty(message.md5OfBody());
    assertNull(message.md5OfMessageAttributes());
}
 
Example #9
Source File: MessageMD5ChecksumInterceptorTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Test
public void receiveMessagePassesValidChecksums() {
    Message message = Message.builder()
                             .body(messageBody())
                             .messageAttributes(messageAttributes())
                             .md5OfBody(messageBodyChecksum())
                             .md5OfMessageAttributes(messageAttributesChecksum())
                             .build();

    ReceiveMessageResponse response = ReceiveMessageResponse.builder()
                                                            .messages(message, message)
                                                            .build();

    assertSuccess(ReceiveMessageRequest.builder().build(), response);
}
 
Example #10
Source File: SNSIntegrationTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
/**
 * Polls the SQS queue created earlier in the test until we find our SNS notification message
 * and returns the base64 decoded message body.
 */
private String receiveMessage() throws InterruptedException {
    int maxRetries = 15;
    while (maxRetries-- > 0) {
        Thread.sleep(1000 * 10);
        List<Message> messages = sqs.receiveMessage(ReceiveMessageRequest.builder().queueUrl(queueUrl).build()).messages();
        if (messages.size() > 0) {
            return new String(messages.get(0).body());
        }
    }

    fail("No SQS messages received after retrying " + maxRetries + "times");
    return null;
}
 
Example #11
Source File: SqsMessageQueueReceiverEndpointIntegrationTest.java    From synapse with Apache License 2.0 5 votes vote down vote up
private ReceiveMessageRequest buildReceiveMessageRequest() throws InterruptedException, ExecutionException {
    return ReceiveMessageRequest.builder()
            .queueUrl(
                    asyncClient.getQueueUrl(GetQueueUrlRequest
                            .builder()
                            .queueName(SQS_INTEGRATION_TEST_CHANNEL)
                            .overrideConfiguration(AwsRequestOverrideConfiguration.builder()
                                    .build())
                            .build()).get().queueUrl())
            .messageAttributeNames(".*")
            .build();
}
 
Example #12
Source File: LongPolling.java    From aws-doc-sdk-examples with Apache License 2.0 5 votes vote down vote up
public static void setLongPoll( SqsClient sqsClient) {

        // Enable long polling when creating a queue
        HashMap<QueueAttributeName, String> attributes = new HashMap<QueueAttributeName, String>();
        attributes.put(QueueAttributeName.RECEIVE_MESSAGE_WAIT_TIME_SECONDS, "20");

        CreateQueueRequest createRequest = CreateQueueRequest.builder()
                .queueName(QueueName)
                .attributes(attributes)
                .build();

        try {
            sqsClient.createQueue(createRequest);

            GetQueueUrlRequest getQueueRequest = GetQueueUrlRequest.builder()
                .queueName(QueueName)
                .build();

            String queueUrl = sqsClient.getQueueUrl(getQueueRequest).queueUrl();

           // Enable long polling on an existing queue
           SetQueueAttributesRequest setAttrsRequest = SetQueueAttributesRequest.builder()
                .queueUrl(queueUrl)
                .attributes(attributes)
                .build();

           sqsClient.setQueueAttributes(setAttrsRequest);

            // Enable long polling on a message receipt
            ReceiveMessageRequest receiveRequest = ReceiveMessageRequest.builder()
                .queueUrl(queueUrl)
                .waitTimeSeconds(20)
                .build();

            sqsClient.receiveMessage(receiveRequest);

        } catch (QueueNameExistsException e) {
            throw e;
        }
    }
 
Example #13
Source File: SQSExample.java    From aws-doc-sdk-examples with Apache License 2.0 5 votes vote down vote up
public static  List<Message> receiveMessages(SqsClient sqsClient, String queueUrl) {


        System.out.println("\nReceive messages");
        // snippet-start:[sqs.java2.sqs_example.retrieve_messages]
        ReceiveMessageRequest receiveMessageRequest = ReceiveMessageRequest.builder()
                .queueUrl(queueUrl)
                .maxNumberOfMessages(5)
                .build();
        List<Message> messages = sqsClient.receiveMessage(receiveMessageRequest).messages();

        return messages;
        // snippet-end:[sqs.java2.sqs_example.retrieve_messages]
    }
 
Example #14
Source File: VisibilityTimeout.java    From aws-doc-sdk-examples with Apache License 2.0 5 votes vote down vote up
public static void changeMessageVisibilityMultiple(String queue_url, int timeout) {
    SqsClient sqs = SqsClient.builder().build();

    List<ChangeMessageVisibilityBatchRequestEntry> entries =
            new ArrayList<ChangeMessageVisibilityBatchRequestEntry>();

    ReceiveMessageRequest receiveRequest = ReceiveMessageRequest.builder()
            .queueUrl(queue_url)
            .build();

    String receipt = sqs.receiveMessage(receiveRequest)
            .messages()
            .get(0)
            .receiptHandle();

    entries.add(ChangeMessageVisibilityBatchRequestEntry.builder()
            .id("unique_id_msg1")
            .receiptHandle(receipt)
            .visibilityTimeout(timeout)
            .build());

    entries.add(ChangeMessageVisibilityBatchRequestEntry.builder()
            .id("unique_id_msg2")
            .receiptHandle(receipt)
            .visibilityTimeout(timeout + 200)
            .build());

    ChangeMessageVisibilityBatchRequest batchRequest = ChangeMessageVisibilityBatchRequest.builder()
            .queueUrl(queue_url)
            .entries(entries)
            .build();
    sqs.changeMessageVisibilityBatch(batchRequest);
}
 
Example #15
Source File: SqsIOTest.java    From beam with Apache License 2.0 5 votes vote down vote up
@Test
public void testWrite() {
  final SqsClient client = EmbeddedSqsServer.getClient();
  final String queueUrl = EmbeddedSqsServer.getQueueUrl();

  List<SendMessageRequest> messages = new ArrayList<>();
  for (int i = 0; i < 100; i++) {
    final SendMessageRequest request =
        SendMessageRequest.builder()
            .queueUrl(queueUrl)
            .messageBody("This is a test " + i)
            .build();
    messages.add(request);
  }

  pipeline
      .apply(Create.of(messages))
      .apply(SqsIO.write().withSqsClientProvider(SqsClientProviderMock.of(client)));
  pipeline.run().waitUntilFinish();

  List<String> received = new ArrayList<>();
  while (received.size() < 100) {
    ReceiveMessageRequest receiveMessageRequest =
        ReceiveMessageRequest.builder().queueUrl(queueUrl).build();
    final ReceiveMessageResponse receiveMessageResponse =
        client.receiveMessage(receiveMessageRequest);

    if (receiveMessageResponse != null) {
      for (Message message : receiveMessageResponse.messages()) {
        received.add(message.body());
      }
    }
  }

  assertEquals(100, received.size());
  for (int i = 0; i < 100; i++) {
    received.contains("This is a test " + i);
  }
}
 
Example #16
Source File: SendReceiveMessages.java    From aws-doc-sdk-examples with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) {

        SqsClient sqsClient = SqsClient.builder()
                .region(Region.US_WEST_2)
                .build();

        try {
            CreateQueueRequest request = CreateQueueRequest.builder()
                    .queueName(QUEUE_NAME)
                    .build();
            CreateQueueResponse createResult = sqsClient.createQueue(request);

            GetQueueUrlRequest getQueueRequest = GetQueueUrlRequest.builder()
                .queueName(QUEUE_NAME)
                .build();

            String queueUrl = sqsClient.getQueueUrl(getQueueRequest).queueUrl();

            SendMessageRequest sendMsgRequest = SendMessageRequest.builder()
                .queueUrl(queueUrl)
                .messageBody("hello world")
                .delaySeconds(5)
                .build();
            sqsClient.sendMessage(sendMsgRequest);

             // Send multiple messages to the queue
            SendMessageBatchRequest sendBatchRequest = SendMessageBatchRequest.builder()
                .queueUrl(queueUrl)
                .entries(
                        SendMessageBatchRequestEntry.builder()
                                .messageBody("Hello from message 1")
                                .id("msg_1")
                                .build()
                        ,
                        SendMessageBatchRequestEntry.builder()
                                .messageBody("Hello from message 2")
                                .delaySeconds(10)
                                .id("msg_2")
                                .build())
                .build();
             sqsClient.sendMessageBatch(sendBatchRequest);

            // Receive messages from the queue
            ReceiveMessageRequest receiveRequest = ReceiveMessageRequest.builder()
                .queueUrl(queueUrl)
                .build();
            List<Message> messages = sqsClient.receiveMessage(receiveRequest).messages();

            // Print out the messages
             for (Message m : messages) {
                System.out.println("\n" +m.body());
            }
        } catch (QueueNameExistsException e) {
            throw e;
        }
    }