Skip to content
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

Using retryCount results in UnsupportedOperationException #2949

Closed
jkaving opened this issue Feb 4, 2025 · 0 comments · Fixed by #2950
Closed

Using retryCount results in UnsupportedOperationException #2949

jkaving opened this issue Feb 4, 2025 · 0 comments · Fixed by #2950

Comments

@jkaving
Copy link
Contributor

jkaving commented Feb 4, 2025

In what version(s) of Spring AMQP are you seeing this issue?

From 3.2.0

Describe the bug

Following the retry algorithm example here (which uses the new retryCount message property) can result in an UnsupportedOperationException being thrown from DefaultMessagePropertiesConverter.fromMessageProperties().
This happens if the message doesn't have any other headers.

DefaultMessagePropertiesConverter.fromMessageProperties() currently does this:

...
Map<String, Object> headers = convertHeadersIfNecessary(source.getHeaders());
long retryCount = source.getRetryCount();
if (retryCount > 0) {
	headers.put(MessageProperties.RETRY_COUNT, retryCount);
}
...

The problem is that convertHeadersIfNecessary() does this if the source message has no headers:

...
if (CollectionUtils.isEmpty(headers)) {
	return Collections.emptyMap();
}
...

Collections.emptyMap() returns an unmodifiable Map (more specifically an instance of Collections.EmptyMap) so headers.put() will then result in an UnsupportedOperationException.

To Reproduce

Create a MessageProperties instance, increment the retry count and convert the message using DefaultMessagePropertiesConverter:

MessageProperties props = new MessageProperties();
props.incrementRetryCount();
new DefaultMessagePropertiesConverter().fromMessageProperties(props, "UTF8");

Expected behavior

It should be possible to use retryCount even if the message doesn't have other headers.

jkaving added a commit to jkaving/spring-amqp that referenced this issue Feb 4, 2025
Fixes spring-projects#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 <[email protected]>
@artembilan artembilan modified the milestones: 4.0.0-M1, 3.2.3 Feb 4, 2025
@artembilan artembilan modified the milestones: 3.2.3, 4.0.0-M1 Feb 4, 2025
spring-builds pushed a commit that referenced this issue Feb 4, 2025
…erter`

Fixes: #2949
Issue link: #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 <[email protected]>

[[email protected] improve commit message]

Signed-off-by: Artem Bilan <[email protected]>
(cherry picked from commit f621b86)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants