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.List<TopicPartition> partitions) |
java.util.Set<TopicPartition> |
assignment() |
void |
close() |
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<java.lang.String,java.util.List<PartitionInfo>> |
listTopics() |
java.util.Map<MetricName,? extends Metric> |
metrics() |
java.util.List<PartitionInfo> |
partitionsFor(java.lang.String topic) |
void |
pause(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(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(TopicPartition... partitions) |
void |
seekToEnd(TopicPartition... partitions) |
void |
setException(KafkaException exception) |
void |
subscribe(java.util.List<java.lang.String> topics) |
void |
subscribe(java.util.List<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.List<java.lang.String> topics)
subscribe
in interface Consumer<K,V>
KafkaConsumer.subscribe(List)
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.List<java.lang.String> topics, ConsumerRebalanceListener listener)
subscribe
in interface Consumer<K,V>
KafkaConsumer.subscribe(List, ConsumerRebalanceListener)
public void assign(java.util.List<TopicPartition> partitions)
assign
in interface Consumer<K,V>
KafkaConsumer.assign(List)
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(TopicPartition... partitions)
seekToBeginning
in interface Consumer<K,V>
KafkaConsumer.seekToBeginning(TopicPartition...)
public void updateBeginningOffsets(java.util.Map<TopicPartition,java.lang.Long> newOffsets)
public void seekToEnd(TopicPartition... partitions)
seekToEnd
in interface Consumer<K,V>
KafkaConsumer.seekToEnd(TopicPartition...)
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(TopicPartition... partitions)
pause
in interface Consumer<K,V>
KafkaConsumer.pause(TopicPartition...)
public void resume(TopicPartition... partitions)
resume
in interface Consumer<K,V>
KafkaConsumer.resume(TopicPartition...)
public void close()
close
in interface java.io.Closeable
close
in interface java.lang.AutoCloseable
close
in interface Consumer<K,V>
KafkaConsumer.close()
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()