I have noticed that KafkaConsumer advocates to use wakeup()
and custom boolean to close KafkaConsumer:
public class KafkaConsumerRunner implements Runnable {
private final AtomicBoolean closed = new AtomicBoolean(false);
private final KafkaConsumer consumer;
public KafkaConsumerRunner(KafkaConsumer consumer) {
this.consumer = consumer;
}
public void run() {
try {
consumer.subscribe(Arrays.asList("topic"));
while (!closed.get()) {
ConsumerRecords records = consumer.poll(Duration.ofMillis(10000));
// Handle new records
}
} catch (WakeupException e) {
// Ignore exception if closing
if (!closed.get()) throw e;
} finally {
consumer.close();
}
}
// Shutdown hook which can be called from a separate thread
public void shutdown() {
closed.set(true);
consumer.wakeup();
}
}
Why don't KafkaConsumer poll()
method throws jdk InterruptedException, then we can just handle InterruptedException, thus avoid to write shutdown method using these two steps:
closed.set(true);
consumer.wakeup();
Kafka explains that:
Note that while it is possible to use thread interrupts instead of wakeup() to abort a blocking operation (in which case, InterruptException will be raised), we discourage their use since they may cause a clean shutdown of the consumer to be aborted. Interrupts are mainly supported for those cases where using wakeup() is impossible, e.g. when a consumer thread is managed by code that is unaware of the Kafka client.
But indeed I'm not so aware of its meaning. Can anyone explain it more explicitly?
Besides, poll()
throws org.apache.kafka.common.errors.InterruptException
, which is an unchecked wrapper for jdk InterruptedException. Why bother using this new exception?
Any suggestion is appreciated.