]> juplo.de Git - demos/kafka/training/commitdiff
Vorlage für das Empfangen von Nachrichten aus einer geteilten Lib springkafka/spring-consumer--json--messages--vorlage--2026-03-21--smartlifecycle-only
authorKai Moritz <kai@juplo.de>
Sun, 2 Feb 2025 17:49:06 +0000 (18:49 +0100)
committerKai Moritz <kai@juplo.de>
Fri, 20 Mar 2026 18:31:55 +0000 (19:31 +0100)
src/main/java/de/juplo/kafka/ApplicationConfiguration.java
src/main/java/de/juplo/kafka/ExampleConsumer.java

index d8de508cca7b38cd041c0968ecb8bd34c2efb775..63ba5ade2159c378109e6c9d98e6aa59a891ab94 100644 (file)
@@ -4,7 +4,6 @@ import de.juplo.messages.Message;
 import org.apache.kafka.clients.consumer.Consumer;
 import org.apache.kafka.clients.consumer.KafkaConsumer;
 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;
@@ -19,7 +18,7 @@ public class ApplicationConfiguration
 {
   @Bean
   public ExampleConsumer exampleConsumer(
-    Consumer<String, Message> kafkaConsumer,
+    Consumer<String, String> kafkaConsumer,
     ApplicationProperties properties,
     ConfigurableApplicationContext applicationContext)
   {
@@ -31,7 +30,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());
@@ -47,7 +46,7 @@ public class ApplicationConfiguration
     }
     props.put("metadata.max.age.ms", 5000); //  5 Sekunden
     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 13f3031d533b45ede7237eb0e49fe7ca3c40333b..4860c409b52c7d6bb1b3b8190830118fd965833e 100644 (file)
@@ -19,7 +19,7 @@ public class ExampleConsumer implements Runnable, SmartLifecycle
 {
   private final String id;
   private final String topic;
-  private final Consumer<String, Message> consumer;
+  private final Consumer<String, String> consumer;
 
   private Thread workerThread;
   private volatile boolean running = false;
@@ -29,7 +29,7 @@ public class ExampleConsumer implements Runnable, SmartLifecycle
   public ExampleConsumer(
     String clientId,
     String topic,
-    Consumer<String, Message> consumer)
+    Consumer<String, String> consumer)
   {
     this.id = clientId;
     this.topic = topic;
@@ -60,10 +60,10 @@ public class ExampleConsumer implements Runnable, SmartLifecycle
 
       while (true)
       {
-        ConsumerRecords<String, Message> records = consumer.poll(Duration.ofSeconds(1));
+        ConsumerRecords<String, String> records = consumer.poll(Duration.ofSeconds(1));
 
         log.info("{} - Received {} messages", id, records.count());
-        for (ConsumerRecord<String, Message> record : records)
+        for (ConsumerRecord<String, String> record : records)
         {
           handleRecord(
             record.topic(),
@@ -96,16 +96,16 @@ public class ExampleConsumer implements Runnable, SmartLifecycle
     Integer partition,
     Long offset,
     String key,
-    Message value)
+    String value)
   {
     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)