Skip to content

Commit 1edbbac

Browse files
garyrussellartembilan
authored andcommitted
GH-2430: Fix Unnecessary describeCluster()
Resolves #2430 Do not obtain the clusterId unless explicitly needed for observation.
1 parent 7290bc1 commit 1edbbac

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

spring-kafka/src/main/java/org/springframework/kafka/listener/KafkaMessageListenerContainer.java

+6-5
Original file line numberDiff line numberDiff line change
@@ -916,21 +916,22 @@ else if (listener instanceof MessageListener) {
916916
this.lastAlertPartition = new HashMap<>();
917917
this.wasIdlePartition = new HashMap<>();
918918
this.kafkaAdmin = obtainAdmin();
919-
obtainClusterId();
920919
}
921920

922921
@Nullable
923922
private KafkaAdmin obtainAdmin() {
924-
ApplicationContext applicationContext = getApplicationContext();
925-
if (applicationContext != null) {
926-
return applicationContext.getBeanProvider(KafkaAdmin.class).getIfUnique();
923+
if (this.containerProperties.isObservationEnabled()) {
924+
ApplicationContext applicationContext = getApplicationContext();
925+
if (applicationContext != null) {
926+
return applicationContext.getBeanProvider(KafkaAdmin.class).getIfUnique();
927+
}
927928
}
928929
return null;
929930
}
930931

931932
@Nullable
932933
private String clusterId() {
933-
if (this.clusterId == null && this.kafkaAdmin != null) {
934+
if (this.kafkaAdmin != null && this.clusterId == null) {
934935
obtainClusterId();
935936
}
936937
return this.clusterId;

spring-kafka/src/test/java/org/springframework/kafka/annotation/BatchListenerConversionTests.java

+12-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
package org.springframework.kafka.annotation;
1818

1919
import static org.assertj.core.api.Assertions.assertThat;
20+
import static org.mockito.Mockito.never;
21+
import static org.mockito.Mockito.spy;
22+
import static org.mockito.Mockito.verify;
2023

2124
import java.util.ArrayList;
2225
import java.util.Collection;
@@ -28,6 +31,7 @@
2831
import java.util.concurrent.TimeUnit;
2932
import java.util.stream.Collectors;
3033

34+
import org.apache.kafka.clients.admin.AdminClientConfig;
3135
import org.apache.kafka.clients.consumer.ConsumerConfig;
3236
import org.apache.kafka.common.serialization.ByteArraySerializer;
3337
import org.apache.kafka.common.serialization.BytesDeserializer;
@@ -44,6 +48,7 @@
4448
import org.springframework.kafka.config.KafkaListenerContainerFactory;
4549
import org.springframework.kafka.core.DefaultKafkaConsumerFactory;
4650
import org.springframework.kafka.core.DefaultKafkaProducerFactory;
51+
import org.springframework.kafka.core.KafkaAdmin;
4752
import org.springframework.kafka.core.KafkaTemplate;
4853
import org.springframework.kafka.core.ProducerFactory;
4954
import org.springframework.kafka.listener.BatchListenerFailedException;
@@ -110,7 +115,7 @@ private void doTest(Listener listener, String topic) throws InterruptedException
110115
}
111116

112117
@Test
113-
public void testBatchOfPojoMessages() throws Exception {
118+
public void testBatchOfPojoMessages(@Autowired KafkaAdmin admin) throws Exception {
114119
String topic = "blc3";
115120
this.template.send(new GenericMessage<>(
116121
new Foo("bar"), Collections.singletonMap(KafkaHeaders.TOPIC, topic)));
@@ -119,6 +124,7 @@ public void testBatchOfPojoMessages() throws Exception {
119124
assertThat(listener.received.size()).isGreaterThan(0);
120125
assertThat(listener.received.get(0).getPayload()).isInstanceOf(Foo.class);
121126
assertThat(listener.received.get(0).getPayload().getBar()).isEqualTo("bar");
127+
verify(admin, never()).clusterId();
122128
}
123129

124130
@Test
@@ -152,6 +158,11 @@ void conversionError() throws InterruptedException {
152158
@EnableKafka
153159
public static class Config {
154160

161+
@Bean
162+
KafkaAdmin admin(EmbeddedKafkaBroker broker) {
163+
return spy(new KafkaAdmin(Map.of(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, broker.getBrokersAsString())));
164+
}
165+
155166
@Bean
156167
public KafkaListenerContainerFactory<?> kafkaListenerContainerFactory(EmbeddedKafkaBroker embeddedKafka,
157168
KafkaTemplate<Integer, Object> template) {

0 commit comments

Comments
 (0)