Commit 2537a0a7 authored by Stephane Nicoll's avatar Stephane Nicoll

Polish "Auto-configure Kafka MessageConverter"

Closes gh-10380
parent d7bc93f2
......@@ -20,7 +20,7 @@ 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;
import org.springframework.kafka.support.converter.RecordMessageConverter;
/**
* Configure {@link ConcurrentKafkaListenerContainerFactory} with sensible defaults.
......@@ -33,7 +33,7 @@ public class ConcurrentKafkaListenerContainerFactoryConfigurer {
private KafkaProperties properties;
private MessageConverter messageConverter;
private RecordMessageConverter messageConverter;
/**
* Set the {@link KafkaProperties} to use.
......@@ -44,10 +44,10 @@ public class ConcurrentKafkaListenerContainerFactoryConfigurer {
}
/**
* Set the {@link MessageConverter} to use.
* Set the {@link RecordMessageConverter} to use.
* @param messageConverter the message converter
*/
public void setMessageConverter(MessageConverter messageConverter) {
void setMessageConverter(RecordMessageConverter messageConverter) {
this.messageConverter = messageConverter;
}
......@@ -61,10 +61,10 @@ public class ConcurrentKafkaListenerContainerFactoryConfigurer {
public void configure(
ConcurrentKafkaListenerContainerFactory<Object, Object> listenerContainerFactory,
ConsumerFactory<Object, Object> consumerFactory) {
listenerContainerFactory.setConsumerFactory(consumerFactory);
if (this.messageConverter != null) {
listenerContainerFactory.setMessageConverter(this.messageConverter);
}
listenerContainerFactory.setConsumerFactory(consumerFactory);
Listener container = this.properties.getListener();
ContainerProperties containerProperties = listenerContainerFactory
.getContainerProperties();
......
......@@ -25,7 +25,7 @@ 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;
import org.springframework.kafka.support.converter.RecordMessageConverter;
/**
* Configuration for Kafka annotation-driven support.
......@@ -40,12 +40,12 @@ class KafkaAnnotationDrivenConfiguration {
private final KafkaProperties properties;
private final MessageConverter messageConverter;
private final RecordMessageConverter messageConverter;
KafkaAnnotationDrivenConfiguration(KafkaProperties properties,
ObjectProvider<MessageConverter> messageConverter) {
ObjectProvider<RecordMessageConverter> messageConverter) {
this.properties = properties;
this.messageConverter = messageConverter.getIfAvailable();
this.messageConverter = messageConverter.getIfUnique();
}
@Bean
......
......@@ -60,7 +60,7 @@ public class KafkaAutoConfiguration {
public KafkaAutoConfiguration(KafkaProperties properties,
ObjectProvider<RecordMessageConverter> messageConverter) {
this.properties = properties;
this.messageConverter = messageConverter.getIfAvailable();
this.messageConverter = messageConverter.getIfUnique();
}
@Bean
......
......@@ -4846,6 +4846,8 @@ public class MyBean {
}
----
NOTE: If a `RecordMessageConverter` bean is defined, it is associated automatically to the
auto-configured `KafkaTemplate`.
[[boot-features-kafka-receiving-a-message]]
......@@ -4853,7 +4855,8 @@ public class MyBean {
When the Apache Kafka infrastructure is present, any bean can be annotated with
`@KafkaListener` to create a listener endpoint. If no `KafkaListenerContainerFactory`
has been defined, a default one is configured automatically with keys defined in
`spring.kafka.listener.*`.
`spring.kafka.listener.*`. Also, if a `RecordMessageConverter` bean is defined, it is
associated automatically to the default factory.
The following component creates a listener endpoint on the `someTopic` topic:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment