Class MockProducer<K,V>
- java.lang.Object
-
- org.apache.kafka.clients.producer.MockProducer<K,V>
-
- All Implemented Interfaces:
Closeable
,AutoCloseable
,Producer<K,V>
public class MockProducer<K,V> extends Object implements Producer<K,V>
A mock of the producer interface you can use for testing code that uses Kafka.By default this mock will synchronously complete each send call successfully. However it can be configured to allow the user to control the completion of the call and supply an optional error for the producer to throw.
-
-
Field Summary
-
Constructor Summary
Constructors Constructor Description MockProducer()
Create a new mock producer with invented metadata.MockProducer(boolean autoComplete, Partitioner partitioner, Serializer<K> keySerializer, Serializer<V> valueSerializer)
Create a new mock producer with invented metadata the given autoComplete setting, partitioner and key\value serializers.MockProducer(boolean autoComplete, Serializer<K> keySerializer, Serializer<V> valueSerializer)
Create a new mock producer with invented metadata the given autoComplete setting and key\value serializers.MockProducer(Cluster cluster, boolean autoComplete, Partitioner partitioner, Serializer<K> keySerializer, Serializer<V> valueSerializer)
Create a mock producer
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
abortTransaction()
void
beginTransaction()
void
clear()
Clear the stored history of sent records, consumer group offsetsvoid
close()
void
close(Duration timeout)
boolean
closed()
long
commitCount()
void
commitTransaction()
boolean
completeNext()
Complete the earliest uncompleted call successfully.List<Map<String,Map<TopicPartition,OffsetAndMetadata>>>
consumerGroupOffsetsHistory()
Get the list of committed consumer group offsets since the last call toclear()
boolean
errorNext(RuntimeException e)
Complete the earliest uncompleted call with the given error.void
fenceProducer()
void
flush()
boolean
flushed()
List<ProducerRecord<K,V>>
history()
Get the list of sent records since the last call toclear()
void
initTransactions()
Map<MetricName,Metric>
metrics()
List<PartitionInfo>
partitionsFor(String topic)
Future<RecordMetadata>
send(ProducerRecord<K,V> record)
Adds the record to the list of sent records.Future<RecordMetadata>
send(ProducerRecord<K,V> record, Callback callback)
Adds the record to the list of sent records.void
sendOffsetsToTransaction(Map<TopicPartition,OffsetAndMetadata> offsets, String consumerGroupId)
void
sendOffsetsToTransaction(Map<TopicPartition,OffsetAndMetadata> offsets, ConsumerGroupMetadata groupMetadata)
boolean
sentOffsets()
void
setMockMetrics(MetricName name, Metric metric)
Set a mock metric for testing purposeboolean
transactionAborted()
boolean
transactionCommitted()
boolean
transactionInFlight()
boolean
transactionInitialized()
Map<String,Map<TopicPartition,OffsetAndMetadata>>
uncommittedOffsets()
List<ProducerRecord<K,V>>
uncommittedRecords()
-
-
-
Field Detail
-
initTransactionException
public RuntimeException initTransactionException
-
beginTransactionException
public RuntimeException beginTransactionException
-
sendOffsetsToTransactionException
public RuntimeException sendOffsetsToTransactionException
-
commitTransactionException
public RuntimeException commitTransactionException
-
abortTransactionException
public RuntimeException abortTransactionException
-
sendException
public RuntimeException sendException
-
flushException
public RuntimeException flushException
-
partitionsForException
public RuntimeException partitionsForException
-
closeException
public RuntimeException closeException
-
-
Constructor Detail
-
MockProducer
public MockProducer(Cluster cluster, boolean autoComplete, Partitioner partitioner, Serializer<K> keySerializer, Serializer<V> valueSerializer)
Create a mock producer- Parameters:
cluster
- The cluster holding metadata for this producerautoComplete
- If true automatically complete all requests successfully and execute the callback. Otherwise the user must callcompleteNext()
orerrorNext(RuntimeException)
aftersend()
to complete the call and unblock theFuture<RecordMetadata>
that is returned.partitioner
- The partition strategykeySerializer
- The serializer for key that implementsSerializer
.valueSerializer
- The serializer for value that implementsSerializer
.
-
MockProducer
public MockProducer(boolean autoComplete, Serializer<K> keySerializer, Serializer<V> valueSerializer)
Create a new mock producer with invented metadata the given autoComplete setting and key\value serializers. Equivalent toMockProducer(Cluster, boolean, Partitioner, Serializer, Serializer)
new MockProducer(Cluster.empty(), autoComplete, new DefaultPartitioner(), keySerializer, valueSerializer)}
-
MockProducer
public MockProducer(boolean autoComplete, Partitioner partitioner, Serializer<K> keySerializer, Serializer<V> valueSerializer)
Create a new mock producer with invented metadata the given autoComplete setting, partitioner and key\value serializers. Equivalent toMockProducer(Cluster, boolean, Partitioner, Serializer, Serializer)
new MockProducer(Cluster.empty(), autoComplete, partitioner, keySerializer, valueSerializer)}
-
MockProducer
public MockProducer()
Create a new mock producer with invented metadata. Equivalent toMockProducer(Cluster, boolean, Partitioner, Serializer, Serializer)
new MockProducer(Cluster.empty(), false, null, null, null)}
-
-
Method Detail
-
initTransactions
public void initTransactions()
Description copied from interface:Producer
- Specified by:
initTransactions
in interfaceProducer<K,V>
-
beginTransaction
public void beginTransaction() throws ProducerFencedException
Description copied from interface:Producer
- Specified by:
beginTransaction
in interfaceProducer<K,V>
- Throws:
ProducerFencedException
-
sendOffsetsToTransaction
public void sendOffsetsToTransaction(Map<TopicPartition,OffsetAndMetadata> offsets, String consumerGroupId) throws ProducerFencedException
Description copied from interface:Producer
- Specified by:
sendOffsetsToTransaction
in interfaceProducer<K,V>
- Throws:
ProducerFencedException
-
sendOffsetsToTransaction
public void sendOffsetsToTransaction(Map<TopicPartition,OffsetAndMetadata> offsets, ConsumerGroupMetadata groupMetadata) throws ProducerFencedException
Description copied from interface:Producer
- Specified by:
sendOffsetsToTransaction
in interfaceProducer<K,V>
- Throws:
ProducerFencedException
-
commitTransaction
public void commitTransaction() throws ProducerFencedException
Description copied from interface:Producer
- Specified by:
commitTransaction
in interfaceProducer<K,V>
- Throws:
ProducerFencedException
-
abortTransaction
public void abortTransaction() throws ProducerFencedException
Description copied from interface:Producer
- Specified by:
abortTransaction
in interfaceProducer<K,V>
- Throws:
ProducerFencedException
-
send
public Future<RecordMetadata> send(ProducerRecord<K,V> record)
Adds the record to the list of sent records. TheRecordMetadata
returned will be immediately satisfied.
-
send
public Future<RecordMetadata> send(ProducerRecord<K,V> record, Callback callback)
Adds the record to the list of sent records.
-
flush
public void flush()
Description copied from interface:Producer
-
partitionsFor
public List<PartitionInfo> partitionsFor(String topic)
Description copied from interface:Producer
- Specified by:
partitionsFor
in interfaceProducer<K,V>
-
metrics
public Map<MetricName,Metric> metrics()
Description copied from interface:Producer
-
setMockMetrics
public void setMockMetrics(MetricName name, Metric metric)
Set a mock metric for testing purpose
-
close
public void close()
Description copied from interface:Producer
-
closed
public boolean closed()
-
fenceProducer
public void fenceProducer()
-
transactionInitialized
public boolean transactionInitialized()
-
transactionInFlight
public boolean transactionInFlight()
-
transactionCommitted
public boolean transactionCommitted()
-
transactionAborted
public boolean transactionAborted()
-
flushed
public boolean flushed()
-
sentOffsets
public boolean sentOffsets()
-
commitCount
public long commitCount()
-
history
public List<ProducerRecord<K,V>> history()
Get the list of sent records since the last call toclear()
-
uncommittedRecords
public List<ProducerRecord<K,V>> uncommittedRecords()
-
consumerGroupOffsetsHistory
public List<Map<String,Map<TopicPartition,OffsetAndMetadata>>> consumerGroupOffsetsHistory()
Get the list of committed consumer group offsets since the last call toclear()
-
uncommittedOffsets
public Map<String,Map<TopicPartition,OffsetAndMetadata>> uncommittedOffsets()
-
clear
public void clear()
Clear the stored history of sent records, consumer group offsets
-
completeNext
public boolean completeNext()
Complete the earliest uncompleted call successfully.- Returns:
- true if there was an uncompleted call to complete
-
errorNext
public boolean errorNext(RuntimeException e)
Complete the earliest uncompleted call with the given error.- Returns:
- true if there was an uncompleted call to complete
-
-