Skip to content

Commit

Permalink
Fix setting timeout in seconds in SqsTemplate (#1250)
Browse files Browse the repository at this point in the history
  • Loading branch information
joey authored Dec 11, 2024
1 parent bb9b4a2 commit c411070
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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(toInt(pollTimeout.toSeconds()));
if (additionalHeaders.containsKey(SqsHeaders.SQS_VISIBILITY_TIMEOUT_HEADER)) {
builder.visibilityTimeout(
getValueAs(additionalHeaders, SqsHeaders.SQS_VISIBILITY_TIMEOUT_HEADER, Duration.class)
.toSecondsPart());
toInt(getValueAs(additionalHeaders, SqsHeaders.SQS_VISIBILITY_TIMEOUT_HEADER, Duration.class)
.toSeconds()));
}
if (additionalHeaders.containsKey(SqsHeaders.SQS_RECEIVE_REQUEST_ATTEMPT_ID_HEADER)) {
builder.receiveRequestAttemptId(
Expand All @@ -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> V getValueAs(Map<String, Object> headers, String headerName, Class<V> valueClass) {
return valueClass.cast(headers.get(headerName));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -920,7 +920,7 @@ void shouldReceiveFromOptions() {
.willReturn(CompletableFuture.completedFuture(deleteResponse));
SqsOperations template = SqsTemplate.newSyncTemplate(mockClient);
Optional<Message<String>> 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 -> {
Expand All @@ -932,8 +932,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
Expand Down

0 comments on commit c411070

Please sign in to comment.