Correctly handle null listener in RedisMessageListenerContainer.remove(…)
#3009
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.

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 inRedisMessageListenerContainerby ensuring that it gracefully handlesnulllisteners. When thelistenerparameter 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 thelistenerTopicsandmappingcollections.Description of the problem:
In the current implementation of
RedisMessageListenerContainer.remove(), a potential NullPointerException (NPE) can occur when thelistenerparameter 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 thelistenerisnull.Without proper handling, the method can attempt to remove a
nulllistener or update thelistenerTopicsandmappingin a way that results in an NPE, disrupting the listener removal process.Proposed solution:
This PR updates the
remove()method inRedisMessageListenerContainerto correctly handle cases where thelistenerparameter isnull. Specifically:listenerisnull, all listeners associated with the specified topic are removed.listenerTopicsandmappingcollections 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
nulllisteners and empty mappings more gracefully.Changes:
remove()method inRedisMessageListenerContainerto:listenerparameter isnull.listenerTopicsandmappingaccordingly.CollectionUtils.isEmpty()for safer collection handling.How to reproduce the issue:
The issue can be triggered by calling the
remove()method with anulllistener while there are no listeners associated with the topic inlistenerTopics. This would result in an NPE when trying to update the collections.Testing:
listenerisnull.