public class MockConsumer<K,V> extends java.lang.Object implements Consumer<K,V>
Consumer
interface you can use for testing code that uses Kafka. This class is not
threadsafe . However, you can use the schedulePollTask(Runnable)
method to write multithreaded tests
where a driver thread waits for poll(long)
to be called by a background thread and then can safely perform
operations during a callback.Constructor and Description |
---|
MockConsumer(OffsetResetStrategy offsetResetStrategy) |
Modifier and Type | Method and Description |
---|---|
void |
addRecord(ConsumerRecord<K,V> record) |
void |
assign(java.util.Collection<TopicPartition> partitions) |
java.util.Set<TopicPartition> |
assignment() |
java.util.Map<TopicPartition,java.lang.Long> |
beginningOffsets(java.util.Collection<TopicPartition> partitions) |
void |
close() |
void |
close(long timeout,
java.util.concurrent.TimeUnit unit) |
boolean |
closed() |
void |
commitAsync() |
void |
commitAsync(java.util.Map<TopicPartition,OffsetAndMetadata> offsets,
OffsetCommitCallback callback) |
void |
commitAsync(OffsetCommitCallback callback) |
void |
commitSync() |
void |
commitSync(java.util.Map<TopicPartition,OffsetAndMetadata> offsets) |
OffsetAndMetadata |
committed(TopicPartition partition) |
java.util.Map<TopicPartition,java.lang.Long> |
endOffsets(java.util.Collection<TopicPartition> partitions) |
java.util.Map<java.lang.String,java.util.List<PartitionInfo>> |
listTopics() |
java.util.Map<MetricName,? extends Metric> |
metrics() |
java.util.Map<TopicPartition,OffsetAndTimestamp> |
offsetsForTimes(java.util.Map<TopicPartition,java.lang.Long> timestampsToSearch) |
java.util.List<PartitionInfo> |
partitionsFor(java.lang.String topic) |
void |
pause(java.util.Collection<TopicPartition> partitions) |
java.util.Set<TopicPartition> |
paused() |
ConsumerRecords<K,V> |
poll(long timeout) |
long |
position(TopicPartition partition) |
void |
rebalance(java.util.Collection<TopicPartition> newAssignment)
Simulate a rebalance event.
|
void |
resume(java.util.Collection<TopicPartition> partitions) |
void |
scheduleNopPollTask() |
void |
schedulePollTask(java.lang.Runnable task)
Schedule a task to be executed during a poll().
|
void |
seek(TopicPartition partition,
long offset) |
void |
seekToBeginning(java.util.Collection<TopicPartition> partitions) |
void |
seekToEnd(java.util.Collection<TopicPartition> partitions) |
void |
setException(KafkaException exception) |
void |
subscribe(java.util.Collection<java.lang.String> topics) |
void |
subscribe(java.util.Collection<java.lang.String> topics,
ConsumerRebalanceListener listener) |
void |
subscribe(java.util.regex.Pattern pattern,
ConsumerRebalanceListener listener) |
java.util.Set<java.lang.String> |
subscription() |
void |
unsubscribe() |
void |
updateBeginningOffsets(java.util.Map<TopicPartition,java.lang.Long> newOffsets) |
void |
updateEndOffsets(java.util.Map<TopicPartition,java.lang.Long> newOffsets) |
void |
updatePartitions(java.lang.String topic,
java.util.List<PartitionInfo> partitions) |
void |
wakeup() |
public MockConsumer(OffsetResetStrategy offsetResetStrategy)
public java.util.Set<TopicPartition> assignment()
assignment
in interface Consumer<K,V>
KafkaConsumer.assignment()
public void rebalance(java.util.Collection<TopicPartition> newAssignment)
public java.util.Set<java.lang.String> subscription()
subscription
in interface Consumer<K,V>
KafkaConsumer.subscription()
public void subscribe(java.util.Collection<java.lang.String> topics)
subscribe
in interface Consumer<K,V>
KafkaConsumer.subscribe(Collection)
public void subscribe(java.util.regex.Pattern pattern, ConsumerRebalanceListener listener)
subscribe
in interface Consumer<K,V>
KafkaConsumer.subscribe(Pattern, ConsumerRebalanceListener)
public void subscribe(java.util.Collection<java.lang.String> topics, ConsumerRebalanceListener listener)
subscribe
in interface Consumer<K,V>
KafkaConsumer.subscribe(Collection, ConsumerRebalanceListener)
public void assign(java.util.Collection<TopicPartition> partitions)
assign
in interface Consumer<K,V>
KafkaConsumer.assign(Collection)
public void unsubscribe()
unsubscribe
in interface Consumer<K,V>
KafkaConsumer.unsubscribe()
public ConsumerRecords<K,V> poll(long timeout)
poll
in interface Consumer<K,V>
KafkaConsumer.poll(long)
public void addRecord(ConsumerRecord<K,V> record)
public void setException(KafkaException exception)
public void commitAsync(java.util.Map<TopicPartition,OffsetAndMetadata> offsets, OffsetCommitCallback callback)
commitAsync
in interface Consumer<K,V>
KafkaConsumer.commitAsync(Map, OffsetCommitCallback)
public void commitSync(java.util.Map<TopicPartition,OffsetAndMetadata> offsets)
commitSync
in interface Consumer<K,V>
KafkaConsumer.commitSync(Map)
public void commitAsync()
commitAsync
in interface Consumer<K,V>
KafkaConsumer.commitAsync()
public void commitAsync(OffsetCommitCallback callback)
commitAsync
in interface Consumer<K,V>
KafkaConsumer.commitAsync(OffsetCommitCallback)
public void commitSync()
commitSync
in interface Consumer<K,V>
KafkaConsumer.commitSync()
public void seek(TopicPartition partition, long offset)
seek
in interface Consumer<K,V>
KafkaConsumer.seek(TopicPartition, long)
public OffsetAndMetadata committed(TopicPartition partition)
committed
in interface Consumer<K,V>
KafkaConsumer.committed(TopicPartition)
public long position(TopicPartition partition)
position
in interface Consumer<K,V>
KafkaConsumer.position(TopicPartition)
public void seekToBeginning(java.util.Collection<TopicPartition> partitions)
seekToBeginning
in interface Consumer<K,V>
KafkaConsumer.seekToBeginning(Collection)
public void updateBeginningOffsets(java.util.Map<TopicPartition,java.lang.Long> newOffsets)
public void seekToEnd(java.util.Collection<TopicPartition> partitions)
seekToEnd
in interface Consumer<K,V>
KafkaConsumer.seekToEnd(Collection)
public void updateEndOffsets(java.util.Map<TopicPartition,java.lang.Long> newOffsets)
public java.util.Map<MetricName,? extends Metric> metrics()
metrics
in interface Consumer<K,V>
KafkaConsumer.metrics()
public java.util.List<PartitionInfo> partitionsFor(java.lang.String topic)
partitionsFor
in interface Consumer<K,V>
KafkaConsumer.partitionsFor(String)
public java.util.Map<java.lang.String,java.util.List<PartitionInfo>> listTopics()
listTopics
in interface Consumer<K,V>
KafkaConsumer.listTopics()
public void updatePartitions(java.lang.String topic, java.util.List<PartitionInfo> partitions)
public void pause(java.util.Collection<TopicPartition> partitions)
pause
in interface Consumer<K,V>
KafkaConsumer.pause(Collection)
public void resume(java.util.Collection<TopicPartition> partitions)
resume
in interface Consumer<K,V>
KafkaConsumer.resume(Collection)
public java.util.Map<TopicPartition,OffsetAndTimestamp> offsetsForTimes(java.util.Map<TopicPartition,java.lang.Long> timestampsToSearch)
offsetsForTimes
in interface Consumer<K,V>
KafkaConsumer.offsetsForTimes(java.util.Map)
public java.util.Map<TopicPartition,java.lang.Long> beginningOffsets(java.util.Collection<TopicPartition> partitions)
beginningOffsets
in interface Consumer<K,V>
KafkaConsumer.beginningOffsets(java.util.Collection)
public java.util.Map<TopicPartition,java.lang.Long> endOffsets(java.util.Collection<TopicPartition> partitions)
endOffsets
in interface Consumer<K,V>
KafkaConsumer.endOffsets(java.util.Collection)
public void close()
close
in interface java.io.Closeable
close
in interface java.lang.AutoCloseable
close
in interface Consumer<K,V>
KafkaConsumer.close()
public void close(long timeout, java.util.concurrent.TimeUnit unit)
close
in interface Consumer<K,V>
KafkaConsumer.close(long, TimeUnit)
public boolean closed()
public void wakeup()
wakeup
in interface Consumer<K,V>
KafkaConsumer.wakeup()
public void schedulePollTask(java.lang.Runnable task)
poll(long)
invocation. You can use this repeatedly to mock out multiple responses to poll invocations.task
- the task to be executedpublic void scheduleNopPollTask()
public java.util.Set<TopicPartition> paused()
paused
in interface Consumer<K,V>
KafkaConsumer.paused()