Class ProducerBuilder<K,V>
- Type Parameters:
K- The record key typeV- The record value type
- All Implemented Interfaces:
Closeable,AutoCloseable
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 Summary
ConstructorsConstructorDescriptionProducerBuilder(Map<String, Object> props, Duration kafkaApiTimeout, String keySerializerClassName, String valueSerializerClassName) Creates a newProducerBuilder.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 newProducerBuilder.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 newProducerBuilder. -
Method Summary
Modifier and TypeMethodDescriptionclientId()voidclose()Close the underlyingKafkaProducerandExecutorService.CreateProducerTaskfor creating records reading from a comma-separated classpath resource.fromMulti(io.smallrye.mutiny.Multi<org.apache.kafka.clients.producer.ProducerRecord<K, V>> recordMulti) CreateProducerTaskfor creating records from the givenMulti.fromRecords(List<org.apache.kafka.clients.producer.ProducerRecord<K, V>> records) CreateProducerTaskfor creating records from the givenListof records.final ProducerTaskfromRecords(org.apache.kafka.clients.producer.ProducerRecord<K, V>... records) CreateProducerTaskfor creating records from the given array of records.booleanunwrap()usingGenerator(Function<Integer, org.apache.kafka.clients.producer.ProducerRecord<K, V>> generatorFunction) CreateProducerTaskfor creating records from the given array of records.usingGenerator(Function<Integer, org.apache.kafka.clients.producer.ProducerRecord<K, V>> generatorFunction, long numberOfRecords) CreateProducerTaskfor creating records from the given array of records.usingGenerator(Function<Integer, org.apache.kafka.clients.producer.ProducerRecord<K, V>> generatorFunction, Duration during) CreateProducerTaskfor creating records from the given array of records.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) CreateProducerTaskfor creating records generated using the given function.withClientId(String clientId) Add configuration property forclient.idfor this producer to be used when the producer is created.Set the concurrency level for producer writes to 1024.withConcurrency(int concurrency) Set the concurrency level for producer writes.withOnTermination(BiConsumer<org.apache.kafka.clients.producer.KafkaProducer<K, V>, Throwable> onTermination) Add configuration property fortransactional.idfor this producer to be used when the producer is created.Add property to the configuration to be used when the producer is created.Add properties to the configuration to be used when the producer is created.withTransactionalId(String transactionalId) Add configuration property fortransactional.idfor this producer to be used when the producer is created.
-
Constructor Details
-
ProducerBuilder
public ProducerBuilder(Map<String, Object> props, Duration kafkaApiTimeout, String keySerializerClassName, String valueSerializerClassName) Creates a newProducerBuilder.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 creationkafkaApiTimeout- the timeout for api calls to KafkakeySerializerClassName- the serializer class name for record keysvalueSerializerClassName- 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 newProducerBuilder.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 creationkafkaApiTimeout- the timeout for api calls to KafkakeySerializer- the serializer for record keysvalueSerializer- 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 newProducerBuilder.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 creationkafkaApiTimeout- the timeout for api calls to KafkakeySerde- the Serde for record keysvalueSerde- the Serde for record values
-
-
Method Details
-
unwrap
- Returns:
- the underlying
KafkaProducer, may benullif producer is not yet created.
-
close
public void close()Close the underlyingKafkaProducerandExecutorService.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable
-
withProp
Add property to the configuration to be used when the producer is created.- Parameters:
key- the key for propertyvalue- the value for property- Returns:
- this
ProducerBuilder
-
withProps
Add properties to the configuration to be used when the producer is created.- Parameters:
properties- the properties- Returns:
- this
ProducerBuilder
-
withClientId
Add configuration property forclient.idfor this producer to be used when the producer is created.- Parameters:
clientId- the client id- Returns:
- this
ProducerBuilder
-
withTransactionalId
Add configuration property fortransactional.idfor 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 fortransactional.idfor this producer to be used when the producer is created.- Parameters:
onTermination- the transaction consumer group- Returns:
- this
ProducerBuilder
-
withConcurrency
Set the concurrency level for producer writes.- Parameters:
concurrency- the concurrency for producer writes- Returns:
- this
ProducerBuilder
-
withConcurrency
Set the concurrency level for producer writes to 1024.- Returns:
- this
ProducerBuilder
-
clientId
- Returns:
- the client id
-
isTransactional
public boolean isTransactional()- Returns:
trueif the producer configuration containstransactional.id, else returnsfalse.
-
fromCsv
CreateProducerTaskfor creating records reading from a comma-separated classpath resource.This
ProducerBuilderneeds to be created providingSerdes 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) CreateProducerTaskfor creating records from the givenMulti.The resulting
ProducerTaskwill be already started.- Parameters:
recordMulti- the multi providingProducerRecords to produce.- Returns:
- the
ProducerTask
-
fromRecords
public ProducerTask fromRecords(List<org.apache.kafka.clients.producer.ProducerRecord<K, V>> records) CreateProducerTaskfor creating records from the givenListof records.The resulting
ProducerTaskwill be already started.- Parameters:
records- the list ofProducerRecords to produce.- Returns:
- the
ProducerTask
-
fromRecords
@SafeVarargs public final ProducerTask fromRecords(org.apache.kafka.clients.producer.ProducerRecord<K, V>... records) CreateProducerTaskfor creating records from the given array of records.The resulting
ProducerTaskwill be already started.- Parameters:
records- the array ofProducerRecordto 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) CreateProducerTaskfor creating records generated using the given function.The plug function is used to modify the
Multigenerating the records, ex. limiting the number of records produced.The resulting
ProducerTaskwill be already started. The task will run until theMultiis terminated (with completion or failure) or is explicitly stopped (subscription to theMulticancelled).- Parameters:
generatorFunction- the function to generateProducerRecordsplugFunction- 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) CreateProducerTaskfor creating records from the given array of records.The resulting
ProducerTaskwill be already started. The task will run until theMultiis terminated (with completion or failure) or is explicitly stopped (subscription to theMulticancelled).- Parameters:
generatorFunction- the function to generateProducerRecords- Returns:
- the
ProducerTask
-
usingGenerator
public ProducerTask usingGenerator(Function<Integer, org.apache.kafka.clients.producer.ProducerRecord<K, V>> generatorFunction, long numberOfRecords) CreateProducerTaskfor creating records from the given array of records.The resulting
ProducerTaskwill be already started. The task will run for generating the given number of records.- Parameters:
generatorFunction- the function to generateProducerRecordsnumberOfRecords- 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) CreateProducerTaskfor creating records from the given array of records.The resulting
ProducerTaskwill be already started. The task will run during the given duration.- Parameters:
generatorFunction- the function to generateProducerRecordsduring- the duration of the producing task to run- Returns:
- the
ProducerTask
-