`spring-producer` verschickt Nachrichten aus geteilter Lib spring/spring-producer--json--messages spring/spring-producer--json--messages--2025-03-signal spring/spring-producer--json--messages--2025-04-signal
authorKai Moritz <kai@juplo.de>
Sun, 2 Feb 2025 15:12:59 +0000 (16:12 +0100)
committerKai Moritz <kai@juplo.de>
Tue, 25 Mar 2025 18:56:53 +0000 (19:56 +0100)
README.sh
build.gradle
docker/docker-compose.yml
pom.xml
src/main/java/de/juplo/kafka/AddNumberMessage.java [deleted file]
src/main/java/de/juplo/kafka/ApplicationConfiguration.java
src/main/java/de/juplo/kafka/CalculateSumMessage.java [deleted file]
src/main/java/de/juplo/kafka/ExampleProducer.java
src/main/java/de/juplo/kafka/SumupMessage.java [deleted file]
src/main/resources/application.yml

index 982f7bd..1780755 100755 (executable)
--- a/README.sh
+++ b/README.sh
@@ -1,6 +1,6 @@
 #!/bin/bash
 
-IMAGE=juplo/spring-producer:1.0-json-SNAPSHOT
+IMAGE=juplo/spring-producer:1.0-json-messages-SNAPSHOT
 
 if [ "$1" = "cleanup" ]
 then
index 7556511..9e2fa4b 100644 (file)
@@ -8,7 +8,7 @@ plugins {
 }
 
 group = 'de.juplo.kafka'
-version = '1.0-json-SNAPSHOT'
+version = '1.0-json-messages-SNAPSHOT'
 
 java {
        toolchain {
@@ -24,6 +24,7 @@ configurations {
 
 repositories {
        mavenCentral()
+       mavenLocal()
 }
 
 dependencies {
@@ -31,6 +32,7 @@ dependencies {
        implementation 'org.springframework.boot:spring-boot-starter-validation'
        implementation 'org.springframework.boot:spring-boot-starter-web'
        implementation 'org.springframework.kafka:spring-kafka'
+       implementation 'de.juplo.messages:sumup-messages:1.0-SNAPSHOT'
        compileOnly 'org.projectlombok:lombok'
        developmentOnly 'org.springframework.boot:spring-boot-devtools'
        annotationProcessor 'org.springframework.boot:spring-boot-configuration-processor'
index ac00f3c..b135ad2 100644 (file)
@@ -136,7 +136,7 @@ services:
       - kafka-3
 
   producer:
-    image: juplo/spring-producer:1.0-json-SNAPSHOT
+    image: juplo/spring-producer:1.0-json-messages-SNAPSHOT
     environment:
       spring.kafka.bootstrap-servers: kafka:9092
       spring.kafka.client-id: producer
diff --git a/pom.xml b/pom.xml
index 7bcb24c..30d59ca 100644 (file)
--- a/pom.xml
+++ b/pom.xml
   <artifactId>spring-producer</artifactId>
   <name>Spring Producer</name>
   <description>A Simple Producer, based on Spring Boot, that sends messages via Kafka</description>
-  <version>1.0-json-SNAPSHOT</version>
+  <version>1.0-json-messages-SNAPSHOT</version>
 
   <properties>
     <java.version>21</java.version>
+    <sumup-messages.version>1.0-SNAPSHOT</sumup-messages.version>
   </properties>
 
   <dependencies>
       <groupId>org.springframework.kafka</groupId>
       <artifactId>spring-kafka</artifactId>
     </dependency>
+    <dependency>
+      <groupId>de.juplo.messages</groupId>
+      <artifactId>sumup-messages</artifactId>
+      <version>${sumup-messages.version}</version>
+    </dependency>
     <dependency>
       <groupId>org.projectlombok</groupId>
       <artifactId>lombok</artifactId>
-      <scope>compile</scope>
     </dependency>
     <dependency>
       <groupId>org.springframework.boot</groupId>
diff --git a/src/main/java/de/juplo/kafka/AddNumberMessage.java b/src/main/java/de/juplo/kafka/AddNumberMessage.java
deleted file mode 100644 (file)
index deb6350..0000000
+++ /dev/null
@@ -1,11 +0,0 @@
-package de.juplo.kafka;
-
-import lombok.Value;
-
-
-@Value
-public class AddNumberMessage implements SumupMessage
-{
-  private final int number;
-  private final int next;
-}
index e212a25..b2c5cfa 100644 (file)
@@ -1,5 +1,6 @@
 package de.juplo.kafka;
 
+import de.juplo.messages.Message;
 import org.apache.kafka.clients.producer.Producer;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.boot.context.properties.EnableConfigurationProperties;
@@ -19,7 +20,7 @@ public class ApplicationConfiguration
   public ExampleProducer exampleProducer(
     @Value("${spring.kafka.client-id}") String clientId,
     ApplicationProperties properties,
-    Producer<String, SumupMessage> kafkaProducer,
+    Producer<String, Message> kafkaProducer,
     ConfigurableApplicationContext applicationContext)
   {
     return
diff --git a/src/main/java/de/juplo/kafka/CalculateSumMessage.java b/src/main/java/de/juplo/kafka/CalculateSumMessage.java
deleted file mode 100644 (file)
index 6aa0121..0000000
+++ /dev/null
@@ -1,11 +0,0 @@
-package de.juplo.kafka;
-
-
-import lombok.Value;
-
-
-@Value
-public class CalculateSumMessage implements SumupMessage
-{
-  private final int number;
-}
index 0d152f7..2607dc0 100644 (file)
@@ -1,5 +1,8 @@
 package de.juplo.kafka;
 
+import de.juplo.messages.Add;
+import de.juplo.messages.Calculate;
+import de.juplo.messages.Message;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.kafka.clients.producer.Producer;
 import org.apache.kafka.clients.producer.ProducerRecord;
@@ -13,7 +16,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, Message> producer;
   private final Thread workerThread;
   private final Runnable closeCallback;
 
@@ -25,7 +28,7 @@ public class ExampleProducer implements Runnable
     String id,
     String topic,
     Duration throttle,
-    Producer<String, SumupMessage> producer,
+    Producer<String, Message> producer,
     Runnable closeCallback)
   {
     this.id = id;
@@ -50,9 +53,9 @@ public class ExampleProducer implements Runnable
       for (; running; i++)
       {
         int number = i % 10;
-        SumupMessage message = (i % 7 == 0)
-          ? new CalculateSumMessage(number)
-          : new AddNumberMessage(number, i);
+        Message message = (i % 7 == 0)
+          ? Calculate.builder().number(number).build()
+          : Add.builder().number(number).next(i).build();
 
         send(Long.toString(number), message);
 
@@ -83,11 +86,11 @@ public class ExampleProducer implements Runnable
     }
   }
 
-  void send(String key, SumupMessage value)
+  void send(String key, Message value)
   {
     final long time = System.currentTimeMillis();
 
-    final ProducerRecord<String, SumupMessage> record = new ProducerRecord<>(
+    final ProducerRecord<String, Message> record = new ProducerRecord<>(
       topic,  // Topic
       key,    // Key
       value   // Value
diff --git a/src/main/java/de/juplo/kafka/SumupMessage.java b/src/main/java/de/juplo/kafka/SumupMessage.java
deleted file mode 100644 (file)
index 739efd1..0000000
+++ /dev/null
@@ -1,5 +0,0 @@
-package de.juplo.kafka;
-
-public interface SumupMessage
-{
-}
index daf440e..99e6508 100644 (file)
@@ -13,9 +13,6 @@ spring:
       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