From 459f64072f3b657990a3bda0d39306208702d537 Mon Sep 17 00:00:00 2001 From: Kai Moritz Date: Tue, 31 May 2022 06:17:07 +0200 Subject: [PATCH] =?utf8?q?F=C3=BCr=20PUT=20anstelle=20von=20POST=20wird=20?= =?utf8?q?eine=20`FooMessage`=20als=20`foo`=20verschickt?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- README.sh | 1 + src/main/java/de/juplo/kafka/FooMessage.java | 11 +++++++++++ src/main/java/de/juplo/kafka/RestProducer.java | 14 ++++++++++++++ src/test/java/de/juplo/kafka/ApplicationTests.java | 11 +++++++++++ 4 files changed, 37 insertions(+) create mode 100644 src/main/java/de/juplo/kafka/FooMessage.java diff --git a/README.sh b/README.sh index 56ddbb5..832a42f 100755 --- 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 index 0000000..2e9e8ba --- /dev/null +++ b/src/main/java/de/juplo/kafka/FooMessage.java @@ -0,0 +1,11 @@ +package de.juplo.kafka; + +import lombok.Value; + + +@Value +public class FooMessage +{ + private final String client; + private final Long timestamp; +} diff --git a/src/main/java/de/juplo/kafka/RestProducer.java b/src/main/java/de/juplo/kafka/RestProducer.java index 0ecd667..70b327a 100644 --- a/src/main/java/de/juplo/kafka/RestProducer.java +++ b/src/main/java/de/juplo/kafka/RestProducer.java @@ -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 message(@PathVariable String key) + { + key = key.trim(); + final ProducerRecord record = new ProducerRecord<>( + topic, // Topic + key, // Key + new FooMessage(key, System.currentTimeMillis()) // Value + ); + + return send(record); + } + @PostMapping(path = "/") public DeferredResult greeting( @RequestBody String name) diff --git a/src/test/java/de/juplo/kafka/ApplicationTests.java b/src/test/java/de/juplo/kafka/ApplicationTests.java index 71625fd..cd7d928 100644 --- a/src/test/java/de/juplo/kafka/ApplicationTests.java +++ b/src/test/java/de/juplo/kafka/ApplicationTests.java @@ -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 { -- 2.20.1