Skip to content

Commit d479032

Browse files
artembilansobychacko
authored andcommitted
GH-3319: Fix NPE in the KafkaListenerEndpointRegistry (#3320)
Fixes: #3319 The `KafkaListenerEndpointRegistry.getUnregisteredListenerContainer()` returns `null` when container is already present in the internal `unregisteredContainers` cache * Fix `KafkaListenerEndpointRegistry.getUnregisteredListenerContainer()` to return a container instance from the `unregisteredContainers` cache (cherry picked from commit 8096c9d)
1 parent 211ce02 commit d479032

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

spring-kafka/src/main/java/org/springframework/kafka/config/KafkaListenerEndpointRegistry.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ public MessageListenerContainer getUnregisteredListenerContainer(String id) {
122122
refreshContextContainers();
123123
return this.unregisteredContainers.get(id);
124124
}
125-
return null;
125+
return container;
126126
}
127127

128128
/**

spring-kafka/src/test/java/org/springframework/kafka/config/KafkaListenerEndpointRegistryTests.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2022 the original author or authors.
2+
* Copyright 2022-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -22,10 +22,13 @@
2222

2323
import org.junit.jupiter.api.Test;
2424

25+
import org.springframework.context.support.GenericApplicationContext;
26+
import org.springframework.kafka.listener.ConcurrentMessageListenerContainer;
2527
import org.springframework.kafka.listener.MessageListenerContainer;
2628

2729
/**
2830
* @author Gary Russell
31+
* @author Artem Bilan
2932
* @since 2.8.9
3033
*
3134
*/
@@ -47,4 +50,19 @@ void unregister() {
4750
assertThat(unregistered).isSameAs(container);
4851
}
4952

53+
@Test
54+
void verifyUnregisteredListenerContainer() {
55+
KafkaListenerEndpointRegistry registry = new KafkaListenerEndpointRegistry();
56+
GenericApplicationContext applicationContext = new GenericApplicationContext();
57+
ConcurrentMessageListenerContainer<?, ?> listenerContainerMock = mock(ConcurrentMessageListenerContainer.class);
58+
given(listenerContainerMock.getListenerId()).willReturn("testListenerContainer");
59+
applicationContext.registerBean(ConcurrentMessageListenerContainer.class, () -> listenerContainerMock);
60+
applicationContext.refresh();
61+
registry.setApplicationContext(applicationContext);
62+
// Lazy-load from application context
63+
assertThat(registry.getUnregisteredListenerContainer("testListenerContainer")).isNotNull();
64+
// From internal map
65+
assertThat(registry.getUnregisteredListenerContainer("testListenerContainer")).isNotNull();
66+
}
67+
5068
}

0 commit comments

Comments
 (0)