use of org.sdase.commons.server.kafka.consumer.strategies.MessageListenerStrategy in project sda-dropwizard-commons by SDA-SE.
the class KafkaBundleWithConfigIT method kafkaConsumerReceivesMessagesAsyncCommit.
@Test
public void kafkaConsumerReceivesMessagesAsyncCommit() {
String topic = "kafkaConsumerReceivesMessagesAsyncCommit";
StringDeserializer deserializer = new StringDeserializer();
KAFKA.getKafkaTestUtils().createTopic(topic, 1, (short) 1);
// register adhoc implementations
assertThat(kafkaBundle).isNotNull();
kafkaBundle.createMessageListener(MessageListenerRegistration.builder().withListenerConfig(LISTENER_CONFIG_ASYNC).forTopic(topic).withConsumerConfig(ConsumerConfig.builder().withGroup(UUID.randomUUID().toString()).withClientId(CLIENT_ID_ASYNC).build()).withKeyDeserializer(deserializer).withValueDeserializer(deserializer).withListenerStrategy(new MessageListenerStrategy<String, String>() {
@Override
public void processRecords(ConsumerRecords<String, String> records, KafkaConsumer<String, String> consumer) {
records.forEach(r -> resultsString.add(r.value()));
consumer.commitSync();
callbackCount++;
}
@Override
public void commitOnClose(KafkaConsumer<String, String> consumer) {
}
@Override
public void verifyConsumerConfig(Map<String, String> config) {
}
}).build());
List<String> checkMessages = new ArrayList<>();
// pass in messages
for (int i = 0; i < KafkaBundleConsts.N_MESSAGES; i++) {
String message = UUID.randomUUID().toString();
checkMessages.add(message);
stringStringProducer.send(new ProducerRecord<>(topic, message));
}
await().atMost(KafkaBundleConsts.N_MAX_WAIT_MS, MILLISECONDS).untilAsserted(() -> {
assertThat(resultsString).containsExactlyInAnyOrderElementsOf(checkMessages);
assertThat(callbackCount).isPositive();
});
}
Aggregations