Einheitliche Benennung des Producers -- MOVE
authorKai Moritz <kai@juplo.de>
Wed, 25 Sep 2024 17:13:00 +0000 (19:13 +0200)
committerKai Moritz <kai@juplo.de>
Wed, 25 Sep 2024 17:13:00 +0000 (19:13 +0200)
src/main/java/de/juplo/kafka/ExampleProducer.java [new file with mode: 0644]
src/main/java/de/juplo/kafka/RestProducer.java [deleted file]

diff --git a/src/main/java/de/juplo/kafka/ExampleProducer.java b/src/main/java/de/juplo/kafka/ExampleProducer.java
new file mode 100644 (file)
index 0000000..cecb980
--- /dev/null
@@ -0,0 +1,102 @@
+package de.juplo.kafka;
+
+import lombok.RequiredArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.kafka.clients.producer.Producer;
+import org.apache.kafka.clients.producer.KafkaProducer;
+import org.apache.kafka.clients.producer.ProducerRecord;
+import org.springframework.http.HttpStatus;
+import org.springframework.web.bind.annotation.*;
+import org.springframework.web.context.request.async.DeferredResult;
+
+import java.math.BigInteger;
+
+
+@Slf4j
+@RequestMapping
+@ResponseBody
+@RequiredArgsConstructor
+public class RestProducer
+{
+  private final String id;
+  private final String topic;
+  private final Integer partition;
+  private final Producer<String, String> producer;
+
+  private long produced = 0;
+
+  @PostMapping(path = "{key}")
+  public DeferredResult<ProduceResult> send(
+      @PathVariable String key,
+      @RequestHeader(name = "X-id", required = false) Long correlationId,
+      @RequestBody String value)
+  {
+    DeferredResult<ProduceResult> result = new DeferredResult<>();
+
+    final long time = System.currentTimeMillis();
+
+    final ProducerRecord<String, String> record = new ProducerRecord<>(
+        topic,  // Topic
+        partition, // Partition
+        key,    // Key
+        value   // Value
+    );
+
+    record.headers().add("source", id.getBytes());
+    if (correlationId != null)
+    {
+      record.headers().add("id", BigInteger.valueOf(correlationId).toByteArray());
+    }
+
+    producer.send(record, (metadata, e) ->
+    {
+      long now = System.currentTimeMillis();
+      if (e == null)
+      {
+        // HANDLE SUCCESS
+        produced++;
+        result.setResult(new ProduceSuccess(metadata.partition(), metadata.offset()));
+        log.debug(
+            "{} - Sent key={} message={} partition={}/{} timestamp={} latency={}ms",
+            id,
+            record.key(),
+            record.value(),
+            metadata.partition(),
+            metadata.offset(),
+            metadata.timestamp(),
+            now - time
+        );
+      }
+      else
+      {
+        // HANDLE ERROR
+        result.setErrorResult(new ProduceFailure(e));
+        log.error(
+            "{} - ERROR key={} timestamp={} latency={}ms: {}",
+            id,
+            record.key(),
+            metadata == null ? -1 : metadata.timestamp(),
+            now - time,
+            e.toString()
+        );
+      }
+    });
+
+    long now = System.currentTimeMillis();
+    log.trace(
+        "{} - Queued message with key={} latency={}ms",
+        id,
+        record.key(),
+        now - time
+    );
+
+    return result;
+  }
+
+  @ExceptionHandler
+  @ResponseStatus(HttpStatus.BAD_REQUEST)
+  public ErrorResponse illegalStateException(IllegalStateException e)
+  {
+    return new ErrorResponse(e.getMessage(), HttpStatus.BAD_REQUEST.value());
+  }
+}
diff --git a/src/main/java/de/juplo/kafka/RestProducer.java b/src/main/java/de/juplo/kafka/RestProducer.java
deleted file mode 100644 (file)
index cecb980..0000000
+++ /dev/null
@@ -1,102 +0,0 @@
-package de.juplo.kafka;
-
-import lombok.RequiredArgsConstructor;
-import lombok.extern.slf4j.Slf4j;
-import org.apache.kafka.clients.producer.Producer;
-import org.apache.kafka.clients.producer.KafkaProducer;
-import org.apache.kafka.clients.producer.ProducerRecord;
-import org.springframework.http.HttpStatus;
-import org.springframework.web.bind.annotation.*;
-import org.springframework.web.context.request.async.DeferredResult;
-
-import java.math.BigInteger;
-
-
-@Slf4j
-@RequestMapping
-@ResponseBody
-@RequiredArgsConstructor
-public class RestProducer
-{
-  private final String id;
-  private final String topic;
-  private final Integer partition;
-  private final Producer<String, String> producer;
-
-  private long produced = 0;
-
-  @PostMapping(path = "{key}")
-  public DeferredResult<ProduceResult> send(
-      @PathVariable String key,
-      @RequestHeader(name = "X-id", required = false) Long correlationId,
-      @RequestBody String value)
-  {
-    DeferredResult<ProduceResult> result = new DeferredResult<>();
-
-    final long time = System.currentTimeMillis();
-
-    final ProducerRecord<String, String> record = new ProducerRecord<>(
-        topic,  // Topic
-        partition, // Partition
-        key,    // Key
-        value   // Value
-    );
-
-    record.headers().add("source", id.getBytes());
-    if (correlationId != null)
-    {
-      record.headers().add("id", BigInteger.valueOf(correlationId).toByteArray());
-    }
-
-    producer.send(record, (metadata, e) ->
-    {
-      long now = System.currentTimeMillis();
-      if (e == null)
-      {
-        // HANDLE SUCCESS
-        produced++;
-        result.setResult(new ProduceSuccess(metadata.partition(), metadata.offset()));
-        log.debug(
-            "{} - Sent key={} message={} partition={}/{} timestamp={} latency={}ms",
-            id,
-            record.key(),
-            record.value(),
-            metadata.partition(),
-            metadata.offset(),
-            metadata.timestamp(),
-            now - time
-        );
-      }
-      else
-      {
-        // HANDLE ERROR
-        result.setErrorResult(new ProduceFailure(e));
-        log.error(
-            "{} - ERROR key={} timestamp={} latency={}ms: {}",
-            id,
-            record.key(),
-            metadata == null ? -1 : metadata.timestamp(),
-            now - time,
-            e.toString()
-        );
-      }
-    });
-
-    long now = System.currentTimeMillis();
-    log.trace(
-        "{} - Queued message with key={} latency={}ms",
-        id,
-        record.key(),
-        now - time
-    );
-
-    return result;
-  }
-
-  @ExceptionHandler
-  @ResponseStatus(HttpStatus.BAD_REQUEST)
-  public ErrorResponse illegalStateException(IllegalStateException e)
-  {
-    return new ErrorResponse(e.getMessage(), HttpStatus.BAD_REQUEST.value());
-  }
-}