K - the type of key for records consumed from KafkaV - the type of value for records consumed from Kafkapublic final class CatchAllExceptionConsumerRecordHandler<K,V> extends Object implements ConsumerRecordHandler<K,V>
ConsumerRecordHandler to catch and swallow all the exceptions throw from the
wrapped ConsumerRecordHandler when it failed to handle a consumed record.
This handler seems good to improve the availability of the consumer because it can swallow all the exceptions on handling a record and carry on to handle next record. But it actually can compromise the consumer to prevent a livelock, where the application did not crash but fails to make progress for some reason. Please use it judiciously. Usually fail fast, let the polling thread exit on exception, is your best choice.
| Constructor and Description |
|---|
CatchAllExceptionConsumerRecordHandler(ConsumerRecordHandler<K,V> wrappedHandler)
Constructor for
CatchAllExceptionConsumerRecordHandler to just log the failed record when
the wrapped handler failed on calling ConsumerRecordHandler.handleRecord(ConsumerRecord). |
CatchAllExceptionConsumerRecordHandler(ConsumerRecordHandler<K,V> wrappedHandler,
BiConsumer<org.apache.kafka.clients.consumer.ConsumerRecord<K,V>,Throwable> errorConsumer)
Constructor for
CatchAllExceptionConsumerRecordHandler to use a BiConsumer to handle the
failed record when the wrapped handler failed on calling ConsumerRecordHandler.handleRecord(ConsumerRecord). |
| Modifier and Type | Method and Description |
|---|---|
void |
handleRecord(org.apache.kafka.clients.consumer.ConsumerRecord<K,V> record)
Handle a
ConsumerRecord consumed from Kafka broker. |
public CatchAllExceptionConsumerRecordHandler(ConsumerRecordHandler<K,V> wrappedHandler)
CatchAllExceptionConsumerRecordHandler to just log the failed record when
the wrapped handler failed on calling ConsumerRecordHandler.handleRecord(ConsumerRecord).wrappedHandler - the wrapped ConsumerRecordHandler.public CatchAllExceptionConsumerRecordHandler(ConsumerRecordHandler<K,V> wrappedHandler, BiConsumer<org.apache.kafka.clients.consumer.ConsumerRecord<K,V>,Throwable> errorConsumer)
CatchAllExceptionConsumerRecordHandler to use a BiConsumer to handle the
failed record when the wrapped handler failed on calling ConsumerRecordHandler.handleRecord(ConsumerRecord).wrappedHandler - the wrapped ConsumerRecordHandler.errorConsumer - a BiConsumer to consume the failed record and the exception thrown from
the ConsumerRecordHandler.handleRecord(ConsumerRecord)public void handleRecord(org.apache.kafka.clients.consumer.ConsumerRecord<K,V> record)
ConsumerRecordHandlerConsumerRecord consumed from Kafka broker.
For the sake of fail fast, if any exception throws from this method, the thread for fetching records from Kafka broker will exit with an error message. This will trigger rebalances of partitions among all the consumer groups this consumer joined and let other consumer try to handle the failed and the following records.
You can consider to use RetriableConsumerRecordHandler to retry handling process automatically.
Or use CatchAllExceptionConsumerRecordHandler as a safety net to handle all the failed records in
the same way.
handleRecord in interface ConsumerRecordHandler<K,V>record - the consumed ConsumerRecordCopyright © 2020 LeanCloud. All rights reserved.