Das `running`-Flag wird nicht mehr benötigt consumer/spring-consumer consumer/spring-consumer--2025-03-18--19-42 consumer/spring-consumer--2025-03-signal consumer/spring-consumer--2025-04-signal
authorKai Moritz <kai@juplo.de>
Sat, 15 Mar 2025 18:12:05 +0000 (19:12 +0100)
committerKai Moritz <kai@juplo.de>
Sat, 15 Mar 2025 18:12:05 +0000 (19:12 +0100)
src/main/java/de/juplo/kafka/ExampleConsumer.java

index 4f7151f..1f5a570 100644 (file)
@@ -19,7 +19,6 @@ public class ExampleConsumer implements Runnable
   private final Thread workerThread;
   private final Runnable closeCallback;
 
-  private volatile boolean running = false;
   private long consumed = 0;
 
 
@@ -47,9 +46,8 @@ public class ExampleConsumer implements Runnable
     {
       log.info("{} - Subscribing to topic {}", id, topic);
       consumer.subscribe(Arrays.asList(topic));
-      running = true;
 
-      while (running)
+      while (true)
       {
         ConsumerRecords<String, String> records = consumer.poll(Duration.ofSeconds(1));
 
@@ -98,9 +96,9 @@ public class ExampleConsumer implements Runnable
 
   public void shutdown() throws InterruptedException
   {
-    log.info("{} joining the worker-thread...", id);
-    running = false;
+    log.info("{} - Waking up the consumer", id);
     consumer.wakeup();
+    log.info("{} - Joining the worker thread", id);
     workerThread.join();
   }
 }