public final class ProducerConfigBuilder extends AbstractConfigBuilder<ProducerConfigBuilder>
props| Modifier and Type | Method and Description |
|---|---|
static ProducerConfigBuilder |
create() |
ProducerConfigBuilder |
withAcks(Acks acks)
The number of acknowledgments the producer requires the leader to have received before considering a request complete.
|
ProducerConfigBuilder |
withBatchSize(int batchSize)
The producer will attempt to batch records together into fewer requests whenever multiple records are being sent
to the same partition.
|
ProducerConfigBuilder |
withBufferMemory(long bufferMemory)
The total bytes of memory the producer can use to buffer records waiting to be sent to the server.
|
ProducerConfigBuilder |
withCompressionType(org.apache.kafka.common.record.CompressionType compressionType)
The compression type for all data generated by the producer.
|
ProducerConfigBuilder |
withDeliveryTimeout(Duration deliveryTimeout)
An upper bound on the time to report success or failure
after a call to
send() returns. |
ProducerConfigBuilder |
withEnableIdempotence(boolean enableIdempotence)
When set to 'true', the producer will ensure that exactly one copy of each message is written in the stream.
|
ProducerConfigBuilder |
withInterceptorClasses(Class<? extends org.apache.kafka.clients.producer.ProducerInterceptor>... interceptorClasses)
A list of classes to use as interceptors.
|
ProducerConfigBuilder |
withKeySerializer(Class<? extends org.apache.kafka.common.serialization.Serializer> keySerializer)
Serializer class for key that implements the
org.apache.kafka.common.serialization.Serializer interface. |
ProducerConfigBuilder |
withLinger(Duration linger)
The producer groups together any records that arrive in between request transmissions into a single batched request.
|
ProducerConfigBuilder |
withMaxBlock(Duration maxBlock)
The configuration controls how long the
KafkaProducer's send(), partitionsFor(), "
initTransactions(), sendOffsetsToTransaction(), commitTransaction() "
and abortTransaction() methods will block. |
ProducerConfigBuilder |
withMaxInFlightRequestsPerConnection(int maxInFlightRequestsPerConnection)
The maximum number of unacknowledged requests the client will send on a single connection before blocking.
|
ProducerConfigBuilder |
withMaxRequestSize(int maxRequestSize)
The maximum size of a request in bytes.
|
ProducerConfigBuilder |
withPartitioner(Class<? extends org.apache.kafka.clients.producer.Partitioner> partitioner)
Partitioner class that implements the
org.apache.kafka.clients.producer.Partitioner interface. |
ProducerConfigBuilder |
withRequestTimeout(Duration requestTimeout)
This should be larger than
replica.lag.time.max.ms (a broker configuration)
to reduce the possibility of message duplication due to unnecessary producer retries. |
ProducerConfigBuilder |
withRetries(int retries)
Setting a value greater than zero will cause the client to resend any record whose send fails with a potentially transient error.
|
ProducerConfigBuilder |
withTransactionalId(String transactionalId)
The TransactionalId to use for transactional delivery.
|
ProducerConfigBuilder |
withTransactionTimeout(Duration transactionTimeout)
The maximum amount of time in ms that the transaction coordinator will wait for a transaction status update from the producer before proactively aborting the ongoing transaction.
|
ProducerConfigBuilder |
withValueSerializer(Class<? extends org.apache.kafka.common.serialization.Serializer> valueSerializer)
Serializer class for value that implements the
org.apache.kafka.common.serialization.Serializer interface. |
buildMap, buildProperties, withBootstrapServers, withBootstrapServers, withClientDNSLookup, withClientId, withClientRack, withConnectionsMaxIdle, withCustom, withCustomMap, withMetadataMaxAge, withMetricReporterClasses, withMetricsNumSamples, withMetricsRecordingLevel, withMetricsSampleWindow, withReceiveBufferBytes, withReconnectBackoff, withReconnectBackoffMax, withRetryBackoff, withSaslMechanism, withSecurityProtocol, withSecurityProviders, withSendBufferBytes, withSocketConnectionSetupTimeout, withSocketConnectionSetupTimeoutMaxpublic static ProducerConfigBuilder create()
public ProducerConfigBuilder withBatchSize(int batchSize)
No attempt will be made to batch records larger than this size.
Requests sent to brokers will contain multiple batches, one for each partition with data available to be sent.
A small batch size will make batching less common and may reduce throughput (a batch size of zero will disable batching entirely). A very large batch size may use memory a bit more wastefully as we will always allocate a buffer of the specified batch size in anticipation of additional records.
public ProducerConfigBuilder withAcks(Acks acks)
acks=0 If set to zero then the producer will not wait for any acknowledgment from the
server at all. The record will be immediately added to the socket buffer and considered sent. No guarantee can be
made that the server has received the record in this case, and the retries configuration will not
take effect (as the client won't generally know of any failures). The offset given back for each record will
always be set to -1.acks=1 This will mean the leader will write the record to its local log but will respond
without awaiting full acknowledgement from all followers. In this case should the leader fail immediately after
acknowledging the record but before the followers have replicated it then the record will be lost.acks=all This means the leader will wait for the full set of in-sync replicas to
acknowledge the record. This guarantees that the record will not be lost as long as at least one in-sync replica
remains alive. This is the strongest available guarantee. This is equivalent to the acks=-1 setting.public ProducerConfigBuilder withLinger(Duration linger)
BATCH_SIZE_CONFIG worth of records for a partition it will be sent immediately regardless of this
setting, however if we have fewer than this many bytes accumulated for this partition we will 'linger' for the
specified time waiting for more records to show up. This setting defaults to 0 (i.e. no delay). Setting LINGER_MS_CONFIG=5,
for example, would have the effect of reducing the number of requests sent but would add up to 5ms of latency to records sent in the absence of load.public ProducerConfigBuilder withRequestTimeout(Duration requestTimeout)
replica.lag.time.max.ms (a broker configuration)
to reduce the possibility of message duplication due to unnecessary producer retries.withRequestTimeout in class AbstractConfigBuilder<ProducerConfigBuilder>public ProducerConfigBuilder withDeliveryTimeout(Duration deliveryTimeout)
send() returns. This limits the total time that a record will be delayed
prior to sending, the time to await acknowledgement from the broker (if expected), and the time allowed
for retriable send failures. The producer may report failure to send a record earlier than this config if
either an unrecoverable error is encountered, the retries have been exhausted,
or the record is added to a batch which reached an earlier delivery expiration deadline.
The value of this config should be greater than or equal to the sum of REQUEST_TIMEOUT_MS_CONFIG
and LINGER_MS_CONFIG .public ProducerConfigBuilder withMaxRequestSize(int maxRequestSize)
public ProducerConfigBuilder withMaxBlock(Duration maxBlock)
KafkaProducer's send(), partitionsFor(), "
initTransactions(), sendOffsetsToTransaction(), commitTransaction() "
and abortTransaction() methods will block. "
For send() this timeout bounds the total time waiting for both metadata fetch and buffer allocation "
(blocking in the user-supplied serializers or partitioner is not counted against this timeout). "
For partitionsFor() this timeout bounds the time spent waiting for metadata if it is unavailable. "
The transaction-related methods always block, but may timeout if "
the transaction coordinator could not be discovered or did not respond within the timeout.public ProducerConfigBuilder withBufferMemory(long bufferMemory)
MAX_BLOCK_MS_CONFIG after which it will throw an exception.This setting should correspond roughly to the total memory the producer will use, but is not a hard bound since not all memory the producer uses is used for buffering. Some additional memory will be used for compression (if compression is enabled) as well as for maintaining in-flight requests.
public ProducerConfigBuilder withCompressionType(org.apache.kafka.common.record.CompressionType compressionType)
none, gzip, snappy, lz4, or zstd.
Compression is of full batches of data, so the efficacy of batching will also impact the compression ratio (more batching means better compression).public ProducerConfigBuilder withMaxInFlightRequestsPerConnection(int maxInFlightRequestsPerConnection)
public ProducerConfigBuilder withRetries(int retries)
MAX_IN_FLIGHT_REQUESTS_PER_CONNECTION to 1 will potentially change the
ordering of records because if two batches are sent to a single partition, and the first fails and is retried but the second
succeeds, then the records in the second batch may appear first. Note additionally that produce requests will be
failed before the number of retries has been exhausted if the timeout configured by
DELIVERY_TIMEOUT_MS_CONFIG expires first before successful acknowledgement. Users should generally
prefer to leave this config unset and instead use DELIVERY_TIMEOUT_MS_CONFIG to control retry behavior.public ProducerConfigBuilder withPartitioner(Class<? extends org.apache.kafka.clients.producer.Partitioner> partitioner)
org.apache.kafka.clients.producer.Partitioner interface.public ProducerConfigBuilder withEnableIdempotence(boolean enableIdempotence)
MAX_IN_FLIGHT_REQUESTS_PER_CONNECTION to be less than or equal to 5,
RETRIES_CONFIG to be greater than 0 and ACKS_CONFIG must be 'all'. If these values
are not explicitly set by the user, suitable values will be chosen. If incompatible values are set,
a ConfigException will be thrown.public ProducerConfigBuilder withTransactionTimeout(Duration transactionTimeout)
InvalidTransactionTimeout error.public ProducerConfigBuilder withTransactionalId(String transactionalId)
enable.idempotence must be enabled if a TransactionalId is configured.
The default is null, which means transactions cannot be used.
Note that, by default, transactions require a cluster of at least three brokers which is the recommended setting for production;
for development you can change this, by adjusting broker setting transaction.state.log.replication.factor.public ProducerConfigBuilder withKeySerializer(Class<? extends org.apache.kafka.common.serialization.Serializer> keySerializer)
org.apache.kafka.common.serialization.Serializer interface.public ProducerConfigBuilder withValueSerializer(Class<? extends org.apache.kafka.common.serialization.Serializer> valueSerializer)
org.apache.kafka.common.serialization.Serializer interface.@SafeVarargs public final ProducerConfigBuilder withInterceptorClasses(Class<? extends org.apache.kafka.clients.producer.ProducerInterceptor>... interceptorClasses)
org.apache.kafka.clients.consumer.ConsumerInterceptor interface allows you to intercept (and possibly mutate) records
received by the consumer. By default, there are no interceptors.Copyright © 2021. All rights reserved.