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;
28 private final String autoOffsetReset;
30 private AtomicBoolean running = new AtomicBoolean();
31 private long consumed = 0;
32 private KafkaConsumer<String, String> consumer = null;
33 private Future<?> future = null;
35 public EndlessConsumer(
36 ExecutorService executor,
37 String bootstrapServer,
41 String autoOffsetReset)
43 this.executor = executor;
44 this.bootstrapServer = bootstrapServer;
45 this.groupId = groupId;
48 this.autoOffsetReset = autoOffsetReset;
56 Properties props = new Properties();
57 props.put("bootstrap.servers", bootstrapServer);
58 props.put("group.id", groupId);
59 props.put("client.id", id);
60 props.put("auto.offset.reset", autoOffsetReset);
61 props.put("key.deserializer", StringDeserializer.class.getName());
62 props.put("value.deserializer", StringDeserializer.class.getName());
64 this.consumer = new KafkaConsumer<>(props);
66 log.info("{} - Subscribing to topic {}", id, topic);
67 consumer.subscribe(Arrays.asList(topic));
71 ConsumerRecords<String, String> records =
72 consumer.poll(Duration.ofSeconds(1));
74 // Do something with the data...
75 log.info("{} - Received {} messages", id, records.count());
76 for (ConsumerRecord<String, String> record : records)
80 "{} - {}: {}/{} - {}={}",
91 catch(WakeupException e)
93 log.info("{} - RIIING!", id);
97 log.error("{} - Unexpected error: {}", id, e.toString(), e);
98 running.set(false); // Mark the instance as not running
102 log.info("{} - Closing the KafkaConsumer", id);
104 log.info("{} - Consumer-Thread exiting", id);
109 public synchronized void start()
111 boolean stateChanged = running.compareAndSet(false, true);
113 throw new RuntimeException("Consumer instance " + id + " is already running!");
115 log.info("{} - Starting - consumed {} messages before", id, consumed);
116 future = executor.submit(this);
119 public synchronized void stop() throws ExecutionException, InterruptedException
121 boolean stateChanged = running.compareAndSet(true, false);
123 throw new RuntimeException("Consumer instance " + id + " is not running!");
125 log.info("{} - Stopping", id);
128 log.info("{} - Stopped - consumed {} messages so far", id, consumed);
132 public void destroy() throws ExecutionException, InterruptedException
134 log.info("{} - Destroy!", id);
139 catch (IllegalStateException e)
141 log.info("{} - Was already stopped", id);
145 log.info("{}: Consumed {} messages in total, exiting!", id, consumed);