Skip to content

Using retryCount results in UnsupportedOperationException #2949

Closed
@jkaving

Description

@jkaving

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions