All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.imrafaelmerino.kafkacli.KafkaProducers Maven / Gradle / Ivy

package com.github.imrafaelmerino.kafkacli;


import jio.cli.ConsoleLogger;
import jsonvalues.JsObj;
import org.apache.kafka.clients.producer.KafkaProducer;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.function.Function;

final class KafkaProducers implements Function> {


    private final Map> producers;

    KafkaProducers() {
        producers = new HashMap<>();
        Runtime.getRuntime()
               .addShutdownHook(new Thread(() -> {
                   for (KafkaProducer consumer : producers.values()) {
                       try {
                           consumer.close();
                       } catch (Exception e) {
                           ConsoleLogger.log("Exception closing producer during shutdown hook: %s".formatted(e));
                       }
                   }
               }));
    }

    boolean isStarted(String producerName) {
        return producers.containsKey(producerName);
    }

    void startProducer(JsObj kafkaCommonConf,
                       String producerName,
                       JsObj producerConf
                      ) {

        Properties kafkaCommonProps = Fun.toProperties(kafkaCommonConf);

        Properties producerProps = Fun.toProperties(producerConf);
        producerProps.putAll(kafkaCommonProps);

        this.producers.put(producerName,
                           new KafkaProducer<>(producerProps));

    }

    void closeProducer(String producerName) {
        KafkaProducer producer = this.producers.get(producerName);
        if (producer != null) {
            producer
                    .close();
            this.producers.remove(producerName);
        }
    }


    @Override
    public KafkaProducer apply(final String name) {

        return producers.get(name);

    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy