GH-2949: Fix retryCount handling in the DefaultMessagePropertiesConverter

Fixes: #2949
Issue link: https://github.com/spring-projects/spring-amqp/issues/2949

* The handling of `retryCount` is moved into `convertHeadersIfNecessary()`,
so that we can still return a `Collections.emptyMap()` if there are no headers
in the source `MessageProperties` and no `retryCount` to add.

Signed-off-by: Johan Kaving <johan@kaving.se>

[artem.bilan@broadcom.com improve commit message]

Signed-off-by: Artem Bilan <artem.bilan@broadcom.com>
(cherry picked from commit f621b86b78)
This commit is contained in:
Johan Kaving
2025-02-04 22:54:46 +01:00
committed by Spring Builds
parent 488c8fb8fc
commit 7751dbc141
2 changed files with 22 additions and 7 deletions

View File

@@ -43,6 +43,7 @@ import org.springframework.util.StringUtils;
* @author Raylax Grey
* @author Artem Bilan
* @author Ngoc Nhan
* @author Johan Kaving
*
* @since 1.0
*/
@@ -155,11 +156,7 @@ public class DefaultMessagePropertiesConverter implements MessagePropertiesConve
@Override
public BasicProperties fromMessageProperties(final MessageProperties source, final String charset) {
BasicProperties.Builder target = new BasicProperties.Builder();
Map<String, Object> headers = convertHeadersIfNecessary(source.getHeaders());
long retryCount = source.getRetryCount();
if (retryCount > 0) {
headers.put(MessageProperties.RETRY_COUNT, retryCount);
}
Map<String, Object> headers = convertHeadersIfNecessary(source);
target.headers(headers)
.timestamp(source.getTimestamp())
.messageId(source.getMessageId())
@@ -186,14 +183,20 @@ public class DefaultMessagePropertiesConverter implements MessagePropertiesConve
return target.build();
}
private Map<String, Object> convertHeadersIfNecessary(Map<String, Object> headers) {
if (CollectionUtils.isEmpty(headers)) {
private Map<String, Object> convertHeadersIfNecessary(MessageProperties source) {
Map<String, Object> headers = source.getHeaders();
long retryCount = source.getRetryCount();
if (CollectionUtils.isEmpty(headers) && retryCount == 0) {
return Collections.emptyMap();
}
Map<String, Object> writableHeaders = new HashMap<>();
for (Map.Entry<String, Object> entry : headers.entrySet()) {
writableHeaders.put(entry.getKey(), this.convertHeaderValueIfNecessary(entry.getValue()));
}
if (retryCount > 0) {
writableHeaders.put(MessageProperties.RETRY_COUNT, retryCount);
}
return writableHeaders;
}

View File

@@ -38,6 +38,7 @@ import static org.assertj.core.api.Assertions.assertThat;
/**
* @author Soeren Unruh
* @author Gary Russell
* @author Johan Kaving
* @since 1.3
*/
public class DefaultMessagePropertiesConverterTests {
@@ -200,6 +201,17 @@ public class DefaultMessagePropertiesConverterTests {
assertThat(basic.getHeaders().get("aClass")).isEqualTo(getClass().getName());
}
@Test
public void testRetryCount() {
MessageProperties props = new MessageProperties();
props.incrementRetryCount();
BasicProperties basic = new DefaultMessagePropertiesConverter().fromMessageProperties(props, "UTF8");
assertThat(basic.getHeaders().get(MessageProperties.RETRY_COUNT)).isEqualTo(1L);
props.incrementRetryCount();
basic = new DefaultMessagePropertiesConverter().fromMessageProperties(props, "UTF8");
assertThat(basic.getHeaders().get(MessageProperties.RETRY_COUNT)).isEqualTo(2L);
}
private static class Foo {
Foo() {