From: Kai Moritz Date: Mon, 5 Dec 2022 19:35:50 +0000 (+0100) Subject: Vorlage & Setup für Fire-n-Forget-Übung überarbeitet X-Git-Url: http://juplo.de/gitweb/?a=commitdiff_plain;h=d1a3c412999501164bff6ad4b43713d3151d586d;p=demos%2Fkafka%2Ftraining Vorlage & Setup für Fire-n-Forget-Übung überarbeitet --- diff --git a/README.sh b/README.sh index 97ca7cc..32c0ecd 100755 --- a/README.sh +++ b/README.sh @@ -1,6 +1,6 @@ #!/bin/bash -IMAGE=juplo/simple-producer:1.0-SNAPSHOT +IMAGE=juplo/fire-forget:1.0-SNAPSHOT if [ "$1" = "cleanup" ] then @@ -30,11 +30,8 @@ sleep 1 docker compose -f docker/docker-compose.yml logs setup docker compose -f docker/docker-compose.yml ps -docker compose -f docker/docker-compose.yml up -d producer -sleep 5 +docker compose -f docker/docker-compose.yml up producer -docker compose -f docker/docker-compose.yml exec cli kafkacat -b kafka:9092 -t test -c 20 -f'topic=%t\tpartition=%p\toffset=%o\tkey=%k\tvalue=%s\n' - -docker compose -f docker/docker-compose.yml stop producer -docker compose -f docker/docker-compose.yml exec cli kafkacat -b kafka:9092 -t test -e -f'topic=%t\tpartition=%p\toffset=%o\tkey=%k\tvalue=%s\n' -docker compose -f docker/docker-compose.yml logs producer +# tag::kafkacat[] +kafkacat -b :9092 -e -q -t test | wc -l +# end::kafkacat[] diff --git a/docker/docker-compose.yml b/docker/docker-compose.yml index 531a116..fc5e67d 100644 --- a/docker/docker-compose.yml +++ b/docker/docker-compose.yml @@ -188,7 +188,7 @@ services: - kafka-3 producer: - image: juplo/simple-producer:1.0-SNAPSHOT + image: juplo/fire-forget:1.0-SNAPSHOT command: kafka:9092 test producer volumes: diff --git a/pom.xml b/pom.xml index ad7f17a..61a2d24 100644 --- a/pom.xml +++ b/pom.xml @@ -12,7 +12,7 @@ de.juplo.kafka - simple-producer + fire-forget Super Simple Producer A Simple Producer, programmed with pure Java, that sends messages via Kafka 1.0-SNAPSHOT diff --git a/src/main/java/de/juplo/kafka/ExampleProducer.java b/src/main/java/de/juplo/kafka/ExampleProducer.java index 06e14ef..4191620 100644 --- a/src/main/java/de/juplo/kafka/ExampleProducer.java +++ b/src/main/java/de/juplo/kafka/ExampleProducer.java @@ -16,8 +16,6 @@ public class ExampleProducer private final String topic; private final Producer producer; - private volatile boolean running = true; - private volatile boolean done = false; private long produced = 0; public ExampleProducer(String broker, String topic, String clientId) @@ -35,14 +33,11 @@ public class ExampleProducer public void run() { - long i = 0; - try { - for (; running; i++) + for (int i = 0; i < 10000 ; i++) { send(Long.toString(i%10), Long.toString(i)); - Thread.sleep(500); } } catch (Exception e) @@ -51,10 +46,7 @@ public class ExampleProducer } finally { - log.info("{}: Closing the KafkaProducer", id); - producer.close(); log.info("{}: Produced {} messages in total, exiting!", id, produced); - done = true; } } @@ -128,21 +120,6 @@ public class ExampleProducer ExampleProducer instance = new ExampleProducer(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 ExampleProducer: broker={}, topic={}, client-id={}", broker,