Merge pull request #11502 from garyrussell:GH-11257

* pr/11502:
  Polish "Add support for additional Kafka listener properties"
  Add support for additional Kafka listener properties
This commit is contained in:
Stephane Nicoll 2018-01-05 14:35:36 +01:00
commit 04ae0cd62c
5 changed files with 100 additions and 4 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2018 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.
@ -96,11 +96,18 @@ public class ConcurrentKafkaListenerContainerFactoryConfigurer {
PropertyMapper map = PropertyMapper.get();
Listener properties = this.properties.getListener();
map.from(properties::getAckMode).whenNonNull().to(container::setAckMode);
map.from(properties::getClientId).whenNonNull().to(container::setClientId);
map.from(properties::getAckCount).whenNonNull().to(container::setAckCount);
map.from(properties::getAckTime).whenNonNull().as(Duration::toMillis)
.to(container::setAckTime);
map.from(properties::getPollTimeout).whenNonNull().as(Duration::toMillis)
.to(container::setPollTimeout);
map.from(properties::getNoPollThreshold).whenNonNull().to(container::setNoPollThreshold);
map.from(properties::getIdleEventInterval).whenNonNull().as(Duration::toMillis)
.to(container::setIdleEventInterval);
map.from(properties::getMonitorInterval).whenNonNull().as(Duration::getSeconds)
.as(Number::intValue).to(container::setMonitorInterval);
map.from(properties::getLogContainerConfig).whenNonNull().to(container::setLogContainerConfig);
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2018 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.
@ -18,6 +18,7 @@ package org.springframework.boot.autoconfigure.kafka;
import java.io.IOException;
import java.time.Duration;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
@ -32,6 +33,7 @@ import org.apache.kafka.common.serialization.StringDeserializer;
import org.apache.kafka.common.serialization.StringSerializer;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.bind.convert.DefaultDurationUnit;
import org.springframework.core.io.Resource;
import org.springframework.kafka.listener.AbstractMessageListenerContainer.AckMode;
import org.springframework.kafka.security.jaas.KafkaJaasLoginModuleInitializer;
@ -797,6 +799,11 @@ public class KafkaProperties {
*/
private AckMode ackMode;
/**
* Prefix for the listener's consumer client.id property.
*/
private String clientId;
/**
* Number of threads to run in the listener containers.
*/
@ -807,6 +814,11 @@ public class KafkaProperties {
*/
private Duration pollTimeout;
/**
* Multiplier applied to "pollTimeout" to determine if a consumer is non-responsive.
*/
private Float noPollThreshold;
/**
* Number of records between offset commits when ackMode is "COUNT" or
* "COUNT_TIME".
@ -818,6 +830,23 @@ public class KafkaProperties {
*/
private Duration ackTime;
/**
* Time between publishing idle consumer events (no data received).
*/
private Duration idleEventInterval;
/**
* Time between checks for non-responsive consumers. If a duration suffix is not
* specified, seconds will be used.
*/
@DefaultDurationUnit(ChronoUnit.SECONDS)
private Duration monitorInterval;
/**
* Whether to log the container configuration during initialization (INFO level).
*/
private Boolean logContainerConfig;
public Type getType() {
return this.type;
}
@ -834,6 +863,14 @@ public class KafkaProperties {
this.ackMode = ackMode;
}
public String getClientId() {
return this.clientId;
}
public void setClientId(String clientId) {
this.clientId = clientId;
}
public Integer getConcurrency() {
return this.concurrency;
}
@ -850,6 +887,14 @@ public class KafkaProperties {
this.pollTimeout = pollTimeout;
}
public Float getNoPollThreshold() {
return this.noPollThreshold;
}
public void setNoPollThreshold(Float noPollThreshold) {
this.noPollThreshold = noPollThreshold;
}
public Integer getAckCount() {
return this.ackCount;
}
@ -866,6 +911,30 @@ public class KafkaProperties {
this.ackTime = ackTime;
}
public Duration getIdleEventInterval() {
return this.idleEventInterval;
}
public void setIdleEventInterval(Duration idleEventInterval) {
this.idleEventInterval = idleEventInterval;
}
public Duration getMonitorInterval() {
return this.monitorInterval;
}
public void setMonitorInterval(Duration monitorInterval) {
this.monitorInterval = monitorInterval;
}
public Boolean getLogContainerConfig() {
return this.logContainerConfig;
}
public void setLogContainerConfig(Boolean logContainerConfig) {
this.logContainerConfig = logContainerConfig;
}
}
public static class Ssl {

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2018 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.
@ -254,11 +254,16 @@ public class KafkaAutoConfigurationTests {
this.contextRunner.withUserConfiguration(TestConfiguration.class)
.withPropertyValues("spring.kafka.template.default-topic=testTopic",
"spring.kafka.listener.ack-mode=MANUAL",
"spring.kafka.listener.client-id=client",
"spring.kafka.listener.ack-count=123",
"spring.kafka.listener.ack-time=456",
"spring.kafka.listener.concurrency=3",
"spring.kafka.listener.poll-timeout=2000",
"spring.kafka.listener.no-poll-threshold=2.5",
"spring.kafka.listener.type=batch",
"spring.kafka.listener.idle-event-interval=1s",
"spring.kafka.listener.monitor-interval=45",
"spring.kafka.listener.log-container-config=true",
"spring.kafka.jaas.enabled=true",
"spring.kafka.producer.transaction-id-prefix=foo",
"spring.kafka.jaas.login-module=foo",
@ -285,6 +290,8 @@ public class KafkaAutoConfigurationTests {
.isEqualTo(consumerFactory);
assertThat(dfa.getPropertyValue("containerProperties.ackMode"))
.isEqualTo(AckMode.MANUAL);
assertThat(dfa.getPropertyValue("containerProperties.clientId"))
.isEqualTo("client");
assertThat(dfa.getPropertyValue("containerProperties.ackCount"))
.isEqualTo(123);
assertThat(dfa.getPropertyValue("containerProperties.ackTime"))
@ -292,6 +299,14 @@ public class KafkaAutoConfigurationTests {
assertThat(dfa.getPropertyValue("concurrency")).isEqualTo(3);
assertThat(dfa.getPropertyValue("containerProperties.pollTimeout"))
.isEqualTo(2000L);
assertThat(dfa.getPropertyValue("containerProperties.noPollThreshold"))
.isEqualTo(2.5f);
assertThat(dfa.getPropertyValue("containerProperties.idleEventInterval"))
.isEqualTo(1000L);
assertThat(dfa.getPropertyValue("containerProperties.monitorInterval"))
.isEqualTo(45);
assertThat(dfa.getPropertyValue("containerProperties.logContainerConfig"))
.isEqualTo(Boolean.TRUE);
assertThat(dfa.getPropertyValue("batchListener")).isEqualTo(true);
assertThat(
context.getBeansOfType(KafkaJaasLoginModuleInitializer.class))

View File

@ -149,7 +149,7 @@
<spring-data-releasetrain.version>Kay-SR2</spring-data-releasetrain.version>
<spring-hateoas.version>0.24.0.RELEASE</spring-hateoas.version>
<spring-integration.version>5.0.1.BUILD-SNAPSHOT</spring-integration.version>
<spring-kafka.version>2.1.0.RELEASE</spring-kafka.version>
<spring-kafka.version>2.1.1.BUILD-SNAPSHOT</spring-kafka.version>
<spring-ldap.version>2.3.2.RELEASE</spring-ldap.version>
<spring-plugin.version>1.2.0.RELEASE</spring-plugin.version>
<spring-restdocs.version>2.0.0.RELEASE</spring-restdocs.version>

View File

@ -987,7 +987,12 @@ content into your application. Rather, pick only the properties that you need.
spring.kafka.listener.ack-count= # Number of records between offset commits when ackMode is "COUNT" or "COUNT_TIME".
spring.kafka.listener.ack-mode= # Listener AckMode. See the spring-kafka documentation.
spring.kafka.listener.ack-time= # Time between offset commits when ackMode is "TIME" or "COUNT_TIME".
spring.kafka.listener.client-id= # Prefix for the listener's consumer client.id property.
spring.kafka.listener.concurrency= # Number of threads to run in the listener containers.
spring.kafka.listener.idle-event-interval= # Time between publishing idle consumer events (no data received).
spring.kafka.listener.log-container-config= # Whether to log the container configuration during initialization (INFO level).
spring.kafka.listener.monitor-interval= # Time between checks for non-responsive consumers. If a duration suffix is not specified, seconds will be used.
spring.kafka.listener.no-poll-threshold= # Multiplier applied to "pollTimeout" to determine if a consumer is non-responsive.
spring.kafka.listener.poll-timeout= # Timeout to use when polling the consumer.
spring.kafka.listener.type=single # Listener type.
spring.kafka.producer.acks= # Number of acknowledgments the producer requires the leader to have received before considering a request complete.