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

The following examples show how to use software.amazon.awssdk.services.sqs.model.Message. 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: 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 #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: SqsUnboundedReader.java    From beam with Apache License 2.0 6 votes vote down vote up
void delete(final Collection<Message> messages) {
  for (Message message : messages) {
    if (messagesToDelete.contains(message)) {
      DeleteMessageRequest deleteMessageRequest =
          DeleteMessageRequest.builder()
              .queueUrl(source.getRead().queueUrl())
              .receiptHandle(message.receiptHandle())
              .build();

      source.getSqs().deleteMessage(deleteMessageRequest);
      Instant currentMessageTimestamp =
          getTimestamp(
              message
                  .attributes()
                  .get(MessageSystemAttributeName.APPROXIMATE_FIRST_RECEIVE_TIMESTAMP));
      if (currentMessageTimestamp.isAfter(oldestPendingTimestamp)) {
        oldestPendingTimestamp = currentMessageTimestamp;
      }
    }
  }
}
 
Example #4
Source File: SqsUnboundedReader.java    From beam with Apache License 2.0 6 votes vote down vote up
@Override
public boolean advance() {
  if (messagesNotYetRead.isEmpty()) {
    pull();
  }

  Message orgMsg = messagesNotYetRead.poll();
  if (orgMsg != null) {
    String timeStamp =
        orgMsg.attributes().get(MessageSystemAttributeName.APPROXIMATE_FIRST_RECEIVE_TIMESTAMP);
    current = SqsMessage.create(orgMsg.body(), orgMsg.messageId(), timeStamp);
  } else {
    return false;
  }

  messagesToDelete.add(orgMsg);

  Instant currentMessageTimestamp = getCurrentTimestamp();
  if (getCurrentTimestamp().isBefore(oldestPendingTimestamp)) {
    oldestPendingTimestamp = currentMessageTimestamp;
  }

  return true;
}
 
Example #5
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 #6
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 #7
Source File: QuarksShieldSyncResource.java    From quarkus-quickstarts with Apache License 2.0 5 votes vote down vote up
@GET
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public List<Quark> receive() {
    List<Message> messages = sqs.receiveMessage(m -> m.maxNumberOfMessages(10).queueUrl(queueUrl)).messages();

    return messages.stream()
        .map(Message::body)
        .map(this::toQuark)
        .collect(Collectors.toList());
}
 
Example #8
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 #9
Source File: SQSExample.java    From aws-doc-sdk-examples with Apache License 2.0 5 votes vote down vote up
public static void deleteMessages(SqsClient sqsClient, String queueUrl,  List<Message> messages) {
     System.out.println("\nDelete messages");
     // snippet-start:[sqs.java2.sqs_example.delete_message]
     for (Message message : messages) {
         DeleteMessageRequest deleteMessageRequest = DeleteMessageRequest.builder()
                 .queueUrl(queueUrl)
                 .receiptHandle(message.receiptHandle())
                 .build();
         sqsClient.deleteMessage(deleteMessageRequest);
     }
     // snippet-end:[sqs.java2.sqs_example.delete_message]
}
 
Example #10
Source File: SQSExample.java    From aws-doc-sdk-examples with Apache License 2.0 5 votes vote down vote up
public static void changeMessages(SqsClient sqsClient, String queueUrl, List<Message> messages) {

        System.out.println("\nChange message visibility");
        for (Message message : messages) {
            ChangeMessageVisibilityRequest req = ChangeMessageVisibilityRequest.builder()
                    .queueUrl(queueUrl)
                    .receiptHandle(message.receiptHandle())
                    .visibilityTimeout(100)
                    .build();
            sqsClient.changeMessageVisibility(req);
        }

    }
 
Example #11
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 #12
Source File: SQSExample.java    From aws-doc-sdk-examples with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) {
    String queueName = "queue" + System.currentTimeMillis();

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

    // Create a queue
    String queueUrl= createQueue(sqsClient, queueName );
    listQueues(sqsClient);
    listQueuesFilter(sqsClient, queueUrl);
    List<Message> messages = receiveMessages(sqsClient, queueUrl);
    changeMessages(sqsClient, queueUrl, messages);
    deleteMessages(sqsClient, queueUrl,  messages) ;
}
 
Example #13
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 #14
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 #15
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 #16
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 #17
Source File: MessageMD5ChecksumInterceptor.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
/**
 * Throw an exception if the MD5 checksums included in the ReceiveMessageResponse do not match the
 * client-side calculation on the received messages.
 */
private static void receiveMessageResultMd5Check(ReceiveMessageResponse receiveMessageResult) {
    if (receiveMessageResult.messages() != null) {
        for (Message messageReceived : receiveMessageResult.messages()) {
            String messageBody = messageReceived.body();
            String bodyMd5Returned = messageReceived.md5OfBody();
            String clientSideBodyMd5 = calculateMessageBodyMd5(messageBody);
            if (!clientSideBodyMd5.equals(bodyMd5Returned)) {
                throw SdkClientException.builder()
                                        .message(String.format(MD5_MISMATCH_ERROR_MESSAGE, MESSAGE_BODY,
                                                              clientSideBodyMd5, bodyMd5Returned))
                                        .build();
            }

            Map<String, MessageAttributeValue> messageAttr = messageReceived.messageAttributes();
            if (messageAttr != null && !messageAttr.isEmpty()) {
                String attrMd5Returned = messageReceived.md5OfMessageAttributes();
                String clientSideAttrMd5 = calculateMessageAttributesMd5(messageAttr);
                if (!clientSideAttrMd5.equals(attrMd5Returned)) {
                    throw SdkClientException.builder()
                                            .message(String.format(MD5_MISMATCH_ERROR_MESSAGE, MESSAGE_ATTRIBUTES,
                                                                  clientSideAttrMd5, attrMd5Returned))
                                            .build();
                }
            }
        }
    }
}
 
Example #18
Source File: QuarksShieldAsyncResource.java    From quarkus-quickstarts with Apache License 2.0 5 votes vote down vote up
@GET
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Uni<List<Quark>> receive() {
    return Uni.createFrom()
        .completionStage(sqs.receiveMessage(m -> m.maxNumberOfMessages(10).queueUrl(queueUrl)))
        .onItem().apply(ReceiveMessageResponse::messages)
        .onItem().apply(m -> m.stream().map(Message::body).map(this::toQuark).collect(Collectors.toList()));
}
 
Example #19
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;
        }
    }
 
Example #20
Source File: SqsCheckpointMark.java    From beam with Apache License 2.0 4 votes vote down vote up
SqsCheckpointMark(SqsUnboundedReader reader, Collection<Message> messagesToDelete) {
  this.reader = Optional.of(reader);
  this.messagesToDelete = ImmutableList.copyOf(messagesToDelete);
}
 
Example #21
Source File: SqsCheckpointMark.java    From beam with Apache License 2.0 4 votes vote down vote up
List<Message> getMessagesToDelete() {
  return messagesToDelete;
}
 
Example #22
Source File: MessageCoder.java    From beam with Apache License 2.0 4 votes vote down vote up
@Override
public void encode(Message value, OutputStream outStream) throws IOException {
  StringUtf8Coder.of().encode(value.messageId(), outStream);
  StringUtf8Coder.of().encode(value.body(), outStream);
}
 
Example #23
Source File: MessageCoder.java    From beam with Apache License 2.0 4 votes vote down vote up
@Override
public Message decode(InputStream inStream) throws IOException {
  final String messageId = StringUtf8Coder.of().decode(inStream);
  final String body = StringUtf8Coder.of().decode(inStream);
  return Message.builder().messageId(messageId).body(body).build();
}
 
Example #24
Source File: MessageCoderRegistrar.java    From beam with Apache License 2.0 4 votes vote down vote up
@Override
public List<CoderProvider> getCoderProviders() {
  return ImmutableList.of(
      CoderProviders.forCoder(TypeDescriptor.of(Message.class), MessageCoder.of()));
}
 
Example #25
Source File: AWSQueueUtils.java    From para with Apache License 2.0 4 votes vote down vote up
/**
 * Pulls a number of messages from an SQS queue.
 * @param queueURL the URL of the SQS queue
 * @param numberOfMessages the number of messages to pull
 * @return a list of messages
 */
protected static List<String> pullMessages(String queueURL, int numberOfMessages) {
	List<String> messages = new ArrayList<>();
	if (!StringUtils.isBlank(queueURL)) {
		try {
			int batchSteps = 1;
			final int maxForBatch;
			if ((numberOfMessages > MAX_MESSAGES)) {
				batchSteps = (numberOfMessages / MAX_MESSAGES) + ((numberOfMessages % MAX_MESSAGES > 0) ? 1 : 0);
				maxForBatch = MAX_MESSAGES;
			} else {
				maxForBatch = numberOfMessages;
			}

			for (int i = 0; i < batchSteps; i++) {
				List<Message> list = getClient().receiveMessage(b -> b.queueUrl(queueURL).
						maxNumberOfMessages(maxForBatch).
						waitTimeSeconds(River.POLLING_INTERVAL)).get().messages();

				if (list != null && !list.isEmpty()) {
					List<DeleteMessageBatchRequestEntry> del = new ArrayList<>();
					for (Message msg : list) {
						messages.add(msg.body());
						del.add(DeleteMessageBatchRequestEntry.builder().
								id(msg.messageId()).receiptHandle(msg.receiptHandle()).build());
					}
					getClient().deleteMessageBatch(b -> b.queueUrl(queueURL).entries(del));
				}
			}
		} catch (AwsServiceException ase) {
			logException(ase);
		} catch (SdkException ace) {
			logger.error("Could not reach SQS. {}", ace.toString());
		} catch (ExecutionException ee) {
			logger.error("SQS Execution exception. {}", ee.toString());
		} catch (InterruptedException ex) {
			logger.error("Interrupted while pulling messages from queue!", ex);
			Thread.currentThread().interrupt();
		}
	}
	return messages;
}