Skip to content

Commit 1f7b3ca

Browse files
committed
Polish Kafka properties
Closes gh-7672
1 parent bdda470 commit 1f7b3ca

File tree

4 files changed

+48
-45
lines changed

4 files changed

+48
-45
lines changed

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/kafka/KafkaProperties.java

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import org.springframework.boot.context.properties.ConfigurationProperties;
3434
import org.springframework.core.io.Resource;
3535
import org.springframework.kafka.listener.AbstractMessageListenerContainer.AckMode;
36+
import org.springframework.util.CollectionUtils;
3637

3738
/**
3839
* Configuration properties for Spring for Apache Kafka.
@@ -47,18 +48,6 @@
4748
@ConfigurationProperties(prefix = "spring.kafka")
4849
public class KafkaProperties {
4950

50-
private final Consumer consumer = new Consumer();
51-
52-
private final Producer producer = new Producer();
53-
54-
private final Listener listener = new Listener();
55-
56-
private final Template template = new Template();
57-
58-
private final Ssl ssl = new Ssl();
59-
60-
// Apache Kafka Common Properties
61-
6251
/**
6352
* Comma-delimited list of host:port pairs to use for establishing the initial
6453
* connection to the Kafka cluster.
@@ -76,25 +65,15 @@ public class KafkaProperties {
7665
*/
7766
private Map<String, String> properties = new HashMap<String, String>();
7867

79-
public Consumer getConsumer() {
80-
return this.consumer;
81-
}
68+
private final Consumer consumer = new Consumer();
8269

83-
public Producer getProducer() {
84-
return this.producer;
85-
}
70+
private final Producer producer = new Producer();
8671

87-
public Listener getListener() {
88-
return this.listener;
89-
}
72+
private final Listener listener = new Listener();
9073

91-
public Ssl getSsl() {
92-
return this.ssl;
93-
}
74+
private final Ssl ssl = new Ssl();
9475

95-
public Template getTemplate() {
96-
return this.template;
97-
}
76+
private final Template template = new Template();
9877

9978
public List<String> getBootstrapServers() {
10079
return this.bootstrapServers;
@@ -120,6 +99,26 @@ public void setProperties(Map<String, String> properties) {
12099
this.properties = properties;
121100
}
122101

102+
public Consumer getConsumer() {
103+
return this.consumer;
104+
}
105+
106+
public Producer getProducer() {
107+
return this.producer;
108+
}
109+
110+
public Listener getListener() {
111+
return this.listener;
112+
}
113+
114+
public Ssl getSsl() {
115+
return this.ssl;
116+
}
117+
118+
public Template getTemplate() {
119+
return this.template;
120+
}
121+
123122
private Map<String, Object> buildCommonProperties() {
124123
Map<String, Object> properties = new HashMap<String, Object>();
125124
if (this.bootstrapServers != null) {
@@ -148,7 +147,7 @@ private Map<String, Object> buildCommonProperties() {
148147
properties.put(SslConfigs.SSL_TRUSTSTORE_PASSWORD_CONFIG,
149148
this.ssl.getTruststorePassword());
150149
}
151-
if (this.properties != null && this.properties.size() > 0) {
150+
if (!CollectionUtils.isEmpty(this.properties)) {
152151
properties.putAll(this.properties);
153152
}
154153
return properties;
@@ -163,9 +162,9 @@ private Map<String, Object> buildCommonProperties() {
163162
* instance
164163
*/
165164
public Map<String, Object> buildConsumerProperties() {
166-
Map<String, Object> props = buildCommonProperties();
167-
props.putAll(this.consumer.buildProperties());
168-
return props;
165+
Map<String, Object> properties = buildCommonProperties();
166+
properties.putAll(this.consumer.buildProperties());
167+
return properties;
169168
}
170169

171170
/**
@@ -177,9 +176,9 @@ public Map<String, Object> buildConsumerProperties() {
177176
* instance
178177
*/
179178
public Map<String, Object> buildProducerProperties() {
180-
Map<String, Object> props = buildCommonProperties();
181-
props.putAll(this.producer.buildProperties());
182-
return props;
179+
Map<String, Object> properties = buildCommonProperties();
180+
properties.putAll(this.producer.buildProperties());
181+
return properties;
183182
}
184183

185184
private static String resourceToPath(Resource resource) {
@@ -425,7 +424,8 @@ public Map<String, Object> buildProperties() {
425424
this.valueDeserializer);
426425
}
427426
if (this.maxPollRecords != null) {
428-
properties.put(ConsumerConfig.MAX_POLL_RECORDS_CONFIG, this.maxPollRecords);
427+
properties.put(ConsumerConfig.MAX_POLL_RECORDS_CONFIG,
428+
this.maxPollRecords);
429429
}
430430
return properties;
431431
}

spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/kafka/KafkaAutoConfigurationTests.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,7 @@ public void closeContext() {
6060

6161
@Test
6262
public void consumerProperties() {
63-
load("spring.kafka.bootstrap-servers=foo:1234",
64-
"spring.kafka.properties.foo=bar",
63+
load("spring.kafka.bootstrap-servers=foo:1234", "spring.kafka.properties.foo=bar",
6564
"spring.kafka.properties.baz=qux",
6665
"spring.kafka.properties.foo.bar.baz=qux.fiz.buz",
6766
"spring.kafka.ssl.key-password=p1",
@@ -113,8 +112,7 @@ public void consumerProperties() {
113112
.isEqualTo(LongDeserializer.class);
114113
assertThat(configs.get(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG))
115114
.isEqualTo(IntegerDeserializer.class);
116-
assertThat(configs.get(ConsumerConfig.MAX_POLL_RECORDS_CONFIG))
117-
.isEqualTo(42);
115+
assertThat(configs.get(ConsumerConfig.MAX_POLL_RECORDS_CONFIG)).isEqualTo(42);
118116
assertThat(configs.get("foo")).isEqualTo("bar");
119117
assertThat(configs.get("baz")).isEqualTo("qux");
120118
assertThat(configs.get("foo.bar.baz")).isEqualTo("qux.fiz.buz");

spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4646,9 +4646,12 @@ Only a subset of the properties supported by Kafka are available via the `KafkaP
46464646
class. If you wish to configure the producer or consumer with additional properties that
46474647
are not directly supported, use the following:
46484648

4649-
`spring.kafka.properties.foo.bar=baz`
4649+
[source,properties,indent=0]
4650+
----
4651+
spring.kafka.properties.foo.bar=baz
4652+
----
46504653

4651-
This sets the common `foo.bar` kafka property to `baz`.
4654+
This sets the common `foo.bar` Kafka property to `baz`.
46524655

46534656
These properties will be shared by both the consumer and producer factory beans.
46544657
If you wish to customize these components with different properties, such as to use a
@@ -4659,6 +4662,8 @@ different metrics reader for each, you can override the bean definitions, as fol
46594662
include::{code-examples}/kafka/KafkaSpecialProducerConsumerConfigExample.java[tag=configuration]
46604663
----
46614664

4665+
4666+
46624667
[[boot-features-restclient]]
46634668
== Calling REST services
46644669
If you need to call remote REST services from your application, you can use Spring

spring-boot-docs/src/main/java/org/springframework/boot/kafka/KafkaSpecialProducerConsumerConfigExample.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,11 @@
3232
import org.springframework.kafka.core.ProducerFactory;
3333

3434
/**
35-
* Example custom kafka configuration beans used when the user wants to
36-
* apply different common properties to the producer and consumer.
35+
* Example custom kafka configuration beans used when the user wants to apply different
36+
* common properties to the producer and consumer.
3737
*
3838
* @author Gary Russell
3939
* @since 1.5
40-
*
4140
*/
4241
public class KafkaSpecialProducerConsumerConfigExample {
4342

@@ -65,7 +64,8 @@ public static class CustomKafkaBeans {
6564
*/
6665
@Bean
6766
public ConsumerFactory<?, ?> kafkaConsumerFactory(KafkaProperties properties) {
68-
Map<String, Object> consumererProperties = properties.buildConsumerProperties();
67+
Map<String, Object> consumererProperties = properties
68+
.buildConsumerProperties();
6969
consumererProperties.put(CommonClientConfigs.METRIC_REPORTER_CLASSES_CONFIG,
7070
MyConsumerMetricsReporter.class);
7171
return new DefaultKafkaConsumerFactory<Object, Object>(consumererProperties);

0 commit comments

Comments
 (0)