VORLAGE spring/spring-producer--json--vorlage spring/spring-producer--json--vorlage--2025-03-signal spring/spring-producer--json--vorlage--2025-04-signal
authorKai Moritz <kai@juplo.de>
Tue, 18 Mar 2025 18:58:41 +0000 (19:58 +0100)
committerKai Moritz <kai@juplo.de>
Tue, 25 Mar 2025 18:56:53 +0000 (19:56 +0100)
README.sh [deleted file]
src/main/java/de/juplo/kafka/ApplicationConfiguration.java
src/main/java/de/juplo/kafka/ExampleProducer.java
src/main/resources/application.yml

diff --git a/README.sh b/README.sh
deleted file mode 100755 (executable)
index 982f7bd..0000000
--- a/README.sh
+++ /dev/null
@@ -1,42 +0,0 @@
-#!/bin/bash
-
-IMAGE=juplo/spring-producer:1.0-json-SNAPSHOT
-
-if [ "$1" = "cleanup" ]
-then
-  docker compose -f docker/docker-compose.yml down -t0 -v --remove-orphans
-  mvn clean
-  exit
-fi
-
-docker compose -f docker/docker-compose.yml up -d --remove-orphans kafka-1 kafka-2 kafka-3
-docker compose -f docker/docker-compose.yml rm -svf producer
-
-if [[
-  $(docker image ls -q $IMAGE) == "" ||
-  "$1" = "build"
-]]
-then
-  mvn clean install || exit
-else
-  echo "Using image existing images:"
-  docker image ls $IMAGE
-fi
-
-docker compose -f docker/docker-compose.yml up --remove-orphans setup || exit 1
-
-
-docker compose -f docker/docker-compose.yml up -d producer
-docker compose -f docker/docker-compose.yml up -d peter ute
-sleep 15
-
-docker compose -f docker/docker-compose.yml stop producer
-
-echo
-echo "Von peter empfangen:"
-docker compose -f docker/docker-compose.yml logs peter | grep '\ test\/.'
-echo
-echo "Von ute empfangen:"
-docker compose -f docker/docker-compose.yml logs ute | grep '\ test\/.'
-
-docker compose -f docker/docker-compose.yml stop peter ute
index e212a25..f670172 100644 (file)
@@ -19,7 +19,7 @@ public class ApplicationConfiguration
   public ExampleProducer exampleProducer(
     @Value("${spring.kafka.client-id}") String clientId,
     ApplicationProperties properties,
-    Producer<String, SumupMessage> kafkaProducer,
+    Producer<String, String> kafkaProducer, // << TODO: Typisierung anpassen
     ConfigurableApplicationContext applicationContext)
   {
     return
index 0d152f7..9d5983b 100644 (file)
@@ -13,7 +13,7 @@ public class ExampleProducer implements Runnable
   private final String id;
   private final String topic;
   private final Duration throttle;
-  private final Producer<String, SumupMessage> producer;
+  private final Producer<String, String> producer;
   private final Thread workerThread;
   private final Runnable closeCallback;
 
@@ -25,7 +25,7 @@ public class ExampleProducer implements Runnable
     String id,
     String topic,
     Duration throttle,
-    Producer<String, SumupMessage> producer,
+    Producer<String, String> producer, // << TODO: Typisierung anpassen
     Runnable closeCallback)
   {
     this.id = id;
@@ -54,19 +54,8 @@ public class ExampleProducer implements Runnable
           ? new CalculateSumMessage(number)
           : new AddNumberMessage(number, i);
 
-        send(Long.toString(number), message);
-
-        if (throttle.isPositive())
-        {
-          try
-          {
-            Thread.sleep(throttle);
-          }
-          catch (InterruptedException e)
-          {
-            log.warn("{} - Interrupted while throttling!", e);
-          }
-        }
+        // TODO: Versenden aus Ihrer bisherigen Implementierung (inkl. Logging & Zählen)
+        // ABER: Anstatt der Zahl soll "message" als JSON serialisiert verschickt werden
       }
     }
     catch (Exception e)
@@ -83,58 +72,6 @@ public class ExampleProducer implements Runnable
     }
   }
 
-  void send(String key, SumupMessage value)
-  {
-    final long time = System.currentTimeMillis();
-
-    final ProducerRecord<String, SumupMessage> record = new ProducerRecord<>(
-      topic,  // Topic
-      key,    // Key
-      value   // Value
-    );
-
-    producer.send(record, (metadata, e) ->
-    {
-      long now = System.currentTimeMillis();
-      if (e == null)
-      {
-        // HANDLE SUCCESS
-        log.debug(
-          "{} - Sent message {}={}, partition={}, offset={}, timestamp={}, latency={}ms",
-          id,
-          key,
-          value,
-          metadata.partition(),
-          metadata.offset(),
-          metadata.timestamp(),
-          now - time
-        );
-      }
-      else
-      {
-        // HANDLE ERROR
-        log.error(
-          "{} - ERROR for message {}={}, latency={}ms: {}",
-          id,
-          key,
-          value,
-          now - time,
-          e.toString()
-        );
-      }
-    });
-
-    long now = System.currentTimeMillis();
-    produced++;
-    log.trace(
-      "{} - Queued message {}={}, latency={}ms",
-      id,
-      key,
-      value,
-      now - time
-    );
-  }
-
 
   public void shutdown() throws InterruptedException
   {
index daf440e..008742c 100644 (file)
@@ -11,11 +11,7 @@ spring:
       buffer-memory: 33554432
       batch-size: 16384
       compression-type: gzip
-      value-serializer: org.springframework.kafka.support.serializer.JsonSerializer
       properties:
-        spring.json.type.mapping: >-
-          ADD:de.juplo.kafka.AddNumberMessage,
-          CALC:de.juplo.kafka.CalculateSumMessage
         delivery-timeout: 10s
         max-block: 5s
         linger: 0