-
Notifications
You must be signed in to change notification settings - Fork 647
Description
In what version(s) of Spring AMQP are you seeing this issue?
4.0.0
Describe the bug
I encountered a NullPointerException in SimpleAmqpHeaderMapper when receiving messages that do not have the priority property explicitly set (i.e., when priority is null). This appears to be a regression in 4.0.0.
The stack trace points to an unboxing issue during header mapping:
java.lang.NullPointerException: Cannot invoke "java.lang.Integer.intValue()" because "priority" is null
at org.springframework.amqp.support.SimpleAmqpHeaderMapper.toHeaders(SimpleAmqpHeaderMapper.java:155) ~[spring-amqp-4.0.0.jar:4.0.0]
at org.springframework.amqp.support.SimpleAmqpHeaderMapper.toHeaders(SimpleAmqpHeaderMapper.java:57) ~[spring-amqp-4.0.0.jar:4.0.0]
Looking at the source for SimpleAmqpHeaderMapper.java (around line 155), the code attempts to compare a nullable Integer to a primitive int:
Integer priority = amqpMessageProperties.getPriority(); // Returns null if not set
// The condition (priority > 0) causes implicit unboxing of null, throwing NPE
javaUtils.acceptIfCondition(priority > 0, AmqpMessageHeaderAccessor.PRIORITY, priority, putObject)
.acceptIfNotNull(...)Comparison with previous versions:
In spring-amqp:2.4.3, this logic was null-safe:
Integer priority = amqpMessageProperties.getPriority();
javaUtils.acceptIfCondition(priority != null && priority > 0, ...);In 4.0.0, the null check was removed, leading to the regression:
Integer priority = amqpMessageProperties.getPriority();
javaUtils.acceptIfCondition(priority > 0, ...); // NPE if priority is nullTo Reproduce
- Create a Spring Boot 4 application with
spring-boot-starter-amqp. - Define a standard
@RabbitListener. - Publish a message to the queue using
RabbitTemplate(or the Management UI) without setting a specificpriorityproperty (leaving it as the default/null). - The listener container logs an exception and fails to process the message.
Expected behavior
The application should consume the message without error, treating the missing priority as null or 0, consistent with previous versions of Spring AMQP. It should not attempt to unbox a null Integer.