Skip to content

Commit 8879427

Browse files
garyrussellartembilan
authored andcommitted
GH-1678: Allow CF Property Overrides
Resolves #1678 Although the consumer factory can override properties using an overloaded `createConsumer` method, add update/remove methods for properties to be consistent with the `ProducerFactory`. Change maps to concurrent to avoid possible `ConcurrentModificationException`.
1 parent 9101166 commit 8879427

File tree

4 files changed

+38
-6
lines changed

4 files changed

+38
-6
lines changed

spring-kafka/src/main/java/org/springframework/kafka/core/ConsumerFactory.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2020 the original author or authors.
2+
* Copyright 2016-2021 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.
@@ -204,6 +204,23 @@ default List<ConsumerPostProcessor<K, V>> getPostProcessors() {
204204
return Collections.emptyList();
205205
}
206206

207+
/**
208+
* Update the consumer configuration map; useful for situations such as
209+
* credential rotation.
210+
* @param updates the configuration properties to update.
211+
* @since 2.7
212+
*/
213+
default void updateConfigs(Map<String, Object> updates) {
214+
}
215+
216+
/**
217+
* Remove the specified key from the configuration map.
218+
* @param configKey the key to remove.
219+
* @since 2.7
220+
*/
221+
default void removeConfig(String configKey) {
222+
}
223+
207224
/**
208225
* Called whenever a consumer is added or removed.
209226
*

spring-kafka/src/main/java/org/springframework/kafka/core/DefaultKafkaConsumerFactory.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2020 the original author or authors.
2+
* Copyright 2016-2021 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.
@@ -25,6 +25,7 @@
2525
import java.util.Map;
2626
import java.util.Properties;
2727
import java.util.Set;
28+
import java.util.concurrent.ConcurrentHashMap;
2829
import java.util.function.Supplier;
2930

3031
import org.aopalliance.aop.Advice;
@@ -121,7 +122,7 @@ public DefaultKafkaConsumerFactory(Map<String, Object> configs,
121122
@Nullable Supplier<Deserializer<K>> keyDeserializerSupplier,
122123
@Nullable Supplier<Deserializer<V>> valueDeserializerSupplier) {
123124

124-
this.configs = new HashMap<>(configs);
125+
this.configs = new ConcurrentHashMap<>(configs);
125126
this.keyDeserializerSupplier = keyDeserializerSupplier == null ? () -> null : keyDeserializerSupplier;
126127
this.valueDeserializerSupplier = valueDeserializerSupplier == null ? () -> null : valueDeserializerSupplier;
127128
}
@@ -229,6 +230,16 @@ public boolean removeListener(Listener<K, V> listener) {
229230
return this.listeners.remove(listener);
230231
}
231232

233+
@Override
234+
public void updateConfigs(Map<String, Object> updates) {
235+
this.configs.putAll(updates);
236+
}
237+
238+
@Override
239+
public void removeConfig(String configKey) {
240+
this.configs.remove(configKey);
241+
}
242+
232243
@Override
233244
public Consumer<K, V> createConsumer(@Nullable String groupId, @Nullable String clientIdPrefix,
234245
@Nullable String clientIdSuffix) {

spring-kafka/src/main/java/org/springframework/kafka/core/DefaultKafkaProducerFactory.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2020 the original author or authors.
2+
* Copyright 2016-2021 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.
@@ -192,7 +192,7 @@ public DefaultKafkaProducerFactory(Map<String, Object> configs,
192192
@Nullable Supplier<Serializer<K>> keySerializerSupplier,
193193
@Nullable Supplier<Serializer<V>> valueSerializerSupplier) {
194194

195-
this.configs = new HashMap<>(configs);
195+
this.configs = new ConcurrentHashMap<>(configs);
196196
this.keySerializerSupplier = keySerializerSupplier == null ? () -> null : keySerializerSupplier;
197197
this.valueSerializerSupplier = valueSerializerSupplier == null ? () -> null : valueSerializerSupplier;
198198
if (this.clientIdPrefix == null && configs.get(ProducerConfig.CLIENT_ID_CONFIG) instanceof String) {

spring-kafka/src/test/java/org/springframework/kafka/core/DefaultKafkaConsumerFactoryTests.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017-2020 the original author or authors.
2+
* Copyright 2017-2021 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.
@@ -145,6 +145,10 @@ protected KafkaConsumer<String, String> createKafkaConsumer(Map<String, Object>
145145
target.createConsumer(null, null, null, overrides);
146146
assertThat(configPassedToKafkaConsumer.get("config1")).isEqualTo("overridden");
147147
assertThat(configPassedToKafkaConsumer.get("config2")).isSameAs(originalConfig.get("config2"));
148+
target.updateConfigs(Map.of("config1", "newValue"));
149+
assertThat(target.getConfigurationProperties().get("config1")).isEqualTo("newValue");
150+
target.removeConfig("config1");
151+
assertThat(target.getConfigurationProperties().get("config1")).isNull();
148152
}
149153

150154
@Test

0 commit comments

Comments
 (0)