Auto-configure Kafka MessageConverter
See gh-10380
This commit is contained in:
committed by
Stephane Nicoll
parent
41424e4529
commit
d7bc93f278
@@ -20,17 +20,21 @@ import org.springframework.boot.autoconfigure.kafka.KafkaProperties.Listener;
|
||||
import org.springframework.kafka.config.ConcurrentKafkaListenerContainerFactory;
|
||||
import org.springframework.kafka.core.ConsumerFactory;
|
||||
import org.springframework.kafka.listener.config.ContainerProperties;
|
||||
import org.springframework.kafka.support.converter.MessageConverter;
|
||||
|
||||
/**
|
||||
* Configure {@link ConcurrentKafkaListenerContainerFactory} with sensible defaults.
|
||||
*
|
||||
* @author Gary Russell
|
||||
* @author Eddú Meléndez
|
||||
* @since 1.5.0
|
||||
*/
|
||||
public class ConcurrentKafkaListenerContainerFactoryConfigurer {
|
||||
|
||||
private KafkaProperties properties;
|
||||
|
||||
private MessageConverter messageConverter;
|
||||
|
||||
/**
|
||||
* Set the {@link KafkaProperties} to use.
|
||||
* @param properties the properties
|
||||
@@ -39,6 +43,14 @@ public class ConcurrentKafkaListenerContainerFactoryConfigurer {
|
||||
this.properties = properties;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the {@link MessageConverter} to use.
|
||||
* @param messageConverter the message converter
|
||||
*/
|
||||
public void setMessageConverter(MessageConverter messageConverter) {
|
||||
this.messageConverter = messageConverter;
|
||||
}
|
||||
|
||||
/**
|
||||
* Configure the specified Kafka listener container factory. The factory can be
|
||||
* further tuned and default settings can be overridden.
|
||||
@@ -49,6 +61,9 @@ public class ConcurrentKafkaListenerContainerFactoryConfigurer {
|
||||
public void configure(
|
||||
ConcurrentKafkaListenerContainerFactory<Object, Object> listenerContainerFactory,
|
||||
ConsumerFactory<Object, Object> consumerFactory) {
|
||||
if (this.messageConverter != null) {
|
||||
listenerContainerFactory.setMessageConverter(this.messageConverter);
|
||||
}
|
||||
listenerContainerFactory.setConsumerFactory(consumerFactory);
|
||||
Listener container = this.properties.getListener();
|
||||
ContainerProperties containerProperties = listenerContainerFactory
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
|
||||
package org.springframework.boot.autoconfigure.kafka;
|
||||
|
||||
import org.springframework.beans.factory.ObjectProvider;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
@@ -24,11 +25,13 @@ import org.springframework.kafka.annotation.EnableKafka;
|
||||
import org.springframework.kafka.config.ConcurrentKafkaListenerContainerFactory;
|
||||
import org.springframework.kafka.config.KafkaListenerConfigUtils;
|
||||
import org.springframework.kafka.core.ConsumerFactory;
|
||||
import org.springframework.kafka.support.converter.MessageConverter;
|
||||
|
||||
/**
|
||||
* Configuration for Kafka annotation-driven support.
|
||||
*
|
||||
* @author Gary Russell
|
||||
* @author Eddú Meléndez
|
||||
* @since 1.5.0
|
||||
*/
|
||||
@Configuration
|
||||
@@ -37,8 +40,12 @@ class KafkaAnnotationDrivenConfiguration {
|
||||
|
||||
private final KafkaProperties properties;
|
||||
|
||||
KafkaAnnotationDrivenConfiguration(KafkaProperties properties) {
|
||||
private final MessageConverter messageConverter;
|
||||
|
||||
KafkaAnnotationDrivenConfiguration(KafkaProperties properties,
|
||||
ObjectProvider<MessageConverter> messageConverter) {
|
||||
this.properties = properties;
|
||||
this.messageConverter = messageConverter.getIfAvailable();
|
||||
}
|
||||
|
||||
@Bean
|
||||
@@ -46,6 +53,7 @@ class KafkaAnnotationDrivenConfiguration {
|
||||
public ConcurrentKafkaListenerContainerFactoryConfigurer kafkaListenerContainerFactoryConfigurer() {
|
||||
ConcurrentKafkaListenerContainerFactoryConfigurer configurer = new ConcurrentKafkaListenerContainerFactoryConfigurer();
|
||||
configurer.setKafkaProperties(this.properties);
|
||||
configurer.setMessageConverter(this.messageConverter);
|
||||
return configurer;
|
||||
}
|
||||
|
||||
|
||||
@@ -18,6 +18,7 @@ package org.springframework.boot.autoconfigure.kafka;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.springframework.beans.factory.ObjectProvider;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
@@ -36,12 +37,14 @@ import org.springframework.kafka.core.ProducerFactory;
|
||||
import org.springframework.kafka.security.jaas.KafkaJaasLoginModuleInitializer;
|
||||
import org.springframework.kafka.support.LoggingProducerListener;
|
||||
import org.springframework.kafka.support.ProducerListener;
|
||||
import org.springframework.kafka.support.converter.RecordMessageConverter;
|
||||
|
||||
/**
|
||||
* {@link EnableAutoConfiguration Auto-configuration} for Apache Kafka.
|
||||
*
|
||||
* @author Gary Russell
|
||||
* @author Stephane Nicoll
|
||||
* @author Eddú Meléndez
|
||||
* @since 1.5.0
|
||||
*/
|
||||
@Configuration
|
||||
@@ -52,8 +55,12 @@ public class KafkaAutoConfiguration {
|
||||
|
||||
private final KafkaProperties properties;
|
||||
|
||||
public KafkaAutoConfiguration(KafkaProperties properties) {
|
||||
private final RecordMessageConverter messageConverter;
|
||||
|
||||
public KafkaAutoConfiguration(KafkaProperties properties,
|
||||
ObjectProvider<RecordMessageConverter> messageConverter) {
|
||||
this.properties = properties;
|
||||
this.messageConverter = messageConverter.getIfAvailable();
|
||||
}
|
||||
|
||||
@Bean
|
||||
@@ -63,6 +70,9 @@ public class KafkaAutoConfiguration {
|
||||
ProducerListener<Object, Object> kafkaProducerListener) {
|
||||
KafkaTemplate<Object, Object> kafkaTemplate = new KafkaTemplate<>(
|
||||
kafkaProducerFactory);
|
||||
if (this.messageConverter != null) {
|
||||
kafkaTemplate.setMessageConverter(this.messageConverter);
|
||||
}
|
||||
kafkaTemplate.setProducerListener(kafkaProducerListener);
|
||||
kafkaTemplate.setDefaultTopic(this.properties.getTemplate().getDefaultTopic());
|
||||
return kafkaTemplate;
|
||||
|
||||
Reference in New Issue
Block a user