Class ProducerInterceptors<K,V>
java.lang.Object
org.apache.kafka.clients.producer.internals.ProducerInterceptors<K,V>
- All Implemented Interfaces:
Closeable,AutoCloseable
A container that holds the list
ProducerInterceptor
and wraps calls to the chain of custom interceptors.-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Closes every interceptor in a container.static <K,V> org.apache.kafka.common.TopicPartition extractTopicPartition(ProducerRecord<K, V> record) voidonAcknowledgement(RecordMetadata metadata, Exception exception) This method is called when the record sent to the server has been acknowledged, or when sending the record fails before it gets sent to the server.onSend(ProducerRecord<K, V> record) This is called when client sends the record to KafkaProducer, before key and value gets serialized.voidonSendError(ProducerRecord<K, V> record, org.apache.kafka.common.TopicPartition interceptTopicPartition, Exception exception) This method is called when sending the record fails in(ProducerRecord)method.
-
Constructor Details
-
ProducerInterceptors
-
-
Method Details
-
onSend
This is called when client sends the record to KafkaProducer, before key and value gets serialized. The method callsProducerInterceptor.onSend(ProducerRecord)method. ProducerRecord returned from the first interceptor's onSend() is passed to the second interceptor onSend(), and so on in the interceptor chain. The record returned from the last interceptor is returned from this method. This method does not throw exceptions. Exceptions thrown by any of interceptor methods are caught and ignored. If an interceptor in the middle of the chain, that normally modifies the record, throws an exception, the next interceptor in the chain will be called with a record returned by the previous interceptor that did not throw an exception.- Parameters:
record- the record from client- Returns:
- producer record to send to topic/partition
-
onAcknowledgement
This method is called when the record sent to the server has been acknowledged, or when sending the record fails before it gets sent to the server. This method callsProducerInterceptor.onAcknowledgement(RecordMetadata, Exception)method for each interceptor. This method does not throw exceptions. Exceptions thrown by any of interceptor methods are caught and ignored.- Parameters:
metadata- The metadata for the record that was sent (i.e. the partition and offset). If an error occurred, metadata will only contain valid topic and maybe partition.exception- The exception thrown during processing of this record. Null if no error occurred.
-
onSendError
public void onSendError(ProducerRecord<K, V> record, org.apache.kafka.common.TopicPartition interceptTopicPartition, Exception exception) This method is called when sending the record fails in(ProducerRecord)method. This method callsProducerInterceptor.onAcknowledgement(RecordMetadata, Exception)method for each interceptor- Parameters:
record- The record from clientinterceptTopicPartition- The topic/partition for the record if an error occurred after partition gets assigned; the topic part of interceptTopicPartition is the same as in record.exception- The exception thrown during processing of this record.
-
extractTopicPartition
public static <K,V> org.apache.kafka.common.TopicPartition extractTopicPartition(ProducerRecord<K, V> record) -
close
public void close()Closes every interceptor in a container.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable
-