From 84cd0721b0efaf3151104c355399b08302d7f366 Mon Sep 17 00:00:00 2001 From: Kai Moritz Date: Mon, 5 Dec 2022 20:35:50 +0100 Subject: [PATCH] =?utf8?q?Vorlage=20&=20Setup=20f=C3=BCr=20Fire-n-Forget-?= =?utf8?q?=C3=9Cbung=20=C3=BCberarbeitet?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- README.sh | 13 ++++------ docker/docker-compose.yml | 2 +- pom.xml | 2 +- .../java/de/juplo/kafka/SimpleProducer.java | 26 +------------------ 4 files changed, 8 insertions(+), 35 deletions(-) diff --git a/README.sh b/README.sh index 9d0f94f..b11792d 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 @@ -31,11 +31,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 495e21f..4d385c6 100644 --- a/docker/docker-compose.yml +++ b/docker/docker-compose.yml @@ -100,5 +100,5 @@ services: - setup producer: - image: juplo/simple-producer:1.0-SNAPSHOT + image: juplo/fire-forget:1.0-SNAPSHOT command: kafka:9092 test producer diff --git a/pom.xml b/pom.xml index 2928676..ab483d9 100644 --- a/pom.xml +++ b/pom.xml @@ -12,7 +12,7 @@ de.juplo.kafka - simple-producer + fire-forget Super Simple Producer 1.0-SNAPSHOT diff --git a/src/main/java/de/juplo/kafka/SimpleProducer.java b/src/main/java/de/juplo/kafka/SimpleProducer.java index db554d7..57796d0 100644 --- a/src/main/java/de/juplo/kafka/SimpleProducer.java +++ b/src/main/java/de/juplo/kafka/SimpleProducer.java @@ -17,8 +17,6 @@ public class SimpleProducer private final Producer producer; private long produced = 0; - private volatile boolean running = true; - private volatile boolean done = false; public SimpleProducer(String broker, String topic, String clientId) { @@ -35,23 +33,16 @@ public class SimpleProducer 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 (InterruptedException e) {} finally { - log.info("{}: Closing the KafkaProducer", id); - producer.close(); log.info("{}: Produced {} messages in total, exiting!", id, produced); - done = true; } } @@ -126,21 +117,6 @@ public class SimpleProducer 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, -- 2.20.1