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.Optional;
14 import java.util.Properties;
15 import java.util.concurrent.ExecutionException;
16 import java.util.concurrent.ExecutorService;
17 import java.util.concurrent.locks.Condition;
18 import java.util.concurrent.locks.Lock;
19 import java.util.concurrent.locks.ReentrantLock;
23 public class EndlessConsumer implements Runnable
25 private final ExecutorService executor;
26 private final String bootstrapServer;
27 private final String groupId;
28 private final String id;
29 private final String topic;
30 private final String autoOffsetReset;
32 private final Lock lock = new ReentrantLock();
33 private final Condition condition = lock.newCondition();
34 private boolean running = false;
35 private Exception exception;
36 private long consumed = 0;
37 private KafkaConsumer<String, String> consumer = null;
40 public EndlessConsumer(
41 ExecutorService executor,
42 String bootstrapServer,
46 String autoOffsetReset)
48 this.executor = executor;
49 this.bootstrapServer = bootstrapServer;
50 this.groupId = groupId;
53 this.autoOffsetReset = autoOffsetReset;
61 Properties props = new Properties();
62 props.put("bootstrap.servers", bootstrapServer);
63 props.put("group.id", groupId);
64 props.put("client.id", id);
65 props.put("auto.offset.reset", autoOffsetReset);
66 props.put("key.deserializer", StringDeserializer.class.getName());
67 props.put("value.deserializer", StringDeserializer.class.getName());
69 this.consumer = new KafkaConsumer<>(props);
71 log.info("{} - Subscribing to topic {}", id, topic);
72 consumer.subscribe(Arrays.asList(topic));
76 ConsumerRecords<String, String> records =
77 consumer.poll(Duration.ofSeconds(1));
79 // Do something with the data...
80 log.info("{} - Received {} messages", id, records.count());
81 for (ConsumerRecord<String, String> record : records)
85 "{} - {}: {}/{} - {}={}",
96 catch(WakeupException e)
98 log.info("{} - RIIING!", id);
103 log.error("{} - Unexpected error: {}", id, e.toString(), e);
108 log.info("{} - Closing the KafkaConsumer", id);
110 log.info("{} - Consumer-Thread exiting", id);
114 private void shutdown()
119 private void shutdown(Exception e)
140 throw new IllegalStateException("Consumer instance " + id + " is already running!");
142 log.info("{} - Starting - consumed {} messages before", id, consumed);
145 executor.submit(this);
153 public synchronized void stop() throws ExecutionException, InterruptedException
159 throw new IllegalStateException("Consumer instance " + id + " is not running!");
161 log.info("{} - Stopping", id);
164 log.info("{} - Stopped - consumed {} messages so far", id, consumed);
173 public void destroy() throws ExecutionException, InterruptedException
175 log.info("{} - Destroy!", id);
180 catch (IllegalStateException e)
182 log.info("{} - Was already stopped", id);
186 log.error("{} - Unexpected exception while trying to stop the consumer", id, e);
190 log.info("{}: Consumed {} messages in total, exiting!", id, consumed);
194 public boolean running()
207 public Optional<Exception> exitStatus()
213 throw new IllegalStateException("No exit-status available: Consumer instance " + id + " is running!");
215 return Optional.ofNullable(exception);