From 5c1e4e203c3c0d7c07b4ce1293a4a120faf1db3c Mon Sep 17 00:00:00 2001 From: Kai Moritz Date: Sun, 17 Jul 2022 19:03:18 +0200 Subject: [PATCH] splitter: 1.0.0-spring-ingetration - message-driven channel-adapter * Instead of the `@InboundChannelAdapter`-annotation, a `KafkaMessageDrivenChannelAdapter` can be instanciated. * The adapter takes a `AbstractMessageListenerContainer` as constructor argument. * The output-channel must be set explicitly on the created instance. * This approach is advantageous, if technical details are important, because the used `MessageListenerContainer` can be configured explicitly. --- .../splitter/SplitterApplication.java | 27 ++++++++++++++----- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/src/main/java/de/juplo/kafka/wordcount/splitter/SplitterApplication.java b/src/main/java/de/juplo/kafka/wordcount/splitter/SplitterApplication.java index 246fffd..3828050 100644 --- a/src/main/java/de/juplo/kafka/wordcount/splitter/SplitterApplication.java +++ b/src/main/java/de/juplo/kafka/wordcount/splitter/SplitterApplication.java @@ -8,14 +8,15 @@ import org.springframework.expression.Expression; import org.springframework.expression.ExpressionParser; import org.springframework.expression.common.LiteralExpression; import org.springframework.expression.spel.standard.SpelExpressionParser; -import org.springframework.integration.annotation.InboundChannelAdapter; import org.springframework.integration.annotation.ServiceActivator; import org.springframework.integration.config.EnableIntegration; -import org.springframework.integration.kafka.inbound.KafkaMessageSource; +import org.springframework.integration.core.MessageProducer; +import org.springframework.integration.kafka.inbound.KafkaMessageDrivenChannelAdapter; import org.springframework.integration.kafka.outbound.KafkaProducerMessageHandler; import org.springframework.kafka.core.ConsumerFactory; import org.springframework.kafka.core.KafkaTemplate; -import org.springframework.kafka.listener.ConsumerProperties; +import org.springframework.kafka.listener.ContainerProperties; +import org.springframework.kafka.listener.KafkaMessageListenerContainer; import org.springframework.messaging.MessageHandler; @@ -24,13 +25,25 @@ import org.springframework.messaging.MessageHandler; @EnableIntegration public class SplitterApplication { - @InboundChannelAdapter(channel = "recordings") @Bean - KafkaMessageSource source( - ConsumerFactory cf, + MessageProducer messageProducer( + KafkaMessageListenerContainer messageListenerContainer) + { + KafkaMessageDrivenChannelAdapter adapter = + new KafkaMessageDrivenChannelAdapter(messageListenerContainer); + adapter.setOutputChannelName("recordings"); + return adapter; + } + + @Bean + KafkaMessageListenerContainer messageListenerContainer( + ConsumerFactory consumerFactory, SplitterApplicationProperties properties) { - return new KafkaMessageSource<>(cf, new ConsumerProperties(properties.getInputTopic())); + return + new KafkaMessageListenerContainer<>( + consumerFactory, + new ContainerProperties(properties.getInputTopic())); } @Bean -- 2.20.1