projects
/
demos
/
kafka
/
training
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
ROT: Korrigierten/Verbesserten Test und Überarbeitetes Setup gemerged
[demos/kafka/training]
/
src
/
test
/
java
/
de
/
juplo
/
kafka
/
GenericApplicationTests.java
diff --git
a/src/test/java/de/juplo/kafka/GenericApplicationTests.java
b/src/test/java/de/juplo/kafka/GenericApplicationTests.java
index
93daf6b
..
b019373
100644
(file)
--- a/
src/test/java/de/juplo/kafka/GenericApplicationTests.java
+++ b/
src/test/java/de/juplo/kafka/GenericApplicationTests.java
@@
-62,6
+62,8
@@
abstract class GenericApplicationTests<K, V>
@Autowired
ExecutorService executor;
@Autowired
@Autowired
ExecutorService executor;
@Autowired
+ StateRepository stateRepository;
+ @Autowired
MongoClient mongoClient;
@Autowired
MongoProperties mongoProperties;
MongoClient mongoClient;
@Autowired
MongoProperties mongoProperties;
@@
-228,23
+230,29
@@
abstract class GenericApplicationTests<K, V>
void seekToEnd()
{
offsetConsumer.assign(partitions());
void seekToEnd()
{
offsetConsumer.assign(partitions());
- offsetConsumer.seekToEnd(partitions());
partitions().forEach(tp ->
{
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);
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)
{
offsetConsumer.unsubscribe();
}
void doForCurrentOffsets(BiConsumer<TopicPartition, Long> consumer)
{
- offsetConsumer.assign(partitions());
- partitions().forEach(tp -> consumer.accept(tp, offsetConsumer.position(tp)));
- offsetConsumer.unsubscribe();
+ 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));
+ });
}
List<TopicPartition> partitions()
}
List<TopicPartition> partitions()
@@
-324,6
+332,7
@@
abstract class GenericApplicationTests<K, V>
props.put("value.deserializer", BytesDeserializer.class.getName());
offsetConsumer = new KafkaConsumer<>(props);
props.put("value.deserializer", BytesDeserializer.class.getName());
offsetConsumer = new KafkaConsumer<>(props);
+ mongoClient.getDatabase(mongoProperties.getDatabase()).drop();
seekToEnd();
oldOffsets = new HashMap<>();
seekToEnd();
oldOffsets = new HashMap<>();
@@
-349,8
+358,6
@@
abstract class GenericApplicationTests<K, V>
}
};
}
};
- mongoClient.getDatabase(mongoProperties.getDatabase()).drop();
-
endlessConsumer =
new EndlessConsumer<>(
executor,
endlessConsumer =
new EndlessConsumer<>(
executor,