Skip to content

Read x-death count in a safer way #3006

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

Conversation

raul-avila-ph
Copy link
Contributor

We had an issue in our system trying to consume a message from RabbitMQ that contained an x-death header, with count value typed as Integer. This caused a ClassCastException in

The reason the count value was an int and not a long is that we were storing headers in an internal database as part of a recovery process, and the typing was slightly changed during serialisation / deserialisation.

We propose this pull request to make the reading process of the count field in x-death header safer.

Copy link
Member

@artembilan artembilan left a comment

Choose a reason for hiding this comment

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

The build has failed due to Checkstyle violations:

 Error: eckstyle] [ERROR] /home/runner/work/spring-amqp/spring-amqp/spring-rabbit/src/main/java/org/springframework/amqp/rabbit/support/DefaultMessagePropertiesConverter.java:36:1: Wrong order for 'org.springframework.util.NumberUtils' import. [ImportOrder]

> Task :spring-rabbit:checkstyleMain
Error: eckstyle] [ERROR] /home/runner/work/spring-amqp/spring-amqp/spring-rabbit/src/main/java/org/springframework/amqp/rabbit/support/DefaultMessagePropertiesConverter.java:153:33: 'if' is not followed by whitespace. [WhitespaceAfter]
Error: eckstyle] [ERROR] /home/runner/work/spring-amqp/spring-amqp/spring-rabbit/src/main/java/org/springframework/amqp/rabbit/support/DefaultMessagePropertiesConverter.java:153:33: 'if' is not followed by whitespace. [WhitespaceAround]

Also, please, add your name to the @author list of the affected classes.

In other place of the same class I just do:

					if (value instanceof Number numberValue) {
						target.setRetryCount(numberValue.longValue());
					}

Instead of NumberUtils.
I mean you still have problem with Checkstyle, so might be better to not use it at all 😄

Please, run gradlew check command locally before pushing.

auto-merge was automatically disabled March 12, 2025 13:40

Head branch was pushed to by a user without write access

@raul-avila-ph raul-avila-ph force-pushed the x-death-count-reading branch from 034d987 to eaa38be Compare March 12, 2025 13:42
@raul-avila-ph
Copy link
Contributor Author

Apologies for the slightly messy commit, I had some restrictions in my company laptop and couldn't do a proper build.

Everything sorted now, any comments let me know.

@artembilan artembilan merged commit 1741803 into spring-projects:main Mar 12, 2025
3 checks passed
@artembilan
Copy link
Member

Thank you for contribution; looking forward for more!

spring-builds pushed a commit that referenced this pull request Mar 12, 2025
Fixes: #3006

We had an issue in our system trying to consume a message from RabbitMQ that contained an `x-death` header, with count value typed as `Integer`.
This caused a `ClassCastException`.
The reason the count value was an int and not a long is that we were storing headers in an internal database as part of a recovery process, and the typing was slightly changed during serialisation / deserialisation.

* Use  `target.setRetryCount(numberValue.longValue());` in the `DefaultMessagePropertiesConverter` instead of cast to `long`

Signed-off-by: Raul Avila <[email protected]>

[[email protected] Improve commit message]

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

Successfully merging this pull request may close these issues.

3 participants