Class ProducerBuilder<K,​V>

  • Type Parameters:
    K - The record key type
    V - The record value type
    All Implemented Interfaces:
    Closeable, AutoCloseable

    public class ProducerBuilder<K,​V>
    extends Object
    implements Closeable
    Kafka producer wrapper for creating tasks (ProducerTask) that produce records.

    The wrapped Kafka producer is created lazily when a ProducerTask is created and started. Until the producer is created builder methods prefixed with with- act to provide additional configuration.

    The created producer is closed automatically when the task is terminated. The producer uses an underlying ExecutorService to send records.

    • Constructor Detail

      • ProducerBuilder

        public ProducerBuilder​(Map<String,​Object> props,
                               Duration kafkaApiTimeout,
                               String keySerializerClassName,
                               String valueSerializerClassName)
        Creates a new ProducerBuilder.

        On producer creation, key and value serializers will be created and Serializer.configure(java.util.Map<java.lang.String, ?>, boolean) methods will be called.

        Parameters:
        props - the initial properties for producer creation
        kafkaApiTimeout - the timeout for api calls to Kafka
        keySerializerClassName - the serializer class name for record keys
        valueSerializerClassName - the serializer class name for record values
      • ProducerBuilder

        public ProducerBuilder​(Map<String,​Object> props,
                               Duration kafkaApiTimeout,
                               org.apache.kafka.common.serialization.Serializer<K> keySerializer,
                               org.apache.kafka.common.serialization.Serializer<V> valueSerializer)
        Creates a new ProducerBuilder.

        Note that on producer creation Serializer.configure(java.util.Map<java.lang.String, ?>, boolean) methods will NOT be called.

        Parameters:
        props - the initial properties for producer creation
        kafkaApiTimeout - the timeout for api calls to Kafka
        keySerializer - the serializer for record keys
        valueSerializer - the serializer for record values
      • ProducerBuilder

        public ProducerBuilder​(Map<String,​Object> props,
                               Duration kafkaApiTimeout,
                               org.apache.kafka.common.serialization.Serde<K> keySerde,
                               org.apache.kafka.common.serialization.Serde<V> valueSerde)
        Creates a new ProducerBuilder.

        Note that on producer creation Serializer.configure(java.util.Map<java.lang.String, ?>, boolean) methods will NOT be called.

        Parameters:
        props - the initial properties for producer creation
        kafkaApiTimeout - the timeout for api calls to Kafka
        keySerde - the Serde for record keys
        valueSerde - the Serde for record values
    • Method Detail

      • unwrap

        public org.apache.kafka.clients.producer.KafkaProducer<K,​V> unwrap()
        Returns:
        the underlying KafkaProducer, may be null if producer is not yet created.
      • withProp

        public ProducerBuilder<K,​V> withProp​(String key,
                                                   String value)
        Add property to the configuration to be used when the producer is created.
        Parameters:
        key - the key for property
        value - the value for property
        Returns:
        this ProducerBuilder
      • withProps

        public ProducerBuilder<K,​V> withProps​(Map<String,​String> properties)
        Add properties to the configuration to be used when the producer is created.
        Parameters:
        properties - the properties
        Returns:
        this ProducerBuilder
      • withClientId

        public ProducerBuilder<K,​V> withClientId​(String clientId)
        Add configuration property for client.id for this producer to be used when the producer is created.
        Parameters:
        clientId - the client id
        Returns:
        this ProducerBuilder
      • withTransactionalId

        public ProducerBuilder<K,​V> withTransactionalId​(String transactionalId)
        Add configuration property for transactional.id for this producer to be used when the producer is created.
        Parameters:
        transactionalId - the transactional id
        Returns:
        this ProducerBuilder
      • withOnTermination

        public ProducerBuilder<K,​V> withOnTermination​(BiConsumer<org.apache.kafka.clients.producer.KafkaProducer<K,​V>,​Throwable> onTermination)
        Add configuration property for transactional.id for this producer to be used when the producer is created.
        Parameters:
        onTermination - the transaction consumer group
        Returns:
        this ProducerBuilder
      • withConcurrency

        public ProducerBuilder<K,​V> withConcurrency​(int concurrency)
        Set the concurrency level for producer writes.
        Parameters:
        concurrency - the concurrency for producer writes
        Returns:
        this ProducerBuilder
      • clientId

        public String clientId()
        Returns:
        the client id
      • isTransactional

        public boolean isTransactional()
        Returns:
        true if the producer configuration contains transactional.id, else returns false.
      • fromCsv

        public ProducerTask fromCsv​(String resourcePath)
        Create ProducerTask for creating records reading from a comma-separated classpath resource.

        This ProducerBuilder needs to be created providing Serdes to deserialize keys and values from the comma-separated file.

        Parameters:
        resourcePath - the path for the resource in the classpath
        Returns:
        the ProducerTask
      • fromMulti

        public ProducerTask fromMulti​(io.smallrye.mutiny.Multi<org.apache.kafka.clients.producer.ProducerRecord<K,​V>> recordMulti)
        Create ProducerTask for creating records from the given Multi.

        The resulting ProducerTask will be already started.

        Parameters:
        recordMulti - the multi providing ProducerRecords to produce.
        Returns:
        the ProducerTask
      • fromRecords

        public ProducerTask fromRecords​(List<org.apache.kafka.clients.producer.ProducerRecord<K,​V>> records)
        Create ProducerTask for creating records from the given List of records.

        The resulting ProducerTask will be already started.

        Parameters:
        records - the list of ProducerRecords to produce.
        Returns:
        the ProducerTask
      • fromRecords

        @SafeVarargs
        public final ProducerTask fromRecords​(org.apache.kafka.clients.producer.ProducerRecord<K,​V>... records)
        Create ProducerTask for creating records from the given array of records.

        The resulting ProducerTask will be already started.

        Parameters:
        records - the array of ProducerRecord to produce.
        Returns:
        the ProducerTask
      • usingGenerator

        public ProducerTask usingGenerator​(Function<Integer,​org.apache.kafka.clients.producer.ProducerRecord<K,​V>> generatorFunction,
                                           Function<io.smallrye.mutiny.Multi<org.apache.kafka.clients.producer.ProducerRecord<K,​V>>,​io.smallrye.mutiny.Multi<org.apache.kafka.clients.producer.ProducerRecord<K,​V>>> plugFunction)
        Create ProducerTask for creating records generated using the given function.

        The plug function is used to modify the Multi generating the records, ex. limiting the number of records produced.

        The resulting ProducerTask will be already started. The task will run until the Multi is terminated (with completion or failure) or is explicitly stopped (subscription to the Multi cancelled).

        Parameters:
        generatorFunction - the function to generate ProducerRecords
        plugFunction - the function to apply to the resulting multi
        Returns:
        the ProducerTask
      • usingGenerator

        public ProducerTask usingGenerator​(Function<Integer,​org.apache.kafka.clients.producer.ProducerRecord<K,​V>> generatorFunction)
        Create ProducerTask for creating records from the given array of records.

        The resulting ProducerTask will be already started. The task will run until the Multi is terminated (with completion or failure) or is explicitly stopped (subscription to the Multi cancelled).

        Parameters:
        generatorFunction - the function to generate ProducerRecords
        Returns:
        the ProducerTask
      • usingGenerator

        public ProducerTask usingGenerator​(Function<Integer,​org.apache.kafka.clients.producer.ProducerRecord<K,​V>> generatorFunction,
                                           long numberOfRecords)
        Create ProducerTask for creating records from the given array of records.

        The resulting ProducerTask will be already started. The task will run for generating the given number of records.

        Parameters:
        generatorFunction - the function to generate ProducerRecords
        numberOfRecords - the number of records to produce
        Returns:
        the ProducerTask
      • usingGenerator

        public ProducerTask usingGenerator​(Function<Integer,​org.apache.kafka.clients.producer.ProducerRecord<K,​V>> generatorFunction,
                                           Duration during)
        Create ProducerTask for creating records from the given array of records.

        The resulting ProducerTask will be already started. The task will run during the given duration.

        Parameters:
        generatorFunction - the function to generate ProducerRecords
        during - the duration of the producing task to run
        Returns:
        the ProducerTask