org.apache.kafka.clients.consumer
Class MockConsumer

java.lang.Object
  extended by org.apache.kafka.clients.consumer.MockConsumer
All Implemented Interfaces:
java.io.Closeable, Consumer

public class MockConsumer
extends java.lang.Object
implements Consumer

A mock of the Consumer interface you can use for testing code that uses Kafka. This class is not threadsafe

The consumer runs in the user thread and multiplexes I/O over TCP connections to each of the brokers it needs to communicate with. Failure to close the consumer after use will leak these resources.


Constructor Summary
MockConsumer()
           
 
Method Summary
 void close()
          Close this consumer
 OffsetMetadata commit(boolean sync)
          Commits offsets returned on the last poll() for the subscribed list of topics and partitions.
 OffsetMetadata commit(java.util.Map<TopicPartition,java.lang.Long> offsets, boolean sync)
          Commits the specified offsets for the specified list of topics and partitions to Kafka.
 java.util.Map<TopicPartition,java.lang.Long> committed(java.util.Collection<TopicPartition> partitions)
          Fetches the last committed offsets for the input list of partitions
 java.util.Map<java.lang.String,? extends Metric> metrics()
          Return a map of metrics maintained by the consumer
 java.util.Map<TopicPartition,java.lang.Long> offsetsBeforeTime(long timestamp, java.util.Collection<TopicPartition> partitions)
          Fetches offsets before a certain timestamp
 java.util.Map<java.lang.String,ConsumerRecords> poll(long timeout)
          Fetches data for the subscribed list of topics and partitions
 java.util.Map<TopicPartition,java.lang.Long> position(java.util.Collection<TopicPartition> partitions)
          Returns the fetch position of the next message for the specified topic partition to be used on the next poll()
 void seek(java.util.Map<TopicPartition,java.lang.Long> offsets)
          Overrides the fetch positions that the consumer will use on the next fetch request.
 void subscribe(java.lang.String... topics)
          Incrementally subscribe to the given list of topics.
 void subscribe(TopicPartition... partitions)
          Incrementally subscribes to a specific topic and partition.
 void unsubscribe(java.lang.String... topics)
          Unsubscribe from the specific topics.
 void unsubscribe(TopicPartition... partitions)
          Unsubscribe from the specific topic partitions.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MockConsumer

public MockConsumer()
Method Detail

subscribe

public void subscribe(java.lang.String... topics)
Description copied from interface: Consumer
Incrementally subscribe to the given list of topics. This API is mutually exclusive to subscribe(partitions)

Specified by:
subscribe in interface Consumer
Parameters:
topics - A variable list of topics that the consumer subscribes to

subscribe

public void subscribe(TopicPartition... partitions)
Description copied from interface: Consumer
Incrementally subscribes to a specific topic and partition. This API is mutually exclusive to subscribe(topics)

Specified by:
subscribe in interface Consumer
Parameters:
partitions - Partitions to subscribe to

unsubscribe

public void unsubscribe(java.lang.String... topics)
Description copied from interface: Consumer
Unsubscribe from the specific topics. Messages for this topic will not be returned from the next poll() onwards. This should be used in conjunction with subscribe(topics). It is an error to unsubscribe from a topic that was never subscribed to using subscribe(topics)

Specified by:
unsubscribe in interface Consumer
Parameters:
topics - Topics to unsubscribe from

unsubscribe

public void unsubscribe(TopicPartition... partitions)
Description copied from interface: Consumer
Unsubscribe from the specific topic partitions. Messages for these partitions will not be returned from the next poll() onwards. This should be used in conjunction with subscribe(topic, partitions). It is an error to unsubscribe from a partition that was never subscribed to using subscribe(partitions)

Specified by:
unsubscribe in interface Consumer
Parameters:
partitions - Partitions to unsubscribe from

poll

public java.util.Map<java.lang.String,ConsumerRecords> poll(long timeout)
Description copied from interface: Consumer
Fetches data for the subscribed list of topics and partitions

Specified by:
poll in interface Consumer
Parameters:
timeout - The time, in milliseconds, spent waiting in poll if data is not available. If 0, waits indefinitely. Must not be negative
Returns:
Map of topic to records for the subscribed topics and partitions as soon as data is available for a topic partition. Availability of data is controlled by ConsumerConfig.FETCH_MIN_BYTES_CONFIG and ConsumerConfig.FETCH_MAX_WAIT_MS_CONFIG. If no data is available for timeout ms, returns an empty list

commit

public OffsetMetadata commit(java.util.Map<TopicPartition,java.lang.Long> offsets,
                             boolean sync)
Description copied from interface: Consumer
Commits the specified offsets for the specified list of topics and partitions to Kafka.

Specified by:
commit in interface Consumer
Parameters:
offsets - The map of offsets to commit for the given topic partitions
sync - If true, commit will block until the consumer receives an acknowledgment
Returns:
An OffsetMetadata object that contains the partition, offset and a corresponding error code. Returns null if the sync flag is set to false.

commit

public OffsetMetadata commit(boolean sync)
Description copied from interface: Consumer
Commits offsets returned on the last poll() for the subscribed list of topics and partitions.

Specified by:
commit in interface Consumer
Parameters:
sync - If true, the commit should block until the consumer receives an acknowledgment
Returns:
An OffsetMetadata object that contains the partition, offset and a corresponding error code. Returns null if the sync flag is set to false

seek

public void seek(java.util.Map<TopicPartition,java.lang.Long> offsets)
Description copied from interface: Consumer
Overrides the fetch positions that the consumer will use on the next fetch request. If the consumer subscribes to a list of topics using subscribe(topics), an exception will be thrown if the specified topic partition is not owned by the consumer.

Specified by:
seek in interface Consumer
Parameters:
offsets - The map of fetch positions per topic and partition

committed

public java.util.Map<TopicPartition,java.lang.Long> committed(java.util.Collection<TopicPartition> partitions)
Description copied from interface: Consumer
Fetches the last committed offsets for the input list of partitions

Specified by:
committed in interface Consumer
Parameters:
partitions - The list of partitions to return the last committed offset for
Returns:
The list of offsets for the specified list of partitions

position

public java.util.Map<TopicPartition,java.lang.Long> position(java.util.Collection<TopicPartition> partitions)
Description copied from interface: Consumer
Returns the fetch position of the next message for the specified topic partition to be used on the next poll()

Specified by:
position in interface Consumer
Parameters:
partitions - Partitions for which the fetch position will be returned
Returns:
The position from which data will be fetched for the specified partition on the next poll()

offsetsBeforeTime

public java.util.Map<TopicPartition,java.lang.Long> offsetsBeforeTime(long timestamp,
                                                                      java.util.Collection<TopicPartition> partitions)
Description copied from interface: Consumer
Fetches offsets before a certain timestamp

Specified by:
offsetsBeforeTime in interface Consumer
Parameters:
timestamp - The unix timestamp. Value -1 indicates earliest available timestamp. Value -2 indicates latest available timestamp.
partitions - The list of partitions for which the offsets are returned
Returns:
The offsets for messages that were written to the server before the specified timestamp.

metrics

public java.util.Map<java.lang.String,? extends Metric> metrics()
Description copied from interface: Consumer
Return a map of metrics maintained by the consumer

Specified by:
metrics in interface Consumer

close

public void close()
Description copied from interface: Consumer
Close this consumer

Specified by:
close in interface java.io.Closeable
Specified by:
close in interface Consumer