From f9223ab42c58bf39865fa85dfa6fdaea9ca24b03 Mon Sep 17 00:00:00 2001 From: Kai Moritz Date: Sat, 28 May 2022 16:55:44 +0200 Subject: [PATCH] Springify: Can send multiple types of messages * `ClientMessage` is send as `message` * `Greeting` is send as `greeting` --- src/main/java/de/juplo/kafka/Greeting.java | 13 +++++++ .../java/de/juplo/kafka/RestProducer.java | 39 ++++++++++++++----- .../java/de/juplo/kafka/ApplicationTests.java | 11 ++++++ 3 files changed, 53 insertions(+), 10 deletions(-) create mode 100644 src/main/java/de/juplo/kafka/Greeting.java diff --git a/src/main/java/de/juplo/kafka/Greeting.java b/src/main/java/de/juplo/kafka/Greeting.java new file mode 100644 index 0000000..2df15a3 --- /dev/null +++ b/src/main/java/de/juplo/kafka/Greeting.java @@ -0,0 +1,13 @@ +package de.juplo.kafka; + +import lombok.Value; + +import java.time.LocalDateTime; + + +@Value +public class Greeting +{ + private final String name; + private final LocalDateTime when = LocalDateTime.now(); +} diff --git a/src/main/java/de/juplo/kafka/RestProducer.java b/src/main/java/de/juplo/kafka/RestProducer.java index bccc166..6c852ce 100644 --- a/src/main/java/de/juplo/kafka/RestProducer.java +++ b/src/main/java/de/juplo/kafka/RestProducer.java @@ -20,7 +20,7 @@ public class RestProducer { private final String id; private final String topic; - private final KafkaProducer producer; + private final KafkaProducer producer; private long produced = 0; @@ -40,26 +40,46 @@ public class RestProducer props.put("compression.type", properties.getCompressionType()); props.put("key.serializer", StringSerializer.class.getName()); props.put("value.serializer", JsonSerializer.class.getName()); - props.put(JsonSerializer.TYPE_MAPPINGS, "message:" + ClientMessage.class.getName()); + props.put(JsonSerializer.TYPE_MAPPINGS, + "message:" + ClientMessage.class.getName() + "," + + "greeting:" + Greeting.class.getName()); this.producer = new KafkaProducer<>(props); } @PostMapping(path = "{key}") - public DeferredResult send( + public DeferredResult message( @PathVariable String key, @RequestBody String value) { - DeferredResult result = new DeferredResult<>(); - - final long time = System.currentTimeMillis(); - - final ProducerRecord record = new ProducerRecord<>( + final ProducerRecord record = new ProducerRecord<>( topic, // Topic key, // Key new ClientMessage(key, value) // Value ); + return send(record); + } + + @PostMapping(path = "/") + public DeferredResult greeting( + @RequestBody String name) + { + final ProducerRecord record = new ProducerRecord<>( + topic, // Topic + name, // Key + new Greeting(name) // Value + ); + + return send(record); + } + + private DeferredResult send(ProducerRecord record) + { + DeferredResult result = new DeferredResult<>(); + + final long time = System.currentTimeMillis(); + producer.send(record, (metadata, e) -> { long now = System.currentTimeMillis(); @@ -96,9 +116,8 @@ public class RestProducer long now = System.currentTimeMillis(); log.trace( - "{} - Queued #{} key={} latency={}ms", + "{} - Queued key={} latency={}ms", id, - value, record.key(), now - time ); diff --git a/src/test/java/de/juplo/kafka/ApplicationTests.java b/src/test/java/de/juplo/kafka/ApplicationTests.java index d872c2f..71625fd 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 testSendGreeting() throws Exception + { + mockMvc + .perform(post("/").content("peter")) + .andExpect(status().isOk()); + await("Message was send") + .atMost(Duration.ofSeconds(5)) + .until(() -> consumer.received.size() == 1); + } + static class Consumer { -- 2.20.1