Vorlage für das Empfangen von Nachrichten aus einer geteilten Lib spring/spring-consumer--json--messages--vorlage--generics4all
authorKai Moritz <kai@juplo.de>
Sun, 2 Feb 2025 17:49:06 +0000 (18:49 +0100)
committerKai Moritz <kai@juplo.de>
Mon, 24 Feb 2025 20:49:41 +0000 (21:49 +0100)
src/main/java/de/juplo/kafka/ApplicationConfiguration.java
src/main/java/de/juplo/kafka/ExampleConsumer.java

index 1d8c916..71f258b 100644 (file)
@@ -5,7 +5,6 @@ import org.apache.kafka.clients.consumer.Consumer;
 import org.apache.kafka.clients.consumer.KafkaConsumer;
 import org.apache.kafka.clients.consumer.StickyAssignor;
 import org.apache.kafka.common.serialization.StringDeserializer;
-import org.springframework.kafka.support.serializer.JsonDeserializer;
 import org.springframework.boot.context.properties.EnableConfigurationProperties;
 import org.springframework.context.ConfigurableApplicationContext;
 import org.springframework.context.annotation.Bean;
@@ -20,7 +19,7 @@ public class ApplicationConfiguration
 {
   @Bean
   public ExampleConsumer exampleConsumer(
-    Consumer<String, Message> kafkaConsumer,
+    Consumer<String, String> kafkaConsumer,
     ApplicationProperties properties,
     ConfigurableApplicationContext applicationContext)
   {
@@ -33,7 +32,7 @@ public class ApplicationConfiguration
   }
 
   @Bean(destroyMethod = "")
-  public KafkaConsumer<String, Message> kafkaConsumer(ApplicationProperties properties)
+  public KafkaConsumer<String, String> kafkaConsumer(ApplicationProperties properties)
   {
     Properties props = new Properties();
     props.put("bootstrap.servers", properties.getBootstrapServer());
@@ -50,7 +49,7 @@ public class ApplicationConfiguration
     props.put("metadata.maxage.ms", 5000); //  5 Sekunden
     props.put("partition.assignment.strategy", StickyAssignor.class.getName());
     props.put("key.deserializer", StringDeserializer.class.getName());
-    props.put("value.deserializer", JsonDeserializer.class.getName());
+    props.put("value.deserializer", StringDeserializer.class.getName());
     props.put("spring.json.trusted.packages", "de.juplo.messages");
 
     return new KafkaConsumer<>(props);
index b61d8b2..bf6d9ce 100644 (file)
@@ -14,7 +14,7 @@ import java.util.Arrays;
 
 
 @Slf4j
-public class ExampleConsumer<K, V extends Message> implements Runnable
+public class ExampleConsumer<K, V> implements Runnable
 {
   private final String id;
   private final String topic;
@@ -96,12 +96,12 @@ public class ExampleConsumer<K, V extends Message> implements Runnable
   {
     consumed++;
     log.info("{} - partition={}-{}, offset={}: {}={}", id, topic, partition, offset, key, value);
-    switch (value.getType())
-    {
-      case ADD  -> addNumber((Add)value);
-      case CALC -> calcSum((Calculate)value);
-      default   -> log.error("{} - Ignoring message of unknown typ {}", id, value.getType());
-    }
+    // switch (value.getType())
+    // {
+    //   case ADD  -> addNumber((Add)value);
+    //   case CALC -> calcSum((Calculate)value);
+    //   default   -> log.error("{} - Ignoring message of unknown typ {}", id, value.getType());
+    // }
   }
 
   private void addNumber(Add add)