Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.listener.AfterRollbackProcessor;
import org.springframework.kafka.listener.BatchErrorHandler;
import org.springframework.kafka.listener.ConsumerAwareRebalanceListener;
import org.springframework.kafka.listener.ContainerProperties;
import org.springframework.kafka.listener.ErrorHandler;
import org.springframework.kafka.support.converter.MessageConverter;
Expand Down Expand Up @@ -53,6 +54,8 @@ public class ConcurrentKafkaListenerContainerFactoryConfigurer {

private AfterRollbackProcessor<Object, Object> afterRollbackProcessor;

private ConsumerAwareRebalanceListener rebalanceListener;

/**
* Set the {@link KafkaProperties} to use.
* @param properties the properties
Expand Down Expand Up @@ -111,6 +114,15 @@ void setAfterRollbackProcessor(
this.afterRollbackProcessor = afterRollbackProcessor;
}

/**
* Set the {@link ConsumerAwareRebalanceListener} to use.
* @param rebalanceListener the rebalance listener.
* @since 2.2
*/
void setRebalanceListener(ConsumerAwareRebalanceListener rebalanceListener) {
this.rebalanceListener = rebalanceListener;
}

/**
* Configure the specified Kafka listener container factory. The factory can be
* further tuned and default settings can be overridden.
Expand Down Expand Up @@ -159,6 +171,7 @@ private void configureContainer(ContainerProperties container) {
.as(Number::intValue).to(container::setMonitorInterval);
map.from(properties::getLogContainerConfig).to(container::setLogContainerConfig);
map.from(this.transactionManager).to(container::setTransactionManager);
map.from(this.rebalanceListener).to(container::setConsumerRebalanceListener);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.listener.AfterRollbackProcessor;
import org.springframework.kafka.listener.BatchErrorHandler;
import org.springframework.kafka.listener.ConsumerAwareRebalanceListener;
import org.springframework.kafka.listener.ErrorHandler;
import org.springframework.kafka.support.converter.BatchMessageConverter;
import org.springframework.kafka.support.converter.BatchMessagingMessageConverter;
Expand Down Expand Up @@ -63,14 +64,17 @@ class KafkaAnnotationDrivenConfiguration {

private final AfterRollbackProcessor<Object, Object> afterRollbackProcessor;

private final ConsumerAwareRebalanceListener rebalanceListener;

KafkaAnnotationDrivenConfiguration(KafkaProperties properties,
ObjectProvider<RecordMessageConverter> messageConverter,
ObjectProvider<BatchMessageConverter> batchMessageConverter,
ObjectProvider<KafkaTemplate<Object, Object>> kafkaTemplate,
ObjectProvider<KafkaAwareTransactionManager<Object, Object>> kafkaTransactionManager,
ObjectProvider<ErrorHandler> errorHandler,
ObjectProvider<BatchErrorHandler> batchErrorHandler,
ObjectProvider<AfterRollbackProcessor<Object, Object>> afterRollbackProcessor) {
ObjectProvider<AfterRollbackProcessor<Object, Object>> afterRollbackProcessor,
ObjectProvider<ConsumerAwareRebalanceListener> rebalanceListener) {
this.properties = properties;
this.messageConverter = messageConverter.getIfUnique();
this.batchMessageConverter = batchMessageConverter.getIfUnique(
Expand All @@ -80,6 +84,7 @@ class KafkaAnnotationDrivenConfiguration {
this.errorHandler = errorHandler.getIfUnique();
this.batchErrorHandler = batchErrorHandler.getIfUnique();
this.afterRollbackProcessor = afterRollbackProcessor.getIfUnique();
this.rebalanceListener = rebalanceListener.getIfUnique();
}

@Bean
Expand All @@ -95,6 +100,7 @@ public ConcurrentKafkaListenerContainerFactoryConfigurer kafkaListenerContainerF
configurer.setErrorHandler(this.errorHandler);
configurer.setBatchErrorHandler(this.batchErrorHandler);
configurer.setAfterRollbackProcessor(this.afterRollbackProcessor);
configurer.setRebalanceListener(this.rebalanceListener);
return configurer;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import org.springframework.kafka.core.KafkaAdmin;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.listener.AfterRollbackProcessor;
import org.springframework.kafka.listener.ConsumerAwareRebalanceListener;
import org.springframework.kafka.listener.ContainerProperties;
import org.springframework.kafka.listener.ContainerProperties.AckMode;
import org.springframework.kafka.listener.SeekToCurrentBatchErrorHandler;
Expand Down Expand Up @@ -651,6 +652,18 @@ public void testConcurrentKafkaListenerContainerFactoryWithCustomAfterRollbackPr
});
}

@Test
public void testConcurrentKafkaListenerContainerFactoryWithCustomRebalanceListener() {
this.contextRunner.withUserConfiguration(RebalanceListenerConfiguration.class)
.run((context) -> {
ConcurrentKafkaListenerContainerFactory<?, ?> factory = context
.getBean(ConcurrentKafkaListenerContainerFactory.class);
assertThat(factory.getContainerProperties())
.hasFieldOrPropertyWithValue("consumerRebalanceListener",
context.getBean("rebalanceListener"));
});
}

@Test
public void testConcurrentKafkaListenerContainerFactoryWithKafkaTemplate() {
this.contextRunner.run((context) -> {
Expand Down Expand Up @@ -726,6 +739,17 @@ public AfterRollbackProcessor<Object, Object> afterRollbackProcessor() {

}

@Configuration(proxyBeanMethods = false)
protected static class RebalanceListenerConfiguration {

@Bean
public ConsumerAwareRebalanceListener rebalanceListener() {
return new ConsumerAwareRebalanceListener() {
};
}

}

@Configuration(proxyBeanMethods = false)
@EnableKafkaStreams
protected static class EnableKafkaStreamsConfiguration {
Expand Down