Search in sources :

Example 1 with ProducerInterceptor

use of org.apache.kafka.clients.producer.ProducerInterceptor in project kafka by apache.

the class ProducerInterceptorsTest method testOnAcknowledgementWithErrorChain.

@Test
public void testOnAcknowledgementWithErrorChain() {
    List<ProducerInterceptor<Integer, String>> interceptorList = new ArrayList<>();
    AppendProducerInterceptor interceptor1 = new AppendProducerInterceptor("One");
    interceptorList.add(interceptor1);
    ProducerInterceptors<Integer, String> interceptors = new ProducerInterceptors<>(interceptorList);
    // verify that metadata contains both topic and partition
    interceptors.onSendError(producerRecord, new TopicPartition(producerRecord.topic(), producerRecord.partition()), new KafkaException("Test"));
    assertEquals(1, onErrorAckCount);
    assertEquals(1, onErrorAckWithTopicPartitionSetCount);
    // verify that metadata contains both topic and partition (because record already contains partition)
    interceptors.onSendError(producerRecord, null, new KafkaException("Test"));
    assertEquals(2, onErrorAckCount);
    assertEquals(2, onErrorAckWithTopicPartitionSetCount);
    // if producer record does not contain partition, interceptor should get partition == -1
    ProducerRecord<Integer, String> record2 = new ProducerRecord<>("test2", null, 1, "value");
    interceptors.onSendError(record2, null, new KafkaException("Test"));
    assertEquals(3, onErrorAckCount);
    assertEquals(3, onErrorAckWithTopicSetCount);
    assertEquals(2, onErrorAckWithTopicPartitionSetCount);
    // if producer record does not contain partition, but topic/partition is passed to
    // onSendError, then interceptor should get valid partition
    int reassignedPartition = producerRecord.partition() + 1;
    interceptors.onSendError(record2, new TopicPartition(record2.topic(), reassignedPartition), new KafkaException("Test"));
    assertEquals(4, onErrorAckCount);
    assertEquals(4, onErrorAckWithTopicSetCount);
    assertEquals(3, onErrorAckWithTopicPartitionSetCount);
    // if both record and topic/partition are null, interceptor should not receive metadata
    interceptors.onSendError(null, null, new KafkaException("Test"));
    assertEquals(5, onErrorAckCount);
    assertEquals(4, onErrorAckWithTopicSetCount);
    assertEquals(3, onErrorAckWithTopicPartitionSetCount);
    interceptors.close();
}
Also used : ArrayList(java.util.ArrayList) TopicPartition(org.apache.kafka.common.TopicPartition) ProducerRecord(org.apache.kafka.clients.producer.ProducerRecord) ProducerInterceptor(org.apache.kafka.clients.producer.ProducerInterceptor) KafkaException(org.apache.kafka.common.KafkaException) Test(org.junit.Test)

Example 2 with ProducerInterceptor

use of org.apache.kafka.clients.producer.ProducerInterceptor in project kafka by apache.

the class ProducerInterceptorsTest method testOnAcknowledgementChain.

@Test
public void testOnAcknowledgementChain() {
    List<ProducerInterceptor<Integer, String>> interceptorList = new ArrayList<>();
    // we are testing two different interceptors by configuring the same interceptor differently, which is not
    // how it would be done in KafkaProducer, but ok for testing interceptor callbacks
    AppendProducerInterceptor interceptor1 = new AppendProducerInterceptor("One");
    AppendProducerInterceptor interceptor2 = new AppendProducerInterceptor("Two");
    interceptorList.add(interceptor1);
    interceptorList.add(interceptor2);
    ProducerInterceptors<Integer, String> interceptors = new ProducerInterceptors<>(interceptorList);
    // verify onAck is called on all interceptors
    RecordMetadata meta = new RecordMetadata(tp, 0, 0, 0, 0, 0, 0);
    interceptors.onAcknowledgement(meta, null);
    assertEquals(2, onAckCount);
    // verify that onAcknowledgement exceptions do not propagate
    interceptor1.injectOnAcknowledgementError(true);
    interceptors.onAcknowledgement(meta, null);
    assertEquals(4, onAckCount);
    interceptor2.injectOnAcknowledgementError(true);
    interceptors.onAcknowledgement(meta, null);
    assertEquals(6, onAckCount);
    interceptors.close();
}
Also used : RecordMetadata(org.apache.kafka.clients.producer.RecordMetadata) ArrayList(java.util.ArrayList) ProducerInterceptor(org.apache.kafka.clients.producer.ProducerInterceptor) Test(org.junit.Test)

Aggregations

ArrayList (java.util.ArrayList)2 ProducerInterceptor (org.apache.kafka.clients.producer.ProducerInterceptor)2 Test (org.junit.Test)2 ProducerRecord (org.apache.kafka.clients.producer.ProducerRecord)1 RecordMetadata (org.apache.kafka.clients.producer.RecordMetadata)1 KafkaException (org.apache.kafka.common.KafkaException)1 TopicPartition (org.apache.kafka.common.TopicPartition)1