Closed sujl95 closed 1 month ago
Care to attach a stack trace of your NPE?
@mp911de
Hello,
As requested, I'm sharing the stack trace where the NullPointerException (NPE) occurs when using the remove() method in RedisMessageListenerContainer. The issue happens when the listener parameter is null, causing the hashCode() method to be invoked on a null key. Please find the attached stack trace screenshot for further details.
If you need any additional information, feel free to let me know. Thank you!
java.lang.NullPointerException: Cannot invoke "Object.hashCode()" because "key" is null
at java.base/java.util.concurrent.ConcurrentHashMap.get(ConcurrentHashMap.java:936)
at org.springframework.data.redis.listener.RedisMessageListenerContainer.remove(RedisMessageListenerContainer.java:774)
at org.springframework.data.redis.listener.RedisMessageListenerContainer.removeListener(RedisMessageListenerContainer.java:731)
at org.springframework.data.redis.listener.RedisMessageListenerContainer.removeMessageListener(RedisMessageListenerContainer.java:562)
at org.springframework.data.redis.listener.RedisMessageListenerContainer.removeMessageListener(RedisMessageListenerContainer.java:576)
Thank you for your contribution. That's merged, polished, and backported now.
Hello,
While using the remove API, I encountered a NullPointerException (NPE) when the listener parameter was null. This PR addresses that issue by updating the remove method in RedisMessageListenerContainer.
The proposed changes ensure that when the listener is null, all listeners associated with the specified topic are removed. Additionally, the listenerTopics and mapping collections are cleaned up properly to prevent NPEs, making the listener removal process more robust.
If this contribution does not meet any of the project’s contribution requirements, please feel free to let me know, and I’ll be happy to make any necessary adjustments.
Summary:
This PR improves the
remove()
method inRedisMessageListenerContainer
by ensuring that it gracefully handlesnull
listeners. When thelistener
parameter isnull
, the method will now remove all associated listeners for the specified topic. The method has been updated to prevent NullPointerExceptions (NPEs) and ensure proper cleanup of thelistenerTopics
andmapping
collections.Description of the problem: In the current implementation of
RedisMessageListenerContainer.remove()
, a potential NullPointerException (NPE) can occur when thelistener
parameter isnull
. This leads to unexpected behavior when trying to remove listeners for a specific topic, especially when no listeners are associated with the topic, or thelistener
isnull
.Without proper handling, the method can attempt to remove a
null
listener or update thelistenerTopics
andmapping
in a way that results in an NPE, disrupting the listener removal process.Proposed solution: This PR updates the
remove()
method inRedisMessageListenerContainer
to correctly handle cases where thelistener
parameter isnull
. Specifically:listener
isnull
, all listeners associated with the specified topic are removed.listenerTopics
andmapping
collections are updated correctly by checking if collections are empty before removal.CollectionUtils.isEmpty()
is used to ensure proper cleanup of empty collections and avoid NPEs.These changes ensure that the listener removal process is more robust and handles edge cases involving
null
listeners and empty mappings more gracefully.Changes:
remove()
method inRedisMessageListenerContainer
to:listener
parameter isnull
.listenerTopics
andmapping
accordingly.CollectionUtils.isEmpty()
for safer collection handling.How to reproduce the issue: The issue can be triggered by calling the
remove()
method with anull
listener while there are no listeners associated with the topic inlistenerTopics
. This would result in an NPE when trying to update the collections.Testing:
listener
isnull
.