io.quarkus.kafka.client.runtime.devui.util.ConsumerFactory Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of quarkus-kafka-client Show documentation
Show all versions of quarkus-kafka-client Show documentation
Connect to Apache Kafka with its native API
package io.quarkus.kafka.client.runtime.devui.util;
import java.util.*;
import org.apache.kafka.clients.consumer.Consumer;
import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.kafka.common.TopicPartition;
import org.apache.kafka.common.serialization.BytesDeserializer;
import org.apache.kafka.common.utils.Bytes;
public class ConsumerFactory {
public static Consumer createConsumer(String topicName, Integer requestedPartition,
Map commonConfig) {
return createConsumer(List.of(new TopicPartition(topicName, requestedPartition)), commonConfig);
}
// We must create a new instance per request, as we might have multiple windows open, each with different pagination, filter and thus different cursor.
public static Consumer createConsumer(Collection requestedPartitions,
Map commonConfig) {
Map config = new HashMap<>(commonConfig);
//TODO: make generic?
config.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, BytesDeserializer.class);
config.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, BytesDeserializer.class);
config.put(ConsumerConfig.CLIENT_ID_CONFIG, "kafka-ui-" + UUID.randomUUID());
// For pagination, we require manual management of offset pointer.
config.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, "false");
var consumer = new KafkaConsumer(config);
consumer.assign(requestedPartitions);
return consumer;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy