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(Duration)
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 |
addEndOffsets(java.util.Map<TopicPartition,java.lang.Long> newOffsets) |
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) |
java.util.Map<TopicPartition,java.lang.Long> |
beginningOffsets(java.util.Collection<TopicPartition> partitions,
java.time.Duration timeout) |
void |
close() |
void |
close(java.time.Duration timeout) |
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.time.Duration timeout) |
void |
commitSync(java.util.Map<TopicPartition,OffsetAndMetadata> offsets) |
void |
commitSync(java.util.Map<TopicPartition,OffsetAndMetadata> offsets,
java.time.Duration timeout) |
OffsetAndMetadata |
committed(TopicPartition partition) |
OffsetAndMetadata |
committed(TopicPartition partition,
java.time.Duration timeout) |
java.util.Map<TopicPartition,java.lang.Long> |
endOffsets(java.util.Collection<TopicPartition> partitions) |
java.util.Map<TopicPartition,java.lang.Long> |
endOffsets(java.util.Collection<TopicPartition> partitions,
java.time.Duration timeout) |
java.util.Map<java.lang.String,java.util.List<PartitionInfo>> |
listTopics() |
java.util.Map<java.lang.String,java.util.List<PartitionInfo>> |
listTopics(java.time.Duration timeout) |
java.util.Map<MetricName,? extends Metric> |
metrics() |
java.util.Map<TopicPartition,OffsetAndTimestamp> |
offsetsForTimes(java.util.Map<TopicPartition,java.lang.Long> timestampsToSearch) |
java.util.Map<TopicPartition,OffsetAndTimestamp> |
offsetsForTimes(java.util.Map<TopicPartition,java.lang.Long> timestampsToSearch,
java.time.Duration timeout) |
java.util.List<PartitionInfo> |
partitionsFor(java.lang.String topic) |
java.util.List<PartitionInfo> |
partitionsFor(java.lang.String topic,
java.time.Duration timeout) |
void |
pause(java.util.Collection<TopicPartition> partitions) |
java.util.Set<TopicPartition> |
paused() |
ConsumerRecords<K,V> |
poll(java.time.Duration timeout) |
ConsumerRecords<K,V> |
poll(long timeout)
Deprecated.
|
long |
position(TopicPartition partition) |
long |
position(TopicPartition partition,
java.time.Duration timeout) |
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 |
seek(TopicPartition partition,
OffsetAndMetadata offsetAndMetadata) |
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) |
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.regex.Pattern pattern)
subscribe
in interface Consumer<K,V>
KafkaConsumer.subscribe(Pattern)
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()
@Deprecated public ConsumerRecords<K,V> poll(long timeout)
poll
in interface Consumer<K,V>
KafkaConsumer.poll(long)
public ConsumerRecords<K,V> poll(java.time.Duration timeout)
poll
in interface Consumer<K,V>
KafkaConsumer.poll(Duration)
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 commitSync(java.time.Duration timeout)
commitSync
in interface Consumer<K,V>
KafkaConsumer.commitSync(Duration)
public void commitSync(java.util.Map<TopicPartition,OffsetAndMetadata> offsets, java.time.Duration timeout)
commitSync
in interface Consumer<K,V>
KafkaConsumer.commitSync(Map, Duration)
public void seek(TopicPartition partition, long offset)
seek
in interface Consumer<K,V>
KafkaConsumer.seek(TopicPartition, long)
public void seek(TopicPartition partition, OffsetAndMetadata offsetAndMetadata)
seek
in interface Consumer<K,V>
KafkaConsumer.seek(TopicPartition, OffsetAndMetadata)
public OffsetAndMetadata committed(TopicPartition partition)
committed
in interface Consumer<K,V>
KafkaConsumer.committed(TopicPartition)
public OffsetAndMetadata committed(TopicPartition partition, java.time.Duration timeout)
committed
in interface Consumer<K,V>
KafkaConsumer.committed(TopicPartition, Duration)
public long position(TopicPartition partition)
position
in interface Consumer<K,V>
KafkaConsumer.position(TopicPartition)
public long position(TopicPartition partition, java.time.Duration timeout)
position
in interface Consumer<K,V>
KafkaConsumer.position(TopicPartition, Duration)
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 addEndOffsets(java.util.Map<TopicPartition,java.lang.Long> newOffsets)
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(Map)
public java.util.Map<TopicPartition,java.lang.Long> beginningOffsets(java.util.Collection<TopicPartition> partitions)
beginningOffsets
in interface Consumer<K,V>
KafkaConsumer.beginningOffsets(Collection)
public java.util.Map<TopicPartition,java.lang.Long> endOffsets(java.util.Collection<TopicPartition> partitions)
endOffsets
in interface Consumer<K,V>
KafkaConsumer.endOffsets(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(Duration)
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()
public java.util.List<PartitionInfo> partitionsFor(java.lang.String topic, java.time.Duration timeout)
partitionsFor
in interface Consumer<K,V>
KafkaConsumer.partitionsFor(String, Duration)
public java.util.Map<java.lang.String,java.util.List<PartitionInfo>> listTopics(java.time.Duration timeout)
listTopics
in interface Consumer<K,V>
KafkaConsumer.listTopics(Duration)
public java.util.Map<TopicPartition,OffsetAndTimestamp> offsetsForTimes(java.util.Map<TopicPartition,java.lang.Long> timestampsToSearch, java.time.Duration timeout)
offsetsForTimes
in interface Consumer<K,V>
KafkaConsumer.offsetsForTimes(Map, Duration)
public java.util.Map<TopicPartition,java.lang.Long> beginningOffsets(java.util.Collection<TopicPartition> partitions, java.time.Duration timeout)
beginningOffsets
in interface Consumer<K,V>
KafkaConsumer.beginningOffsets(Collection, Duration)
public java.util.Map<TopicPartition,java.lang.Long> endOffsets(java.util.Collection<TopicPartition> partitions, java.time.Duration timeout)
endOffsets
in interface Consumer<K,V>
KafkaConsumer.endOffsets(Collection, Duration)
public void close(java.time.Duration timeout)
close
in interface Consumer<K,V>
KafkaConsumer.close(Duration)