1 package de.juplo.kafka;
3 import lombok.extern.slf4j.Slf4j;
4 import org.apache.kafka.clients.consumer.ConsumerRecord;
5 import org.apache.kafka.clients.consumer.ConsumerRecords;
6 import org.apache.kafka.clients.consumer.KafkaConsumer;
7 import org.apache.kafka.common.errors.WakeupException;
8 import org.apache.kafka.common.serialization.StringDeserializer;
10 import javax.annotation.PreDestroy;
11 import java.time.Duration;
12 import java.util.Arrays;
13 import java.util.Properties;
14 import java.util.concurrent.ExecutionException;
15 import java.util.concurrent.ExecutorService;
16 import java.util.concurrent.Future;
17 import java.util.concurrent.atomic.AtomicBoolean;
21 public class EndlessConsumer implements Runnable
23 private final ExecutorService executor;
24 private final String bootstrapServer;
25 private final String groupId;
26 private final String id;
27 private final String topic;
29 private AtomicBoolean running = new AtomicBoolean();
30 private long consumed = 0;
31 private KafkaConsumer<String, String> consumer = null;
32 private Future<?> future = null;
34 public EndlessConsumer(
35 ExecutorService executor,
36 String bootstrapServer,
41 this.executor = executor;
42 this.bootstrapServer = bootstrapServer;
43 this.groupId = groupId;
53 Properties props = new Properties();
54 props.put("bootstrap.servers", bootstrapServer);
55 props.put("group.id", groupId);
56 props.put("client.id", id);
57 props.put("auto.offset.reset", "earliest");
58 props.put("key.deserializer", StringDeserializer.class.getName());
59 props.put("value.deserializer", StringDeserializer.class.getName());
61 this.consumer = new KafkaConsumer<>(props);
63 log.info("{} - Subscribing to topic {}", id, topic);
64 consumer.subscribe(Arrays.asList(topic));
68 ConsumerRecords<String, String> records =
69 consumer.poll(Duration.ofSeconds(1));
71 // Do something with the data...
72 log.info("{} - Received {} messages", id, records.count());
73 for (ConsumerRecord<String, String> record : records)
77 "{} - {}: {}/{} - {}={}",
88 catch(WakeupException e)
90 log.info("{} - RIIING!", id);
94 log.error("{} - Unexpected error: {}", id, e.toString(), e);
95 running.set(false); // Mark the instance as not running
99 log.info("{} - Closing the KafkaConsumer", id);
101 log.info("{} - Consumer-Thread exiting", id);
106 public synchronized void start()
108 boolean stateChanged = running.compareAndSet(false, true);
110 throw new RuntimeException("Consumer instance " + id + " is already running!");
112 log.info("{} - Starting - consumed {} messages before", id, consumed);
113 future = executor.submit(this);
116 public synchronized void stop() throws ExecutionException, InterruptedException
118 boolean stateChanged = running.compareAndSet(true, false);
120 throw new RuntimeException("Consumer instance " + id + " is not running!");
122 log.info("{} - Stopping", id);
125 log.info("{} - Stopped - consumed {} messages so far", id, consumed);
129 public void destroy() throws ExecutionException, InterruptedException
131 log.info("{} - Destroy!", id);
136 catch (IllegalStateException e)
138 log.info("{} - Was already stopped", id);
142 log.info("{}: Consumed {} messages in total, exiting!", id, consumed);