import com.mongodb.client.MongoClient;
import lombok.extern.slf4j.Slf4j;
+import org.apache.kafka.clients.consumer.ConsumerRebalanceListener;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.kafka.clients.producer.KafkaProducer;
@Autowired
ExecutorService executor;
@Autowired
- StateRepository stateRepository;
- @Autowired
MongoClient mongoClient;
@Autowired
MongoProperties mongoProperties;
@Autowired
- PollIntervalAwareConsumerRebalanceListener rebalanceListener;
+ ConsumerRebalanceListener rebalanceListener;
@Autowired
RecordHandler<K, V> recordHandler;
/** Tests methods */
@Test
- void commitsCurrentOffsetsOnSuccess()
+ void commitsCurrentOffsetsOnSuccess() throws Exception
{
int numberOfGeneratedMessages =
recordGenerator.generate(false, false, messageSender);
.isThrownBy(() -> endlessConsumer.exitStatus())
.describedAs("Consumer should still be running");
+ endlessConsumer.stop();
recordGenerator.assertBusinessLogic();
}
Long expected = offsetsToCheck.get(tp) + 1;
log.debug("Checking, if the offset {} for {} is at most {}", offset, tp, expected);
assertThat(offset)
- .describedAs("Committed offset corresponds to the offset of the consumer")
+ .describedAs("Committed offset must be at most equal to the offset of the consumer")
.isLessThanOrEqualTo(expected);
isOffsetBehindSeen.add(offset < expected);
});
void seekToEnd()
{
offsetConsumer.assign(partitions());
+ offsetConsumer.seekToEnd(partitions());
partitions().forEach(tp ->
{
+ // seekToEnd() works lazily: it only takes effect on poll()/position()
Long offset = offsetConsumer.position(tp);
log.info("New position for {}: {}", tp, offset);
- Integer partition = tp.partition();
- StateDocument document =
- stateRepository
- .findById(partition.toString())
- .orElse(new StateDocument(partition));
- document.offset = offset;
- stateRepository.save(document);
});
+ // The new positions must be commited!
+ offsetConsumer.commitSync();
offsetConsumer.unsubscribe();
}
void doForCurrentOffsets(BiConsumer<TopicPartition, Long> consumer)
{
- partitions().forEach(tp ->
- {
- String partition = Integer.toString(tp.partition());
- Optional<Long> offset = stateRepository.findById(partition).map(document -> document.offset);
- consumer.accept(tp, offset.orElse(0l));
- });
+ offsetConsumer.assign(partitions());
+ partitions().forEach(tp -> consumer.accept(tp, offsetConsumer.position(tp)));
+ offsetConsumer.unsubscribe();
}
List<TopicPartition> partitions()
{
try
{
- endlessConsumer.stop();
testRecordProducer.close();
offsetConsumer.close();
}