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.locks.Condition;
17 import java.util.concurrent.locks.Lock;
18 import java.util.concurrent.locks.ReentrantLock;
22 public class EndlessConsumer implements Runnable
24 private final ExecutorService executor;
25 private final String bootstrapServer;
26 private final String groupId;
27 private final String id;
28 private final String topic;
29 private final String autoOffsetReset;
31 private final Lock lock = new ReentrantLock();
32 private final Condition condition = lock.newCondition();
33 private boolean running = false;
34 private long consumed = 0;
35 private KafkaConsumer<String, String> consumer = null;
38 public EndlessConsumer(
39 ExecutorService executor,
40 String bootstrapServer,
44 String autoOffsetReset)
46 this.executor = executor;
47 this.bootstrapServer = bootstrapServer;
48 this.groupId = groupId;
51 this.autoOffsetReset = autoOffsetReset;
59 Properties props = new Properties();
60 props.put("bootstrap.servers", bootstrapServer);
61 props.put("group.id", groupId);
62 props.put("client.id", id);
63 props.put("auto.offset.reset", autoOffsetReset);
64 props.put("key.deserializer", StringDeserializer.class.getName());
65 props.put("value.deserializer", StringDeserializer.class.getName());
67 this.consumer = new KafkaConsumer<>(props);
69 log.info("{} - Subscribing to topic {}", id, topic);
70 consumer.subscribe(Arrays.asList(topic));
74 ConsumerRecords<String, String> records =
75 consumer.poll(Duration.ofSeconds(1));
77 // Do something with the data...
78 log.info("{} - Received {} messages", id, records.count());
79 for (ConsumerRecord<String, String> record : records)
83 "{} - {}: {}/{} - {}={}",
94 catch(WakeupException e)
96 log.info("{} - RIIING!", id);
101 log.error("{} - Unexpected error: {}", id, e.toString(), e);
106 log.info("{} - Closing the KafkaConsumer", id);
108 log.info("{} - Consumer-Thread exiting", id);
112 private void shutdown()
132 throw new IllegalStateException("Consumer instance " + id + " is already running!");
134 log.info("{} - Starting - consumed {} messages before", id, consumed);
136 executor.submit(this);
144 public synchronized void stop() throws ExecutionException, InterruptedException
150 throw new IllegalStateException("Consumer instance " + id + " is not running!");
152 log.info("{} - Stopping", id);
155 log.info("{} - Stopped - consumed {} messages so far", id, consumed);
164 public void destroy() throws ExecutionException, InterruptedException
166 log.info("{} - Destroy!", id);
171 catch (IllegalStateException e)
173 log.info("{} - Was already stopped", id);
177 log.error("{} - Unexpected exception while trying to stop the consumer", id, e);
181 log.info("{}: Consumed {} messages in total, exiting!", id, consumed);