From: Kai Moritz Date: Fri, 11 Nov 2022 16:33:11 +0000 (+0100) Subject: Vorbereitung für Überarbeitung als supersimple-producer X-Git-Tag: supersimple-producer---lvm-2-tage--easy-path~9 X-Git-Url: http://juplo.de/gitweb/?a=commitdiff_plain;h=907c9beca9823a0a03350c22c034346aa5476099;p=demos%2Fkafka%2Ftraining Vorbereitung für Überarbeitung als supersimple-producer * Da Git Dateien nur wiedererkenn, wenn sie unverändert verschoben werden, wird die Umbenennung hier vorgezogen. --- diff --git a/pom.xml b/pom.xml index 4c52f37..179103a 100644 --- a/pom.xml +++ b/pom.xml @@ -61,7 +61,7 @@ true libs/ - de.juplo.kafka.SimpleProducer + de.juplo.kafka.Application diff --git a/src/main/java/de/juplo/kafka/Application.java b/src/main/java/de/juplo/kafka/Application.java new file mode 100644 index 0000000..9fcaa6e --- /dev/null +++ b/src/main/java/de/juplo/kafka/Application.java @@ -0,0 +1,151 @@ +package de.juplo.kafka; + +import lombok.extern.slf4j.Slf4j; +import org.apache.kafka.clients.producer.KafkaProducer; +import org.apache.kafka.clients.producer.ProducerRecord; +import org.apache.kafka.common.serialization.StringSerializer; + +import java.util.Properties; + + +@Slf4j +public class SimpleProducer +{ + private final String id; + private final String topic; + private final KafkaProducer producer; + + private long produced = 0; + private volatile boolean running = true; + private volatile boolean done = false; + + public SimpleProducer(String broker, String topic, String clientId) + { + Properties props = new Properties(); + props.put("bootstrap.servers", broker); + props.put("client.id", clientId); // Nur zur Wiedererkennung + props.put("key.serializer", StringSerializer.class.getName()); + props.put("value.serializer", StringSerializer.class.getName()); + + producer = new KafkaProducer<>(props); + + this.topic = topic; + this.id = clientId; + } + + public void run() + { + long i = 0; + + try + { + for (; running ; i++) + { + send(Long.toString(i%10), Long.toString(i)); + Thread.sleep(500); + } + } + catch (InterruptedException e) {} + finally + { + log.info("{}: Closing the KafkaProducer", id); + producer.close(); + log.info("{}: Produced {} messages in total, exiting!", id, produced); + done = true; + } + } + + void send(String key, String value) + { + final long time = System.currentTimeMillis(); + + final ProducerRecord record = new ProducerRecord<>( + topic, // Topic + key, // Key + value // Value + ); + + producer.send(record, (metadata, e) -> + { + long now = System.currentTimeMillis(); + if (e == null) + { + // HANDLE SUCCESS + produced++; + 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 + 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 #{} key={} latency={}ms", + id, + value, + record.key(), + now - time + ); + } + + + public static void main(String[] args) throws Exception + { + String broker = ":9092"; + String topic = "test"; + String clientId = "DEV"; + + switch (args.length) + { + case 3: + clientId = args[2]; + case 2: + topic = args[1]; + case 1: + broker = args[0]; + } + + SimpleProducer instance = new SimpleProducer(broker, topic, clientId); + + Runtime.getRuntime().addShutdownHook(new Thread(() -> + { + instance.running = false; + while (!instance.done) + { + log.info("Waiting for main-thread..."); + try + { + Thread.sleep(1000); + } + catch (InterruptedException e) {} + } + log.info("Shutdown completed."); + })); + + log.info( + "Running SimpleProducer: broker={}, topic={}, client-id={}", + broker, + topic, + clientId); + instance.run(); + } +} diff --git a/src/main/java/de/juplo/kafka/SimpleProducer.java b/src/main/java/de/juplo/kafka/SimpleProducer.java deleted file mode 100644 index 9fcaa6e..0000000 --- a/src/main/java/de/juplo/kafka/SimpleProducer.java +++ /dev/null @@ -1,151 +0,0 @@ -package de.juplo.kafka; - -import lombok.extern.slf4j.Slf4j; -import org.apache.kafka.clients.producer.KafkaProducer; -import org.apache.kafka.clients.producer.ProducerRecord; -import org.apache.kafka.common.serialization.StringSerializer; - -import java.util.Properties; - - -@Slf4j -public class SimpleProducer -{ - private final String id; - private final String topic; - private final KafkaProducer producer; - - private long produced = 0; - private volatile boolean running = true; - private volatile boolean done = false; - - public SimpleProducer(String broker, String topic, String clientId) - { - Properties props = new Properties(); - props.put("bootstrap.servers", broker); - props.put("client.id", clientId); // Nur zur Wiedererkennung - props.put("key.serializer", StringSerializer.class.getName()); - props.put("value.serializer", StringSerializer.class.getName()); - - producer = new KafkaProducer<>(props); - - this.topic = topic; - this.id = clientId; - } - - public void run() - { - long i = 0; - - try - { - for (; running ; i++) - { - send(Long.toString(i%10), Long.toString(i)); - Thread.sleep(500); - } - } - catch (InterruptedException e) {} - finally - { - log.info("{}: Closing the KafkaProducer", id); - producer.close(); - log.info("{}: Produced {} messages in total, exiting!", id, produced); - done = true; - } - } - - void send(String key, String value) - { - final long time = System.currentTimeMillis(); - - final ProducerRecord record = new ProducerRecord<>( - topic, // Topic - key, // Key - value // Value - ); - - producer.send(record, (metadata, e) -> - { - long now = System.currentTimeMillis(); - if (e == null) - { - // HANDLE SUCCESS - produced++; - 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 - 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 #{} key={} latency={}ms", - id, - value, - record.key(), - now - time - ); - } - - - public static void main(String[] args) throws Exception - { - String broker = ":9092"; - String topic = "test"; - String clientId = "DEV"; - - switch (args.length) - { - case 3: - clientId = args[2]; - case 2: - topic = args[1]; - case 1: - broker = args[0]; - } - - SimpleProducer instance = new SimpleProducer(broker, topic, clientId); - - Runtime.getRuntime().addShutdownHook(new Thread(() -> - { - instance.running = false; - while (!instance.done) - { - log.info("Waiting for main-thread..."); - try - { - Thread.sleep(1000); - } - catch (InterruptedException e) {} - } - log.info("Shutdown completed."); - })); - - log.info( - "Running SimpleProducer: broker={}, topic={}, client-id={}", - broker, - topic, - clientId); - instance.run(); - } -}