|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.apache.kafka.clients.consumer.MockConsumer
public class MockConsumer
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 |
---|
public MockConsumer()
Method Detail |
---|
public void subscribe(java.lang.String... topics)
Consumer
subscribe(partitions)
subscribe
in interface Consumer
topics
- A variable list of topics that the consumer subscribes topublic void subscribe(TopicPartition... partitions)
Consumer
subscribe(topics)
subscribe
in interface Consumer
partitions
- Partitions to subscribe topublic void unsubscribe(java.lang.String... topics)
Consumer
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)
unsubscribe
in interface Consumer
topics
- Topics to unsubscribe frompublic void unsubscribe(TopicPartition... partitions)
Consumer
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)
unsubscribe
in interface Consumer
partitions
- Partitions to unsubscribe frompublic java.util.Map<java.lang.String,ConsumerRecords> poll(long timeout)
Consumer
poll
in interface Consumer
timeout
- The time, in milliseconds, spent waiting in poll if data is not available. If 0, waits indefinitely. Must not be negative
ConsumerConfig.FETCH_MIN_BYTES_CONFIG
and ConsumerConfig.FETCH_MAX_WAIT_MS_CONFIG
.
If no data is available for timeout ms, returns an empty listpublic OffsetMetadata commit(java.util.Map<TopicPartition,java.lang.Long> offsets, boolean sync)
Consumer
commit
in interface Consumer
offsets
- The map of offsets to commit for the given topic partitionssync
- If true, commit will block until the consumer receives an acknowledgment
OffsetMetadata
object that contains the partition, offset and a corresponding error code. Returns null
if the sync flag is set to false.public OffsetMetadata commit(boolean sync)
Consumer
poll()
for the subscribed list of topics and partitions.
commit
in interface Consumer
sync
- If true, the commit should block until the consumer receives an acknowledgment
OffsetMetadata
object that contains the partition, offset and a corresponding error code. Returns null
if the sync flag is set to falsepublic void seek(java.util.Map<TopicPartition,java.lang.Long> offsets)
Consumer
subscribe(topics)
, an exception will be thrown if the specified topic partition is not owned by
the consumer.
seek
in interface Consumer
offsets
- The map of fetch positions per topic and partitionpublic java.util.Map<TopicPartition,java.lang.Long> committed(java.util.Collection<TopicPartition> partitions)
Consumer
committed
in interface Consumer
partitions
- The list of partitions to return the last committed offset for
public java.util.Map<TopicPartition,java.lang.Long> position(java.util.Collection<TopicPartition> partitions)
Consumer
poll()
position
in interface Consumer
partitions
- Partitions for which the fetch position will be returned
poll()
public java.util.Map<TopicPartition,java.lang.Long> offsetsBeforeTime(long timestamp, java.util.Collection<TopicPartition> partitions)
Consumer
offsetsBeforeTime
in interface Consumer
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
public java.util.Map<java.lang.String,? extends Metric> metrics()
Consumer
metrics
in interface Consumer
public void close()
Consumer
close
in interface java.io.Closeable
close
in interface Consumer
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |