Search in sources :

Example 1 with AsyncHandlingAutoCommitKafkaConsumer

use of org.eclipse.hono.client.kafka.consumer.AsyncHandlingAutoCommitKafkaConsumer in project hono by eclipse.

the class KafkaBasedCommandConsumerFactoryImpl method start.

@Override
public Future<Void> start() {
    final Context context = Vertx.currentContext();
    if (context == null) {
        return Future.failedFuture(new IllegalStateException("factory must be started in a Vert.x context"));
    }
    final KafkaCommandProcessingQueue commandQueue = new KafkaCommandProcessingQueue(context);
    commandHandler = new KafkaBasedMappingAndDelegatingCommandHandler(vertx, tenantClient, commandQueue, commandTargetMapper, internalCommandSender, kafkaBasedCommandResponseSender, metrics, tracer);
    final Map<String, String> consumerConfig = kafkaConsumerConfig.getConsumerConfig("command");
    consumerConfig.put(ConsumerConfig.GROUP_ID_CONFIG, groupId);
    consumerConfig.putIfAbsent(ConsumerConfig.PARTITION_ASSIGNMENT_STRATEGY_CONFIG, CooperativeStickyAssignor.class.getName());
    consumerConfig.putIfAbsent(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
    kafkaConsumer = new AsyncHandlingAutoCommitKafkaConsumer(vertx, COMMANDS_TOPIC_PATTERN, commandHandler::mapAndDelegateIncomingCommandMessage, consumerConfig);
    kafkaConsumer.setPollTimeout(Duration.ofMillis(kafkaConsumerConfig.getPollTimeout()));
    kafkaConsumer.setConsumerCreationRetriesTimeout(KafkaClientFactory.UNLIMITED_RETRIES_DURATION);
    kafkaConsumer.setMetricsSupport(kafkaClientMetricsSupport);
    kafkaConsumer.setOnRebalanceDoneHandler(partitions -> commandQueue.setCurrentlyHandledPartitions(Helper.to(partitions)));
    kafkaConsumer.setOnPartitionsLostHandler(partitions -> commandQueue.setRevokedPartitions(Helper.to(partitions)));
    final Future<Void> cleanupServiceStartFuture = Optional.ofNullable(internalKafkaTopicCleanupService).map(InternalKafkaTopicCleanupService::start).orElseGet(Future::succeededFuture);
    return CompositeFuture.all(commandHandler.start(), kafkaConsumer.start(), cleanupServiceStartFuture).mapEmpty();
}
Also used : Context(io.vertx.core.Context) SpanContext(io.opentracing.SpanContext) CooperativeStickyAssignor(org.apache.kafka.clients.consumer.CooperativeStickyAssignor) CompositeFuture(io.vertx.core.CompositeFuture) Future(io.vertx.core.Future) AsyncHandlingAutoCommitKafkaConsumer(org.eclipse.hono.client.kafka.consumer.AsyncHandlingAutoCommitKafkaConsumer)

Aggregations

SpanContext (io.opentracing.SpanContext)1 CompositeFuture (io.vertx.core.CompositeFuture)1 Context (io.vertx.core.Context)1 Future (io.vertx.core.Future)1 CooperativeStickyAssignor (org.apache.kafka.clients.consumer.CooperativeStickyAssignor)1 AsyncHandlingAutoCommitKafkaConsumer (org.eclipse.hono.client.kafka.consumer.AsyncHandlingAutoCommitKafkaConsumer)1