Skip to content

Commit 8da52f5

Browse files
correct many defects in DefaultKafkaProducerFactory#updateConfigs()
1 parent e47fb3d commit 8da52f5

File tree

2 files changed

+27
-15
lines changed

2 files changed

+27
-15
lines changed

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

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.util.Iterator;
2424
import java.util.List;
2525
import java.util.Map;
26+
import java.util.Objects;
2627
import java.util.concurrent.BlockingQueue;
2728
import java.util.concurrent.ConcurrentHashMap;
2829
import java.util.concurrent.Future;
@@ -110,6 +111,7 @@
110111
* @author Artem Bilan
111112
* @author Chris Gilbert
112113
* @author Thomas Strauß
114+
* @author Nathan Xu
113115
*/
114116
public class DefaultKafkaProducerFactory<K, V> extends KafkaResourceFactory
115117
implements ProducerFactory<K, V>, ApplicationContextAware,
@@ -660,23 +662,31 @@ public boolean removePostProcessor(ProducerPostProcessor<K, V> postProcessor) {
660662

661663
@Override
662664
public void updateConfigs(Map<String, Object> updates) {
663-
updates.entrySet().forEach(entry -> {
664-
if (entry.getKey().equals(ProducerConfig.TRANSACTIONAL_ID_CONFIG)) {
665-
Assert.isTrue(entry.getValue() instanceof String, () -> "'" + ProducerConfig.TRANSACTIONAL_ID_CONFIG
666-
+ "' must be a String, not a " + entry.getClass().getName());
667-
Assert.isTrue(this.transactionIdPrefix != null
668-
? entry.getValue() != null
669-
: entry.getValue() == null,
670-
"Cannot change transactional capability");
671-
this.transactionIdPrefix = (String) entry.getValue();
665+
updates.forEach((key, value) -> {
666+
if (key == null) { // ConcurrentHashMap doesn't accept null key
667+
return;
672668
}
673-
else if (entry.getKey().equals(ProducerConfig.CLIENT_ID_CONFIG)) {
674-
Assert.isTrue(entry.getValue() instanceof String, () -> "'" + ProducerConfig.CLIENT_ID_CONFIG
675-
+ "' must be a String, not a " + entry.getClass().getName());
676-
this.clientIdPrefix = (String) entry.getValue();
669+
if (key.equals(ProducerConfig.TRANSACTIONAL_ID_CONFIG)) {
670+
Assert.isTrue(
671+
value == null || value instanceof String,
672+
() -> "'" + ProducerConfig.TRANSACTIONAL_ID_CONFIG
673+
+ "' must be null or a String, not a " + Objects.requireNonNull(value)
674+
.getClass()
675+
.getName()
676+
);
677+
Assert.isTrue(
678+
(this.transactionIdPrefix != null) == (value != null),
679+
"Cannot change transactional capability"
680+
);
681+
this.transactionIdPrefix = (String) value;
677682
}
678-
else {
679-
this.configs.put(entry.getKey(), entry.getValue());
683+
else if (key.equals(ProducerConfig.CLIENT_ID_CONFIG)) {
684+
Assert.isTrue(value == null || value instanceof String, () -> "'" + ProducerConfig.CLIENT_ID_CONFIG
685+
+ "' must be null or a String, not a " + Objects.requireNonNull(value).getClass().getName());
686+
this.clientIdPrefix = (String) value;
687+
}
688+
else if (value != null) { // ConcurrentHashMap doesn't accept null value
689+
this.configs.put(key, value);
680690
}
681691
});
682692
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.springframework.kafka.core;
1818

1919
import static org.assertj.core.api.Assertions.assertThat;
20+
import static org.assertj.core.api.Assertions.assertThatCode;
2021
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
2122
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
2223
import static org.mockito.ArgumentMatchers.any;
@@ -545,6 +546,7 @@ void configUpdates() {
545546
assertThat(KafkaTestUtils.getPropertyValue(pf1, "transactionIdPrefix")).isEqualTo("tx2-");
546547
configs.put(ProducerConfig.TRANSACTIONAL_ID_CONFIG, null);
547548
assertThatIllegalArgumentException().isThrownBy(() -> pf1.updateConfigs(configs));
549+
assertThatCode(() -> pf1.updateConfigs(Collections.singletonMap(null, null))).doesNotThrowAnyException();
548550
}
549551

550552
@SuppressWarnings("unchecked")

0 commit comments

Comments
 (0)