use of org.eclipse.hono.client.kafka.KafkaClientFactory in project hono by eclipse.
the class HonoKafkaConsumer method start.
@Override
public Future<Void> start() {
context = vertx.getOrCreateContext();
final Promise<Void> startPromise = Promise.promise();
runOnContext(v -> {
// create KafkaConsumer here so that it is created in the Vert.x context of the start() method (KafkaConsumer uses vertx.getOrCreateContext())
Optional.ofNullable(kafkaConsumerSupplier).map(supplier -> Future.succeededFuture(KafkaConsumer.create(vertx, supplier.get()))).orElseGet(() -> {
final KafkaClientFactory kafkaClientFactory = new KafkaClientFactory(vertx);
return kafkaClientFactory.createKafkaConsumerWithRetries(consumerConfig, String.class, Buffer.class, consumerCreationRetriesTimeout);
}).onFailure(thr -> {
log.error("error creating consumer [client-id: {}]", getClientId(), thr);
startPromise.fail(thr);
}).onSuccess(consumer -> {
kafkaConsumer = consumer;
Optional.ofNullable(metricsSupport).ifPresent(ms -> ms.registerKafkaConsumer(kafkaConsumer.unwrap()));
kafkaConsumer.handler(record -> {
if (!startPromise.future().isComplete()) {
log.debug("postponing record handling until start() is completed [topic: {}, partition: {}, offset: {}]", record.topic(), record.partition(), record.offset());
}
startPromise.future().onSuccess(v2 -> {
if (respectTtl && KafkaRecordHelper.isTtlElapsed(record.headers())) {
onRecordHandlerSkippedForExpiredRecord(record);
} else {
try {
recordHandler.handle(record);
} catch (final Exception e) {
log.warn("error handling record [topic: {}, partition: {}, offset: {}, headers: {}]", record.topic(), record.partition(), record.offset(), record.headers(), e);
}
}
});
});
kafkaConsumer.batchHandler(this::onBatchOfRecordsReceived);
kafkaConsumer.exceptionHandler(error -> log.error("consumer error occurred [client-id: {}]", getClientId(), error));
installRebalanceListeners();
// subscribe and wait for rebalance to make sure that when start() completes,
// the consumer is actually ready to receive records already
// let polls finish quickly until start() is completed
kafkaConsumer.asStream().pollTimeout(Duration.ofMillis(10));
subscribeAndWaitForRebalance().onSuccess(v2 -> {
kafkaConsumer.asStream().pollTimeout(pollTimeout);
logSubscribedTopicsOnStartComplete();
}).onComplete(startPromise);
});
});
return startPromise.future();
}
Aggregations