Class CommonMixedErrorHandler
java.lang.Object
org.springframework.kafka.listener.CommonMixedErrorHandler
- All Implemented Interfaces:
CommonErrorHandler
,DeliveryAttemptAware
A
CommonErrorHandler
that delegates to different CommonErrorHandler
s
for record and batch listeners.- Since:
- 2.8
- Author:
- Gary Russell
-
Constructor Summary
ConstructorDescriptionCommonMixedErrorHandler
(CommonErrorHandler recordErrorHandler, CommonErrorHandler batchErrorHandler) Construct an instance with the provided delegateCommonErrorHandler
s. -
Method Summary
Modifier and TypeMethodDescriptionvoid
Optional method to clear thread state; will be called just before a consumer thread terminates.int
deliveryAttempt
(TopicPartitionOffset topicPartitionOffset) Return the next delivery attempt for the topic/partition/offset.boolean
Return true if this error handler supports delivery attempts headers.void
handleBatch
(Exception thrownException, org.apache.kafka.clients.consumer.ConsumerRecords<?, ?> data, org.apache.kafka.clients.consumer.Consumer<?, ?> consumer, MessageListenerContainer container, Runnable invokeListener) Handle the exception for a batch listener.void
handleOtherException
(Exception thrownException, org.apache.kafka.clients.consumer.Consumer<?, ?> consumer, MessageListenerContainer container, boolean batchListener) Called when an exception is thrown with no records available, e.g.void
handleRecord
(Exception thrownException, org.apache.kafka.clients.consumer.ConsumerRecord<?, ?> record, org.apache.kafka.clients.consumer.Consumer<?, ?> consumer, MessageListenerContainer container) Handle the exception for a record listener whenCommonErrorHandler.remainingRecords()
returns false.void
handleRemaining
(Exception thrownException, List<org.apache.kafka.clients.consumer.ConsumerRecord<?, ?>> records, org.apache.kafka.clients.consumer.Consumer<?, ?> consumer, MessageListenerContainer container) Handle the exception for a record listener whenCommonErrorHandler.remainingRecords()
returns true.boolean
Return true if the offset should be committed for a handled error (no exception thrown).boolean
Return false if this error handler should only receive the current failed record; remaining records will be passed to the listener after the error handler returns.void
setAckAfterHandle
(boolean ack) Set to false to prevent the container from committing the offset of a recovered record (when the error handler does not itself throw an exception).
-
Constructor Details
-
CommonMixedErrorHandler
public CommonMixedErrorHandler(CommonErrorHandler recordErrorHandler, CommonErrorHandler batchErrorHandler) Construct an instance with the provided delegateCommonErrorHandler
s.- Parameters:
recordErrorHandler
- the error handler for record listeners.batchErrorHandler
- the error handler for batch listeners.
-
-
Method Details
-
remainingRecords
public boolean remainingRecords()Description copied from interface:CommonErrorHandler
Return false if this error handler should only receive the current failed record; remaining records will be passed to the listener after the error handler returns. When true (default), all remaining records including the failed record are passed to the error handler.- Specified by:
remainingRecords
in interfaceCommonErrorHandler
- Returns:
- false to receive only the failed record.
- See Also:
-
deliveryAttemptHeader
public boolean deliveryAttemptHeader()Description copied from interface:CommonErrorHandler
Return true if this error handler supports delivery attempts headers.- Specified by:
deliveryAttemptHeader
in interfaceCommonErrorHandler
- Returns:
- true if capable.
-
handleOtherException
public void handleOtherException(Exception thrownException, org.apache.kafka.clients.consumer.Consumer<?, ?> consumer, MessageListenerContainer container, boolean batchListener) Description copied from interface:CommonErrorHandler
Called when an exception is thrown with no records available, e.g. if the consumer poll throws an exception.- Specified by:
handleOtherException
in interfaceCommonErrorHandler
- Parameters:
thrownException
- the exception.consumer
- the consumer.container
- the container.batchListener
- true if the listener is a batch listener.
-
handleRecord
public void handleRecord(Exception thrownException, org.apache.kafka.clients.consumer.ConsumerRecord<?, ?> record, org.apache.kafka.clients.consumer.Consumer<?, ?> consumer, MessageListenerContainer container) Description copied from interface:CommonErrorHandler
Handle the exception for a record listener whenCommonErrorHandler.remainingRecords()
returns false. Use this to handle just the single failed record; remaining records from the poll will be sent to the listener.- Specified by:
handleRecord
in interfaceCommonErrorHandler
- Parameters:
thrownException
- the exception.record
- the record.consumer
- the consumer.container
- the container.- See Also:
-
handleRemaining
public void handleRemaining(Exception thrownException, List<org.apache.kafka.clients.consumer.ConsumerRecord<?, ?>> records, org.apache.kafka.clients.consumer.Consumer<?, ?> consumer, MessageListenerContainer container) Description copied from interface:CommonErrorHandler
Handle the exception for a record listener whenCommonErrorHandler.remainingRecords()
returns true. The failed record and all the remaining records from the poll are passed in. Usually used when the error handler performs seeks so that the remaining records will be redelivered on the next poll.- Specified by:
handleRemaining
in interfaceCommonErrorHandler
- Parameters:
thrownException
- the exception.records
- the remaining records including the one that failed.consumer
- the consumer.container
- the container.- See Also:
-
handleBatch
public void handleBatch(Exception thrownException, org.apache.kafka.clients.consumer.ConsumerRecords<?, ?> data, org.apache.kafka.clients.consumer.Consumer<?, ?> consumer, MessageListenerContainer container, Runnable invokeListener) Description copied from interface:CommonErrorHandler
Handle the exception for a batch listener. The completeConsumerRecords
from the poll is supplied. The error handler needs to perform seeks if you wish to reprocess the records in the batch.- Specified by:
handleBatch
in interfaceCommonErrorHandler
- Parameters:
thrownException
- the exception.data
- the consumer records.consumer
- the consumer.container
- the container.invokeListener
- a callback to re-invoke the listener.
-
deliveryAttempt
Description copied from interface:DeliveryAttemptAware
Return the next delivery attempt for the topic/partition/offset.- Specified by:
deliveryAttempt
in interfaceCommonErrorHandler
- Specified by:
deliveryAttempt
in interfaceDeliveryAttemptAware
- Parameters:
topicPartitionOffset
- the topic/partition/offset.- Returns:
- the next delivery attempt.
-
clearThreadState
public void clearThreadState()Description copied from interface:CommonErrorHandler
Optional method to clear thread state; will be called just before a consumer thread terminates.- Specified by:
clearThreadState
in interfaceCommonErrorHandler
-
isAckAfterHandle
public boolean isAckAfterHandle()Description copied from interface:CommonErrorHandler
Return true if the offset should be committed for a handled error (no exception thrown).- Specified by:
isAckAfterHandle
in interfaceCommonErrorHandler
- Returns:
- true to commit.
-
setAckAfterHandle
public void setAckAfterHandle(boolean ack) Description copied from interface:CommonErrorHandler
Set to false to prevent the container from committing the offset of a recovered record (when the error handler does not itself throw an exception).- Specified by:
setAckAfterHandle
in interfaceCommonErrorHandler
- Parameters:
ack
- false to not commit.
-