projects
/
demos
/
kafka
/
training
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Implementierung vereinfacht: Auf das Nötigste zusammengekürzt
[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
0f40058
..
595ef89
100644
(file)
--- a/
src/test/java/de/juplo/kafka/GenericApplicationTests.java
+++ b/
src/test/java/de/juplo/kafka/GenericApplicationTests.java
@@
-2,6
+2,7
@@
package de.juplo.kafka;
import com.mongodb.client.MongoClient;
import lombok.extern.slf4j.Slf4j;
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;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.kafka.clients.producer.KafkaProducer;
@@
-62,13
+63,11
@@
abstract class GenericApplicationTests<K, V>
@Autowired
ExecutorService executor;
@Autowired
@Autowired
ExecutorService executor;
@Autowired
- StateRepository stateRepository;
- @Autowired
MongoClient mongoClient;
@Autowired
MongoProperties mongoProperties;
@Autowired
MongoClient mongoClient;
@Autowired
MongoProperties mongoProperties;
@Autowired
-
PollIntervalAware
ConsumerRebalanceListener rebalanceListener;
+ ConsumerRebalanceListener rebalanceListener;
@Autowired
RecordHandler<K, V> recordHandler;
@Autowired
RecordHandler<K, V> recordHandler;
@@
-93,7
+92,7
@@
abstract class GenericApplicationTests<K, V>
/** Tests methods */
@Test
/** Tests methods */
@Test
- void commitsCurrentOffsetsOnSuccess()
+ void commitsCurrentOffsetsOnSuccess()
throws Exception
{
int numberOfGeneratedMessages =
recordGenerator.generate(false, false, messageSender);
{
int numberOfGeneratedMessages =
recordGenerator.generate(false, false, messageSender);
@@
-116,6
+115,7
@@
abstract class GenericApplicationTests<K, V>
.isThrownBy(() -> endlessConsumer.exitStatus())
.describedAs("Consumer should still be running");
.isThrownBy(() -> endlessConsumer.exitStatus())
.describedAs("Consumer should still be running");
+ endlessConsumer.stop();
recordGenerator.assertBusinessLogic();
}
recordGenerator.assertBusinessLogic();
}
@@
-248,29
+248,23
@@
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)
{
- 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()
}
List<TopicPartition> partitions()
@@
-393,7
+387,6
@@
abstract class GenericApplicationTests<K, V>
{
try
{
{
try
{
- endlessConsumer.stop();
testRecordProducer.close();
offsetConsumer.close();
}
testRecordProducer.close();
offsetConsumer.close();
}