Springify: Gemeinsame DLQ für Poison Pills und Fachlogik-Fehler konfiguriert
[demos/kafka/training] / src / main / java / de / juplo / kafka / ApplicationConfiguration.java
index 12b6990..4923b09 100644 (file)
@@ -1,24 +1,30 @@
 package de.juplo.kafka;
 
 import org.apache.kafka.clients.consumer.ConsumerRecord;
+import org.apache.kafka.common.TopicPartition;
+import org.apache.kafka.common.serialization.ByteArraySerializer;
+import org.apache.kafka.common.serialization.StringSerializer;
+import org.springframework.boot.autoconfigure.kafka.KafkaProperties;
 import org.springframework.boot.context.properties.EnableConfigurationProperties;
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;
-import org.springframework.kafka.config.ConcurrentKafkaListenerContainerFactory;
-import org.springframework.kafka.config.KafkaListenerContainerFactory;
-import org.springframework.kafka.core.ConsumerFactory;
-import org.springframework.kafka.listener.CommonContainerStoppingErrorHandler;
-import org.springframework.kafka.listener.CommonErrorHandler;
+import org.springframework.kafka.core.*;
+import org.springframework.kafka.listener.DeadLetterPublishingRecoverer;
+import org.springframework.kafka.listener.DefaultErrorHandler;
+import org.springframework.kafka.support.serializer.DelegatingByTypeSerializer;
+import org.springframework.kafka.support.serializer.JsonSerializer;
+import org.springframework.util.backoff.FixedBackOff;
 
+import java.util.Map;
 import java.util.function.Consumer;
 
 
 @Configuration
-@EnableConfigurationProperties(ApplicationProperties.class)
+@EnableConfigurationProperties({ KafkaProperties.class, ApplicationProperties.class })
 public class ApplicationConfiguration
 {
   @Bean
-  public Consumer<ConsumerRecord<String, Long>> consumer()
+  public Consumer<ConsumerRecord<String, ClientMessage>> consumer()
   {
     return (record) ->
     {
@@ -27,23 +33,41 @@ public class ApplicationConfiguration
   }
 
   @Bean
-  public KafkaListenerContainerFactory<?> batchFactory(
-      ConsumerFactory<String, Long> consumerFactory,
-      CommonErrorHandler errorHandler)
-  {
-    ConcurrentKafkaListenerContainerFactory<String, Long> factory =
-        new ConcurrentKafkaListenerContainerFactory<>();
+  public ProducerFactory<String, Object> producerFactory(KafkaProperties properties) {
+    return new DefaultKafkaProducerFactory<>(
+        properties.getProducer().buildProperties(),
+        new StringSerializer(),
+        new DelegatingByTypeSerializer(Map.of(
+            byte[].class, new ByteArraySerializer(),
+            ClientMessage.class, new JsonSerializer<>())));
+  }
 
-    factory.setConsumerFactory(consumerFactory);
-    factory.setCommonErrorHandler(errorHandler);
-    factory.setBatchListener(true);
+  @Bean
+  public KafkaTemplate<String, Object> kafkaTemplate(
+      ProducerFactory<String, Object> producerFactory) {
+
+    return new KafkaTemplate<>(producerFactory);
+  }
 
-    return factory;
+  @Bean
+  public DeadLetterPublishingRecoverer recoverer(
+      ApplicationProperties properties,
+      KafkaOperations<?, ?> template)
+  {
+    return new DeadLetterPublishingRecoverer(
+        template,
+        (record, exception) -> new TopicPartition(properties.getDlqTopic(), record.partition()));
   }
 
   @Bean
-  public CommonContainerStoppingErrorHandler errorHandler()
+  public DefaultErrorHandler errorHandler(DeadLetterPublishingRecoverer recoverer)
+  {
+    return new DefaultErrorHandler(recoverer, new FixedBackOff(0l, 0l));
+  }
+
+  @Bean(destroyMethod = "close")
+  public org.apache.kafka.clients.consumer.Consumer<String, ClientMessage> kafkaConsumer(ConsumerFactory<String, ClientMessage> factory)
   {
-    return new CommonContainerStoppingErrorHandler();
+    return factory.createConsumer();
   }
 }