splitter: 1.0.0-spring-ingetration - added an interceptor for logging
[demos/kafka/wordcount] / src / main / java / de / juplo / kafka / wordcount / splitter / SplitterApplication.java
index 3828050..3dcb443 100644 (file)
@@ -9,15 +9,31 @@ 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.annotation.Transformer;
+import org.springframework.integration.channel.DirectChannel;
+import org.springframework.integration.channel.interceptor.WireTap;
 import org.springframework.integration.config.EnableIntegration;
+import org.springframework.integration.config.GlobalChannelInterceptor;
 import org.springframework.integration.core.MessageProducer;
+import org.springframework.integration.handler.LoggingHandler;
 import org.springframework.integration.kafka.inbound.KafkaMessageDrivenChannelAdapter;
 import org.springframework.integration.kafka.outbound.KafkaProducerMessageHandler;
+import org.springframework.integration.transformer.HeaderEnricher;
+import org.springframework.integration.transformer.support.ExpressionEvaluatingHeaderValueMessageProcessor;
+import org.springframework.integration.transformer.support.HeaderValueMessageProcessor;
 import org.springframework.kafka.core.ConsumerFactory;
 import org.springframework.kafka.core.KafkaTemplate;
 import org.springframework.kafka.listener.ContainerProperties;
 import org.springframework.kafka.listener.KafkaMessageListenerContainer;
+import org.springframework.integration.kafka.channel.SubscribableKafkaChannel;
+import org.springframework.kafka.config.KafkaListenerContainerFactory;
+import org.springframework.kafka.listener.AbstractMessageListenerContainer;
+import org.springframework.messaging.MessageChannel;
 import org.springframework.messaging.MessageHandler;
+import org.springframework.messaging.support.ChannelInterceptor;
+
+import java.util.HashMap;
+import java.util.Map;
 
 
 @SpringBootApplication
@@ -31,10 +47,24 @@ public class SplitterApplication
        {
                KafkaMessageDrivenChannelAdapter<String, String> adapter =
                                new KafkaMessageDrivenChannelAdapter(messageListenerContainer);
-               adapter.setOutputChannelName("recordings");
+               adapter.setOutputChannelName("kafka-in");
                return adapter;
        }
 
+       @Bean
+       @Transformer(inputChannel = "kafka-in", outputChannel = "recordings")
+       public HeaderEnricher kafkaInHeaderEnricher()
+       {
+               Map<String, HeaderValueMessageProcessor<?>> headersToAdd = new HashMap<>();
+               Expression expression =
+                               new SpelExpressionParser().parseExpression("headers['kafka_receivedMessageKey']");
+               headersToAdd.put(
+                               "key",
+                               new ExpressionEvaluatingHeaderValueMessageProcessor<>(expression, String.class));
+               HeaderEnricher enricher = new HeaderEnricher(headersToAdd);
+               return enricher;
+       }
+
        @Bean
        KafkaMessageListenerContainer<String, String> messageListenerContainer(
                        ConsumerFactory<String,String> consumerFactory,
@@ -46,6 +76,17 @@ public class SplitterApplication
                                                new ContainerProperties(properties.getInputTopic()));
        }
 
+       @Bean
+       SubscribableKafkaChannel recordings(
+                       KafkaTemplate<String, String> kafkaTemplate,
+                       KafkaListenerContainerFactory<AbstractMessageListenerContainer<String, String>> containerFactory)
+       {
+               SubscribableKafkaChannel channel =
+                               new SubscribableKafkaChannel(kafkaTemplate, containerFactory, "channel-recordings");
+               channel.setGroupId("recordings");
+               return channel;
+       }
+
        @Bean
        @ServiceActivator(inputChannel = "words")
        MessageHandler handler(
@@ -56,11 +97,46 @@ public class SplitterApplication
                                new KafkaProducerMessageHandler<>(kafkaTemplate);
                handler.setTopicExpression(new LiteralExpression(properties.getOutputTopic()));
                final ExpressionParser parser = new SpelExpressionParser();
-               Expression expression = parser.parseExpression("headers['kafka_receivedMessageKey']");
+               Expression expression = parser.parseExpression("headers['key']");
                handler.setMessageKeyExpression(expression);
                return handler;
        }
 
+       @Bean
+       SubscribableKafkaChannel words(
+                       KafkaTemplate<String, String> kafkaTemplate,
+                       KafkaListenerContainerFactory<AbstractMessageListenerContainer<String, String>> containerFactory)
+       {
+               SubscribableKafkaChannel channel =
+                               new SubscribableKafkaChannel(kafkaTemplate, containerFactory, "channel-words");
+               channel.setGroupId("words");
+               return channel;
+       }
+
+       @Bean
+       MessageChannel messageLog()
+       {
+               return new DirectChannel();
+       }
+
+       @GlobalChannelInterceptor
+       @Bean
+       ChannelInterceptor globalLoggingWireTap(MessageChannel messageLog)
+       {
+               return new WireTap(messageLog);
+       }
+
+       @Bean
+       @ServiceActivator(inputChannel = "messageLog")
+       public LoggingHandler logging()
+       {
+               LoggingHandler adapter = new LoggingHandler(LoggingHandler.Level.DEBUG);
+               adapter.setLoggerName("MESSAGE_LOG");
+               adapter.setLogExpressionString("headers.id + ': ' + payload + ', headers=' + headers");
+               return adapter;
+       }
+
+
        public static void main(String[] args)
        {
                SpringApplication.run(SplitterApplication.class, args);