Skip to content

Commit

Permalink
GH-3319: Fix NPE in the KafkaListenerEndpointRegistry (#3320)
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
artembilan authored and sobychacko committed Jun 20, 2024
1 parent 211ce02 commit d479032
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public MessageListenerContainer getUnregisteredListenerContainer(String id) {
refreshContextContainers();
return this.unregisteredContainers.get(id);
}
return null;
return container;
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2022 the original author or authors.
* Copyright 2022-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -22,10 +22,13 @@

import org.junit.jupiter.api.Test;

import org.springframework.context.support.GenericApplicationContext;
import org.springframework.kafka.listener.ConcurrentMessageListenerContainer;
import org.springframework.kafka.listener.MessageListenerContainer;

/**
* @author Gary Russell
* @author Artem Bilan
* @since 2.8.9
*
*/
Expand All @@ -47,4 +50,19 @@ void unregister() {
assertThat(unregistered).isSameAs(container);
}

@Test
void verifyUnregisteredListenerContainer() {
KafkaListenerEndpointRegistry registry = new KafkaListenerEndpointRegistry();
GenericApplicationContext applicationContext = new GenericApplicationContext();
ConcurrentMessageListenerContainer<?, ?> listenerContainerMock = mock(ConcurrentMessageListenerContainer.class);
given(listenerContainerMock.getListenerId()).willReturn("testListenerContainer");
applicationContext.registerBean(ConcurrentMessageListenerContainer.class, () -> listenerContainerMock);
applicationContext.refresh();
registry.setApplicationContext(applicationContext);
// Lazy-load from application context
assertThat(registry.getUnregisteredListenerContainer("testListenerContainer")).isNotNull();
// From internal map
assertThat(registry.getUnregisteredListenerContainer("testListenerContainer")).isNotNull();
}

}

0 comments on commit d479032

Please sign in to comment.