Skip to content

GH-2949: Fix retryCount handling #2950

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
* @author Raylax Grey
* @author Artem Bilan
* @author Ngoc Nhan
* @author Johan Kaving
*
* @since 1.0
*/
Expand Down Expand Up @@ -155,11 +156,7 @@ else if (MessageProperties.RETRY_COUNT.equals(key)) {
@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);
}
Comment on lines -158 to -162
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An alternative solution could be to keep the retryCount code here, but add some kind of check inside the if (retryCount > 0) block.

We could e.g., check if headers is empty:

...
if (retryCount > 0) {
	if (headers.isEmpty()) {
		headers = new HashMap<>();
	}
	headers.put(MessageProperties.RETRY_COUNT, retryCount);
}
...

or catch the exception:

...
if (retryCount > 0) {
	try {
		headers.put(MessageProperties.RETRY_COUNT, retryCount);
	} catch (UnsupportedOperationException e) {
		headers = Map.of(MessageProperties.RETRY_COUNT, retryCount);
	}
}
...

Map<String, Object> headers = convertHeadersIfNecessary(source);
target.headers(headers)
.timestamp(source.getTimestamp())
.messageId(source.getMessageId())
Expand All @@ -186,14 +183,20 @@ public BasicProperties fromMessageProperties(final MessageProperties source, fin
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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
/**
* @author Soeren Unruh
* @author Gary Russell
* @author Johan Kaving
* @since 1.3
*/
public class DefaultMessagePropertiesConverterTests {
Expand Down Expand Up @@ -200,6 +201,17 @@ public void testClassHeader() {
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() {
Expand Down