From 4bd8bf743928b701b14b5dab3f322b8f94662ae7 Mon Sep 17 00:00:00 2001 From: Joey Echeverria Date: Wed, 2 Oct 2024 17:53:41 -0500 Subject: [PATCH 1/2] fix: Timeout settings in SqsTemplate * Timeouts were being set by calling Duration#getSecondsPart whic is alwawys a number between 0 and 59 * Update to use Duration#getSeconds to get the full timeout specified, in seconds --- .../java/io/awspring/cloud/sqs/operations/SqsTemplate.java | 4 ++-- .../io/awspring/cloud/sqs/operations/SqsTemplateTests.java | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/spring-cloud-aws-sqs/src/main/java/io/awspring/cloud/sqs/operations/SqsTemplate.java b/spring-cloud-aws-sqs/src/main/java/io/awspring/cloud/sqs/operations/SqsTemplate.java index 4c6ff0a23..840d2960d 100644 --- a/spring-cloud-aws-sqs/src/main/java/io/awspring/cloud/sqs/operations/SqsTemplate.java +++ b/spring-cloud-aws-sqs/src/main/java/io/awspring/cloud/sqs/operations/SqsTemplate.java @@ -602,11 +602,11 @@ private ReceiveMessageRequest doCreateReceiveMessageRequest(Duration pollTimeout ReceiveMessageRequest.Builder builder = ReceiveMessageRequest.builder().queueUrl(attributes.getQueueUrl()) .maxNumberOfMessages(maxNumberOfMessages).messageAttributeNames(this.messageAttributeNames) .attributeNamesWithStrings(this.messageSystemAttributeNames) - .waitTimeSeconds(pollTimeout.toSecondsPart()); + .waitTimeSeconds(pollTimeout.toSeconds()); if (additionalHeaders.containsKey(SqsHeaders.SQS_VISIBILITY_TIMEOUT_HEADER)) { builder.visibilityTimeout( getValueAs(additionalHeaders, SqsHeaders.SQS_VISIBILITY_TIMEOUT_HEADER, Duration.class) - .toSecondsPart()); + .toSeconds()); } if (additionalHeaders.containsKey(SqsHeaders.SQS_RECEIVE_REQUEST_ATTEMPT_ID_HEADER)) { builder.receiveRequestAttemptId( diff --git a/spring-cloud-aws-sqs/src/test/java/io/awspring/cloud/sqs/operations/SqsTemplateTests.java b/spring-cloud-aws-sqs/src/test/java/io/awspring/cloud/sqs/operations/SqsTemplateTests.java index f9ab5cec7..a182b2648 100644 --- a/spring-cloud-aws-sqs/src/test/java/io/awspring/cloud/sqs/operations/SqsTemplateTests.java +++ b/spring-cloud-aws-sqs/src/test/java/io/awspring/cloud/sqs/operations/SqsTemplateTests.java @@ -937,7 +937,7 @@ void shouldReceiveFromOptions() { .willReturn(CompletableFuture.completedFuture(deleteResponse)); SqsOperations template = SqsTemplate.newSyncTemplate(mockClient); Optional> receivedMessage = template.receive(from -> from.queue(queue) - .pollTimeout(Duration.ofSeconds(1)).visibilityTimeout(Duration.ofSeconds(5)) + .pollTimeout(Duration.ofSeconds(61)).visibilityTimeout(Duration.ofSeconds(65)) .additionalHeader(headerName1, headerValue1).additionalHeaders(Map.of(headerName2, headerValue2)), String.class); assertThat(receivedMessage).isPresent().hasValueSatisfying(message -> { @@ -949,8 +949,8 @@ void shouldReceiveFromOptions() { then(mockClient).should().receiveMessage(captor.capture()); ReceiveMessageRequest request = captor.getValue(); assertThat(request.maxNumberOfMessages()).isEqualTo(1); - assertThat(request.visibilityTimeout()).isEqualTo(5); - assertThat(request.waitTimeSeconds()).isEqualTo(1); + assertThat(request.visibilityTimeout()).isEqualTo(65); + assertThat(request.waitTimeSeconds()).isEqualTo(61); } @Test From 3ec6b0ac1f9d00d531c9546111fbd27970702236 Mon Sep 17 00:00:00 2001 From: Joey Echeverria Date: Thu, 3 Oct 2024 11:30:25 -0500 Subject: [PATCH 2/2] Convert the long seconds into an int --- .../cloud/sqs/operations/SqsTemplate.java | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/spring-cloud-aws-sqs/src/main/java/io/awspring/cloud/sqs/operations/SqsTemplate.java b/spring-cloud-aws-sqs/src/main/java/io/awspring/cloud/sqs/operations/SqsTemplate.java index 840d2960d..6f555f123 100644 --- a/spring-cloud-aws-sqs/src/main/java/io/awspring/cloud/sqs/operations/SqsTemplate.java +++ b/spring-cloud-aws-sqs/src/main/java/io/awspring/cloud/sqs/operations/SqsTemplate.java @@ -602,11 +602,11 @@ private ReceiveMessageRequest doCreateReceiveMessageRequest(Duration pollTimeout ReceiveMessageRequest.Builder builder = ReceiveMessageRequest.builder().queueUrl(attributes.getQueueUrl()) .maxNumberOfMessages(maxNumberOfMessages).messageAttributeNames(this.messageAttributeNames) .attributeNamesWithStrings(this.messageSystemAttributeNames) - .waitTimeSeconds(pollTimeout.toSeconds()); + .waitTimeSeconds(toInt(pollTimeout.toSeconds())); if (additionalHeaders.containsKey(SqsHeaders.SQS_VISIBILITY_TIMEOUT_HEADER)) { builder.visibilityTimeout( - getValueAs(additionalHeaders, SqsHeaders.SQS_VISIBILITY_TIMEOUT_HEADER, Duration.class) - .toSeconds()); + toInt(getValueAs(additionalHeaders, SqsHeaders.SQS_VISIBILITY_TIMEOUT_HEADER, Duration.class) + .toSeconds())); } if (additionalHeaders.containsKey(SqsHeaders.SQS_RECEIVE_REQUEST_ATTEMPT_ID_HEADER)) { builder.receiveRequestAttemptId( @@ -616,6 +616,15 @@ private ReceiveMessageRequest doCreateReceiveMessageRequest(Duration pollTimeout return builder.build(); } + // Convert a long value to an int. Values larger than Integer.MAX_VALUE are set to Integer.MAX_VALUE + private int toInt(long longValue) { + if (longValue > Integer.MAX_VALUE) { + return Integer.MAX_VALUE; + } + + return (int) longValue; + } + private V getValueAs(Map headers, String headerName, Class valueClass) { return valueClass.cast(headers.get(headerName)); }