]> juplo.de Git - demos/kafka/training/commitdiff
`ExampleProducer` auf das `KafkaTemplate` umgestellt spring/spring-producer--kafkatemplate--2026-03--vor-branchumbenennung--springframework spring/spring-producer--kafkatemplate--2026-03-lvm springkafka/spring-producer--kafkatemplate--2026-03-20 springkafka/spring-producer--kafkatemplate--2026-03-20--19-06 springkafka/spring-producer--kafkatemplate--2026-03-21--smartlifecycle-only
authorKai Moritz <kai@juplo.de>
Sat, 18 Jan 2025 15:44:08 +0000 (16:44 +0100)
committerKai Moritz <kai@juplo.de>
Sun, 15 Mar 2026 20:08:59 +0000 (21:08 +0100)
README.sh
build.gradle
docker/docker-compose.yml
pom.xml
src/main/java/de/juplo/kafka/ApplicationConfiguration.java
src/main/java/de/juplo/kafka/ExampleProducer.java
src/test/java/de/juplo/kafka/ApplicationTests.java

index 1d208d534a05f579cb7a49e78aecf53c9699b79c..04f0a01b4aa43703db35b69649c6442d842c8b3e 100755 (executable)
--- a/README.sh
+++ b/README.sh
@@ -1,6 +1,6 @@
 #!/bin/bash
 
-IMAGE=juplo/spring-producer:2.0-SNAPSHOT
+IMAGE=juplo/spring-producer:2.0-kafkatemplate-SNAPSHOT
 
 if [ "$1" = "cleanup" ]
 then
index 60118b631b1368e50753f94eee843fe1ef877590..80fceadd015527b5cead9f8e3dbb5eee89629117 100644 (file)
@@ -8,7 +8,7 @@ plugins {
 }
 
 group = 'de.juplo.kafka'
-version = '2.0-SNAPSHOT'
+version = '2.0-kafkatemplate-SNAPSHOT'
 
 java {
        toolchain {
index d151dab5dd111e95b65cfc4d4c92b380e45ea14b..b5f848a0df1c92edb51db68e53e21dcd69eaaed5 100644 (file)
@@ -173,7 +173,7 @@ services:
       - kafka-3
 
   producer:
-    image: juplo/spring-producer:2.0-SNAPSHOT
+    image: juplo/spring-producer:2.0-kafkatemplate-SNAPSHOT
     environment:
       spring.kafka.bootstrap-servers: kafka:9092
       spring.kafka.client-id: producer
diff --git a/pom.xml b/pom.xml
index 9e11d6248a2613e406198e93cad1ef638b02929d..e2ae20def26478fa6afc3a873d799eab0874ecbb 100644 (file)
--- a/pom.xml
+++ b/pom.xml
@@ -14,8 +14,8 @@
   <groupId>de.juplo.kafka</groupId>
   <artifactId>spring-producer</artifactId>
   <name>Spring Producer</name>
-  <description>A Simple Producer, based on Spring Boot, that sends messages via Kafka</description>
-  <version>2.0-SNAPSHOT</version>
+  <description>A Simple Producer, based on the KafkaTemplate and Spring Boot, that sends messages via Kafka</description>
+  <version>2.0-kafkatemplate-SNAPSHOT</version>
 
   <properties>
     <java.version>21</java.version>
index efdfafa179d6e0c27c16cdeb15ea8d0e7666f506..f7ad65941284ec5f17a65d40fab9c7a0ba7625fa 100644 (file)
@@ -1,12 +1,11 @@
 package de.juplo.kafka;
 
-import org.apache.kafka.clients.producer.Producer;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.boot.context.properties.EnableConfigurationProperties;
 import org.springframework.context.ConfigurableApplicationContext;
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;
-import org.springframework.kafka.core.ProducerFactory;
+import org.springframework.kafka.core.KafkaTemplate;
 
 import java.time.Duration;
 
@@ -19,7 +18,7 @@ public class ApplicationConfiguration
   public ExampleProducer exampleProducer(
     @Value("${spring.kafka.client-id}") String clientId,
     ApplicationProperties properties,
-    Producer<String, String> kafkaProducer,
+    KafkaTemplate<String, String> kafkaTemplate,
     ConfigurableApplicationContext applicationContext)
   {
     return
@@ -29,13 +28,7 @@ public class ApplicationConfiguration
         properties.getProducerProperties().getThrottle() == null
           ? Duration.ofMillis(500)
           : properties.getProducerProperties().getThrottle(),
-        kafkaProducer,
+        kafkaTemplate,
         () -> applicationContext.close());
   }
-
-  @Bean(destroyMethod = "")
-  public Producer<?, ?> kafkaProducer(ProducerFactory<?, ?> producerFactory)
-  {
-    return producerFactory.createProducer();
-  }
 }
index 93d0d1738a3ce0eeabb83cd040c01b41dac4bda9..1e0ec3e712c060597c0e5d8032fe58cfcaf01620 100644 (file)
@@ -1,8 +1,8 @@
 package de.juplo.kafka;
 
 import lombok.extern.slf4j.Slf4j;
-import org.apache.kafka.clients.producer.Producer;
-import org.apache.kafka.clients.producer.ProducerRecord;
+import org.apache.kafka.clients.producer.RecordMetadata;
+import org.springframework.kafka.core.KafkaTemplate;
 
 import java.time.Duration;
 
@@ -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, String> producer;
+  private final KafkaTemplate<String, String> kafkaTemplate;
   private final Thread workerThread;
   private final Runnable closeCallback;
 
@@ -25,13 +25,13 @@ public class ExampleProducer implements Runnable
     String id,
     String topic,
     Duration throttle,
-    Producer<String, String> producer,
+    KafkaTemplate<String, String> kafkaTemplate,
     Runnable closeCallback)
   {
     this.id = id;
     this.topic = topic;
     this.throttle = throttle;
-    this.producer = producer;
+    this.kafkaTemplate = kafkaTemplate;
 
     workerThread = new Thread(this, "ExampleProducer Worker-Thread");
     workerThread.start();
@@ -72,8 +72,6 @@ public class ExampleProducer implements Runnable
     }
     finally
     {
-      log.info("{}: Closing the KafkaProducer", id);
-      producer.close();
       log.info("{}: Produced {} messages in total, exiting!", id, produced);
     }
   }
@@ -82,18 +80,13 @@ public class ExampleProducer implements Runnable
   {
     final long sendRequested = System.currentTimeMillis();
 
-    final ProducerRecord<String, String> record = new ProducerRecord<>(
-      topic,  // Topic
-      key,    // Key
-      value   // Value
-    );
-
-    producer.send(record, (metadata, e) ->
+    kafkaTemplate.send(topic, key, value).whenComplete((result, e) ->
     {
       long sendRequestProcessed = System.currentTimeMillis();
       if (e == null)
       {
         // HANDLE SUCCESS
+        RecordMetadata metadata = result.getRecordMetadata();
         log.debug(
           "{} - Sent message {}={}, partition={}, offset={}, timestamp={}, latency={}ms",
           id,
index b313e9c9337f8ee5d1beab191e5fbaf2999c885c..b35cb35376c61790f3cccb0653b41b7fbb8e2488 100644 (file)
@@ -27,7 +27,6 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
 
 @SpringBootTest(
   properties = {
-    "spring.kafka.bootstrap-servers=${spring.embedded.kafka.brokers}",
     "spring.kafka.consumer.auto-offset-reset=earliest",
     "juplo.producer.topic=" + TOPIC})
 @AutoConfigureMockMvc