org.apache.kafka.clients.producer
Class MockProducer

java.lang.Object
  extended by org.apache.kafka.clients.producer.MockProducer
All Implemented Interfaces:
java.io.Closeable, Producer<byte[],byte[]>

public class MockProducer
extends java.lang.Object
implements Producer<byte[],byte[]>

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
MockProducer()
          Create a new auto completing mock producer Equivalent to new MockProducer(true)
MockProducer(boolean autoComplete)
          Create a new mock producer with invented metadata the given autoComplete setting.
MockProducer(Cluster cluster, boolean autoComplete)
          Create a mock producer
 
Method Summary
 void clear()
          Clear the stored history of sent records
 void close()
          Close this producer
 boolean completeNext()
          Complete the earliest uncompleted call successfully.
 boolean errorNext(java.lang.RuntimeException e)
          Complete the earliest uncompleted call with the given error.
 java.util.List<ProducerRecord<byte[],byte[]>> history()
          Get the list of sent records since the last call to clear()
 java.util.Map<MetricName,Metric> metrics()
          Return a map of metrics maintained by the producer
 java.util.List<PartitionInfo> partitionsFor(java.lang.String topic)
          Get a list of partitions for the given topic for custom partition assignment.
 java.util.concurrent.Future<RecordMetadata> send(ProducerRecord<byte[],byte[]> record)
          Adds the record to the list of sent records.
 java.util.concurrent.Future<RecordMetadata> send(ProducerRecord<byte[],byte[]> record, Callback callback)
          Adds the record to the list of sent records.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MockProducer

public MockProducer(Cluster cluster,
                    boolean autoComplete)
Create a mock producer

Parameters:
cluster - The cluster holding metadata for this producer
autoComplete - If true automatically complete all requests successfully and execute the callback. Otherwise the user must call completeNext() or errorNext(RuntimeException) after send() to complete the call and unblock the @{link java.util.concurrent.Future Future<RecordMetadata>} that is returned.

MockProducer

public MockProducer(boolean autoComplete)
Create a new mock producer with invented metadata the given autoComplete setting. Equivalent to new MockProducer(null, autoComplete)


MockProducer

public MockProducer()
Create a new auto completing mock producer Equivalent to new MockProducer(true)

Method Detail

send

public java.util.concurrent.Future<RecordMetadata> send(ProducerRecord<byte[],byte[]> record)
Adds the record to the list of sent records. The RecordMetadata returned will be immediately satisfied.

Specified by:
send in interface Producer<byte[],byte[]>
Parameters:
record - The record to send
Returns:
A future which will eventually contain the response information
See Also:
history()

send

public java.util.concurrent.Future<RecordMetadata> send(ProducerRecord<byte[],byte[]> record,
                                                        Callback callback)
Adds the record to the list of sent records.

Specified by:
send in interface Producer<byte[],byte[]>
See Also:
history()

partitionsFor

public java.util.List<PartitionInfo> partitionsFor(java.lang.String topic)
Description copied from interface: Producer
Get a list of partitions for the given topic for custom partition assignment. The partition metadata will change over time so this list should not be cached.

Specified by:
partitionsFor in interface Producer<byte[],byte[]>

metrics

public java.util.Map<MetricName,Metric> metrics()
Description copied from interface: Producer
Return a map of metrics maintained by the producer

Specified by:
metrics in interface Producer<byte[],byte[]>

close

public void close()
Description copied from interface: Producer
Close this producer

Specified by:
close in interface java.io.Closeable
Specified by:
close in interface Producer<byte[],byte[]>

history

public java.util.List<ProducerRecord<byte[],byte[]>> history()
Get the list of sent records since the last call to clear()


clear

public void clear()
Clear the stored history of sent records


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