Für PUT anstelle von POST wird eine `FooMessage` als `foo` verschickt
authorKai Moritz <kai@juplo.de>
Tue, 31 May 2022 04:17:07 +0000 (06:17 +0200)
committerKai Moritz <kai@juplo.de>
Sun, 12 Jun 2022 13:26:50 +0000 (15:26 +0200)
README.sh
src/main/java/de/juplo/kafka/FooMessage.java [new file with mode: 0644]
src/main/java/de/juplo/kafka/RestProducer.java
src/test/java/de/juplo/kafka/ApplicationTests.java

index 56ddbb5..832a42f 100755 (executable)
--- a/README.sh
+++ b/README.sh
@@ -31,6 +31,7 @@ sleep 15
 
 echo 'Hallo Welt!' | http -v :8080/peter
 echo peter | http -v :8080/
+http -v PUT :8080/peter
 dd if=/dev/zero bs=1024 count=1024  | http -v :8080/fehler
 http -v :8081/seen
 
diff --git a/src/main/java/de/juplo/kafka/FooMessage.java b/src/main/java/de/juplo/kafka/FooMessage.java
new file mode 100644 (file)
index 0000000..2e9e8ba
--- /dev/null
@@ -0,0 +1,11 @@
+package de.juplo.kafka;
+
+import lombok.Value;
+
+
+@Value
+public class FooMessage
+{
+  private final String client;
+  private final Long timestamp;
+}
index 0ecd667..70b327a 100644 (file)
@@ -42,6 +42,7 @@ public class RestProducer
     props.put("value.serializer", JsonSerializer.class.getName());
     props.put(JsonSerializer.TYPE_MAPPINGS,
         "message:" + ClientMessage.class.getName() + "," +
+        "foo:" + FooMessage.class.getName() + "," +
         "greeting:" + Greeting.class.getName());
 
     this.producer = new KafkaProducer<>(props);
@@ -62,6 +63,19 @@ public class RestProducer
     return send(record);
   }
 
+  @PutMapping(path = "{key}")
+  public DeferredResult<ProduceResult> message(@PathVariable String key)
+  {
+    key = key.trim();
+    final ProducerRecord<String, Object> record = new ProducerRecord<>(
+        topic,  // Topic
+        key,    // Key
+        new FooMessage(key, System.currentTimeMillis()) // Value
+    );
+
+    return send(record);
+  }
+
   @PostMapping(path = "/")
   public DeferredResult<ProduceResult> greeting(
       @RequestBody String name)
index 71625fd..cd7d928 100644 (file)
@@ -61,6 +61,17 @@ public class ApplicationTests
                                .until(() -> consumer.received.size() == 1);
        }
 
+       @Test
+       void testSendFooMessage() throws Exception
+       {
+               mockMvc
+                               .perform(put("/peter"))
+                               .andExpect(status().isOk());
+               await("Message was send")
+                               .atMost(Duration.ofSeconds(5))
+                               .until(() -> consumer.received.size() == 1);
+       }
+
        @Test
        void testSendGreeting() throws Exception
        {