splitter: 1.0.0-spring-ingetration - using a channel backed by Kafka
[demos/kafka/wordcount] / src / main / java / de / juplo / kafka / wordcount / splitter / SplitterApplication.java
index d46a7cd..eeeeb39 100644 (file)
@@ -1,23 +1,57 @@
 package de.juplo.kafka.wordcount.splitter;
 
+import org.springframework.beans.factory.annotation.Value;
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
 import org.springframework.boot.context.properties.EnableConfigurationProperties;
 import org.springframework.context.annotation.Bean;
-
-import java.time.Clock;
+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.ServiceActivator;
+import org.springframework.integration.config.EnableIntegration;
+import org.springframework.integration.kafka.channel.SubscribableKafkaChannel;
+import org.springframework.integration.kafka.outbound.KafkaProducerMessageHandler;
+import org.springframework.kafka.config.KafkaListenerContainerFactory;
+import org.springframework.kafka.core.KafkaTemplate;
+import org.springframework.kafka.listener.AbstractMessageListenerContainer;
+import org.springframework.messaging.MessageHandler;
+import org.springframework.messaging.SubscribableChannel;
 
 
 @SpringBootApplication
 @EnableConfigurationProperties(SplitterApplicationProperties.class)
+@EnableIntegration
 public class SplitterApplication
 {
        @Bean
-       Clock clock()
+       SubscribableChannel recordings(
+                       KafkaTemplate<String, String> kafkaTemplate,
+                       KafkaListenerContainerFactory<AbstractMessageListenerContainer<String, String>> containerFactory,
+                       SplitterApplicationProperties properties,
+                       @Value("${spring.kafka.consumer.group-id}") String groupId)
        {
-               return Clock.systemDefaultZone();
+               SubscribableKafkaChannel channel =
+                               new SubscribableKafkaChannel(kafkaTemplate, containerFactory, properties.getInputTopic());
+               channel.setGroupId(groupId);
+               return channel;
        }
 
+       @Bean
+       @ServiceActivator(inputChannel = "words")
+       MessageHandler handler(
+                       KafkaTemplate<String, String> kafkaTemplate,
+                       SplitterApplicationProperties properties)
+       {
+               KafkaProducerMessageHandler<String, String> handler =
+                               new KafkaProducerMessageHandler<>(kafkaTemplate);
+               handler.setTopicExpression(new LiteralExpression(properties.getOutputTopic()));
+               final ExpressionParser parser = new SpelExpressionParser();
+               Expression expression = parser.parseExpression("headers['kafka_receivedMessageKey']");
+               handler.setMessageKeyExpression(expression);
+               return handler;
+       }
 
        public static void main(String[] args)
        {