Class MockProducer<K,V>
- java.lang.Object
-
- org.apache.kafka.clients.producer.MockProducer<K,V>
-
- All Implemented Interfaces:
java.io.Closeable
,java.lang.AutoCloseable
,Producer<K,V>
public class MockProducer<K,V> extends java.lang.Object implements Producer<K,V>
This class is not supported.
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.
-
-
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 offsets, and transactional statevoid
close()
void
close(long timeout, java.util.concurrent.TimeUnit timeUnit)
boolean
closed()
long
commitCount()
void
commitTransaction()
boolean
completeNext()
Complete the earliest uncompleted call successfully.java.util.List<java.util.Map<java.lang.String,java.util.Map<TopicPartition,OffsetAndMetadata>>>
consumerGroupOffsetsHistory()
Get the list of committed consumer group offsets since the last call toclear()
boolean
errorNext(java.lang.RuntimeException e)
Complete the earliest uncompleted call with the given error.void
fenceProducer()
void
fenceProducerOnClose()
void
flush()
boolean
flushed()
java.util.List<ProducerRecord<K,V>>
history()
Get the list of sent records since the last call toclear()
void
initTransactions()
java.util.Map<MetricName,Metric>
metrics()
java.util.List<PartitionInfo>
partitionsFor(java.lang.String topic)
java.util.concurrent.Future<RecordMetadata>
send(ProducerRecord<K,V> record)
Adds the record to the list of sent records.java.util.concurrent.Future<RecordMetadata>
send(ProducerRecord<K,V> record, Callback callback)
Adds the record to the list of sent records.void
sendOffsetsToTransaction(java.util.Map<TopicPartition,OffsetAndMetadata> offsets, java.lang.String consumerGroupId)
boolean
sentOffsets()
void
setMockMetrics(MetricName name, Metric metric)
Set a mock metric for testing purposeboolean
transactionAborted()
boolean
transactionCommitted()
boolean
transactionInFlight()
boolean
transactionInitialized()
-
-
-
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(java.util.Map<TopicPartition,OffsetAndMetadata> offsets, java.lang.String consumerGroupId) 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 java.util.concurrent.Future<RecordMetadata> send(ProducerRecord<K,V> record)
Adds the record to the list of sent records. TheRecordMetadata
returned will be immediately satisfied.
-
send
public java.util.concurrent.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 java.util.List<PartitionInfo> partitionsFor(java.lang.String topic)
Description copied from interface:Producer
- Specified by:
partitionsFor
in interfaceProducer<K,V>
-
metrics
public java.util.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
-
close
public void close(long timeout, java.util.concurrent.TimeUnit timeUnit)
Description copied from interface:Producer
-
closed
public boolean closed()
-
fenceProducer
public void fenceProducer()
-
fenceProducerOnClose
public void fenceProducerOnClose()
-
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 java.util.List<ProducerRecord<K,V>> history()
Get the list of sent records since the last call toclear()
-
consumerGroupOffsetsHistory
public java.util.List<java.util.Map<java.lang.String,java.util.Map<TopicPartition,OffsetAndMetadata>>> consumerGroupOffsetsHistory()
Get the list of committed consumer group offsets since the last call toclear()
-
clear
public void clear()
Clear the stored history of sent records, consumer group offsets, and transactional state
-
completeNext
public boolean completeNext()
Complete the earliest uncompleted call successfully.- Returns:
- true if there was an uncompleted call to complete
-
errorNext
public boolean errorNext(java.lang.RuntimeException e)
Complete the earliest uncompleted call with the given error.- Returns:
- true if there was an uncompleted call to complete
-
-