public interface ProducerBuilder<T> extends Cloneable
ProducerBuilder
is used to configure and create instances of Producer
.Modifier and Type | Method and Description |
---|---|
ProducerBuilder<T> |
addEncryptionKey(String key)
Add public encryption key, used by producer to encrypt the data key.
|
ProducerBuilder<T> |
autoUpdatePartitions(boolean autoUpdate)
If enabled, partitioned producer will automatically discover new partitions at runtime.
|
ProducerBuilder<T> |
batcherBuilder(BatcherBuilder batcherBuilder)
Set the batcher builder
BatcherBuilder of the producer. |
ProducerBuilder<T> |
batchingMaxBytes(int batchingMaxBytes)
Set the maximum number of bytes permitted in a batch.
|
ProducerBuilder<T> |
batchingMaxMessages(int batchMessagesMaxMessagesPerBatch)
Set the maximum number of messages permitted in a batch.
|
ProducerBuilder<T> |
batchingMaxPublishDelay(long batchDelay,
TimeUnit timeUnit)
Set the time period within which the messages sent will be batched default: 1 ms if batch messages are
enabled.
|
ProducerBuilder<T> |
blockIfQueueFull(boolean blockIfQueueFull)
Set whether the
Producer.send(T) and Producer.sendAsync(T) operations should block when the outgoing
message queue is full. |
ProducerBuilder<T> |
clone()
Create a copy of the current
ProducerBuilder . |
ProducerBuilder<T> |
compressionType(CompressionType compressionType)
Set the compression type for the producer.
|
Producer<T> |
create()
Finalize the creation of the
Producer instance. |
CompletableFuture<Producer<T>> |
createAsync()
Finalize the creation of the
Producer instance in asynchronous mode. |
ProducerBuilder<T> |
cryptoFailureAction(ProducerCryptoFailureAction action)
Sets the ProducerCryptoFailureAction to the value specified.
|
ProducerBuilder<T> |
cryptoKeyReader(CryptoKeyReader cryptoKeyReader)
Sets a
CryptoKeyReader . |
ProducerBuilder<T> |
enableBatching(boolean enableBatching)
Control whether automatic batching of messages is enabled for the producer.
|
ProducerBuilder<T> |
enableChunking(boolean enableChunking)
If message size is higher than allowed max publish-payload size by broker then enableChunking helps producer to
split message into multiple chunks and publish them to broker separately and in order.
|
ProducerBuilder<T> |
enableMultiSchema(boolean multiSchema)
Control whether enable the multiple schema mode for producer.
|
ProducerBuilder<T> |
hashingScheme(HashingScheme hashingScheme)
Change the
HashingScheme used to chose the partition on where to publish a particular message. |
ProducerBuilder<T> |
initialSequenceId(long initialSequenceId)
Set the baseline for the sequence ids for messages published by the producer.
|
ProducerBuilder<T> |
intercept(ProducerInterceptor... interceptors)
Add a set of
ProducerInterceptor to the producer. |
ProducerBuilder<T> |
intercept(ProducerInterceptor<T>... interceptors)
Deprecated.
|
ProducerBuilder<T> |
loadConf(Map<String,Object> config)
Load the configuration from provided config map.
|
ProducerBuilder<T> |
maxPendingMessages(int maxPendingMessages)
Set the max size of the queue holding the messages pending to receive an acknowledgment from the broker.
|
ProducerBuilder<T> |
maxPendingMessagesAcrossPartitions(int maxPendingMessagesAcrossPartitions)
Set the number of max pending messages across all the partitions.
|
ProducerBuilder<T> |
messageRouter(MessageRouter messageRouter)
Set a custom message routing policy by passing an implementation of MessageRouter.
|
ProducerBuilder<T> |
messageRoutingMode(MessageRoutingMode messageRoutingMode)
Set the
MessageRoutingMode for a partitioned producer. |
ProducerBuilder<T> |
producerName(String producerName)
Specify a name for the producer.
|
ProducerBuilder<T> |
properties(Map<String,String> properties)
Add all the properties in the provided map to the producer.
|
ProducerBuilder<T> |
property(String key,
String value)
Set a name/value property with this producer.
|
ProducerBuilder<T> |
roundRobinRouterBatchingPartitionSwitchFrequency(int frequency)
Set the partition switch frequency while batching of messages is enabled and
using round-robin routing mode for non-keyed message default: 10.
|
ProducerBuilder<T> |
sendTimeout(int sendTimeout,
TimeUnit unit)
Set the send timeout (default: 30 seconds).
|
ProducerBuilder<T> |
topic(String topicName)
Specify the topic this producer will be publishing on.
|
Producer<T> create() throws PulsarClientException
Producer
instance.
This method will block until the producer is created successfully.
PulsarClientException.ProducerBusyException
- if a producer with the same "producer name" is already connected to the topicPulsarClientException
- if the producer creation failsCompletableFuture<Producer<T>> createAsync()
Producer
instance in asynchronous mode.
This method will return a CompletableFuture
that can be used to access the instance when it's ready.
PulsarClientException.ProducerBusyException
- if a producer with the same "producer name" is already connected to the topicPulsarClientException
- if the producer creation failsProducerBuilder<T> loadConf(Map<String,Object> config)
Example:
Map<String, Object> config = new HashMap<>();
config.put("producerName", "test-producer");
config.put("sendTimeoutMs", 2000);
ProducerBuilder<byte[]> builder = client.newProducer()
.loadConf(config);
Producer<byte[]> producer = builder.create();
config
- configuration map to loadProducerBuilder<T> clone()
ProducerBuilder
.
Cloning the builder can be used to share an incomplete configuration and specialize it multiple times. For example:
ProducerBuilder<String> builder = client.newProducer(Schema.STRING)
.sendTimeout(10, TimeUnit.SECONDS)
.blockIfQueueFull(true);
Producer<String> producer1 = builder.clone().topic("topic-1").create();
Producer<String> producer2 = builder.clone().topic("topic-2").create();
ProducerBuilder<T> topic(String topicName)
This argument is required when constructing the produce.
topicName
- the name of the topicProducerBuilder<T> producerName(String producerName)
If not assigned, the system will generate a globally unique name which can be accessed with
Producer.getProducerName()
.
Warning: When specifying a name, it is up to the user to ensure that, for a given topic, the producer name is unique across all Pulsar's clusters. Brokers will enforce that only a single producer a given name can be publishing on a topic.
producerName
- the custom name to use for the producerProducerBuilder<T> sendTimeout(int sendTimeout, TimeUnit unit)
If a message is not acknowledged by the server before the sendTimeout expires, an error will be reported.
Setting the timeout to zero, for example setTimeout(0, TimeUnit.SECONDS)
will set the timeout
to infinity, which can be useful when using Pulsar's message deduplication feature, since the client
library will retry forever to publish a message. No errors will be propagated back to the application.
sendTimeout
- the send timeoutunit
- the time unit of the sendTimeout
ProducerBuilder<T> maxPendingMessages(int maxPendingMessages)
When the queue is full, by default, all calls to Producer.send(T)
and Producer.sendAsync(T)
will fail unless blockIfQueueFull=true
. Use blockIfQueueFull(boolean)
to change the blocking behavior.
The producer queue size also determines the max amount of memory that will be required by the client application. Until, the producer gets a successful acknowledgment back from the broker, it will keep in memory (direct memory pool) all the messages in the pending queue.
Default is 1000.
maxPendingMessages
- the max size of the pending messages queue for the producerProducerBuilder<T> maxPendingMessagesAcrossPartitions(int maxPendingMessagesAcrossPartitions)
This setting will be used to lower the max pending messages for each partition
(maxPendingMessages(int)
), if the total exceeds the configured value.
The purpose of this setting is to have an upper-limit on the number
of pending messages when publishing on a partitioned topic.
Default is 50000.
If publishing at high rate over a topic with many partitions (especially when publishing messages without a partitioning key), it might be beneficial to increase this parameter to allow for more pipelining within the individual partitions producers.
maxPendingMessagesAcrossPartitions
- max pending messages across all the partitionsProducerBuilder<T> blockIfQueueFull(boolean blockIfQueueFull)
Producer.send(T)
and Producer.sendAsync(T)
operations should block when the outgoing
message queue is full.
Default is false
. If set to false
, send operations will immediately fail with
PulsarClientException.ProducerQueueIsFullError
when there is no space left in pending queue. If set to
true
, the Producer.sendAsync(T)
operation will instead block.
Setting blockIfQueueFull=true
simplifies the task of an application that
just wants to publish messages as fast as possible, without having to worry
about overflowing the producer send queue.
For example:
Producer<String> producer = client.newProducer()
.topic("my-topic")
.blockIfQueueFull(true)
.create();
while (true) {
producer.sendAsync("my-message")
.thenAccept(messageId -> {
System.out.println("Published message: " + messageId);
})
.exceptionally(ex -> {
System.err.println("Failed to publish: " + e);
return null;
});
}
blockIfQueueFull
- whether to block Producer.send(T)
and Producer.sendAsync(T)
operations on queue fullProducerBuilder<T> messageRoutingMode(MessageRoutingMode messageRoutingMode)
MessageRoutingMode
for a partitioned producer.
Default routing mode is to round-robin across the available partitions.
This logic is applied when the application is not setting a key on a
particular message. If the key is set with MessageBuilder#setKey(String)
,
then the hash of the key will be used to select a partition for the message.
messageRoutingMode
- the message routing modeMessageRoutingMode
ProducerBuilder<T> hashingScheme(HashingScheme hashingScheme)
HashingScheme
used to chose the partition on where to publish a particular message.
Standard hashing functions available are:
HashingScheme.JavaStringHash
: Java String.hashCode()
(Default)
HashingScheme.Murmur3_32Hash
: Use Murmur3 hashing function.
https://en.wikipedia.org/wiki/MurmurHash
hashingScheme
- the chosen HashingScheme
ProducerBuilder<T> compressionType(CompressionType compressionType)
By default, message payloads are not compressed. Supported compression types are:
CompressionType.NONE
: No compression (Default)CompressionType.LZ4
: Compress with LZ4 algorithm. Faster but lower compression than ZLibCompressionType.ZLIB
: Standard ZLib compressionCompressionType.ZSTD
Compress with Zstandard codec. Since Pulsar 2.3. Zstd cannot be used if consumer
applications are not in version >= 2.3 as wellCompressionType.SNAPPY
Compress with Snappy codec. Since Pulsar 2.4. Snappy cannot be used if
consumer applications are not in version >= 2.4 as wellcompressionType
- the selected compression typeProducerBuilder<T> messageRouter(MessageRouter messageRouter)
messageRouter
- ProducerBuilder<T> enableBatching(boolean enableBatching)
When batching is enabled, multiple calls to Producer.sendAsync(T)
can result in a single batch
to be sent to the broker, leading to better throughput, especially when publishing small messages.
If compression is enabled, messages will be compressed at the batch level, leading to a much better
compression ratio for similar headers or contents.
When enabled default batch delay is set to 1 ms and default batch size is 1000 messages
Batching is enabled by default since 2.0.0.
batchingMaxPublishDelay(long, TimeUnit)
,
batchingMaxMessages(int)
ProducerBuilder<T> enableChunking(boolean enableChunking)
This feature allows publisher to publish large size of message by splitting it to multiple chunks and let consumer stitch them together to form a original large published message. Therefore, it's necessary to configure recommended configuration at pulsar producer and consumer. Recommendation to use this feature:
1. This feature is right now only supported by non-shared subscription and persistent-topic.
2. Disable batching to use chunking feature
3. Pulsar-client keeps published messages into buffer until it receives ack from broker.
So, it's better to reduce "maxPendingMessages" size to avoid producer occupying large amount
of memory by buffered messages.
4. Set message-ttl on the namespace to cleanup incomplete chunked messages.
(sometime due to broker-restart or publish time, producer might fail to publish entire large message
so, consumer will not be able to consume and ack those messages. So, those messages can
be only discared by msg ttl) Or configure
ConsumerBuilder#expireTimeOfIncompleteChunkedMessage()
5. Consumer configuration: consumer should also configure receiverQueueSize and maxPendingChuckedMessage
enableChunking
- ProducerBuilder<T> cryptoKeyReader(CryptoKeyReader cryptoKeyReader)
CryptoKeyReader
.
Configure the key reader to be used to encrypt the message payloads.
cryptoKeyReader
- CryptoKeyReader objectProducerBuilder<T> addEncryptionKey(String key)
At the time of producer creation, Pulsar client checks if there are keys added to encryptionKeys. If keys are
found, a callback CryptoKeyReader.getPrivateKey(String, Map)
and
CryptoKeyReader.getPublicKey(String, Map)
is invoked against each key to load the values of the key.
Application should implement this callback to return the key in pkcs8 format. If compression is enabled, message
is encrypted after compression. If batch messaging is enabled, the batched message is encrypted.
key
- the name of the encryption key in the key storeProducerBuilder<T> cryptoFailureAction(ProducerCryptoFailureAction action)
action
- the action the producer will take in case of encryption failuresProducerBuilder<T> batchingMaxPublishDelay(long batchDelay, TimeUnit timeUnit)
batchingMaxMessages(int)
)
All messages will be published as a single batch message. The consumer will be delivered individual messages in the batch in the same order they were enqueued.
batchDelay
- the batch delaytimeUnit
- the time unit of the batchDelay
batchingMaxMessages(int)
,
batchingMaxBytes(int)
ProducerBuilder<T> roundRobinRouterBatchingPartitionSwitchFrequency(int frequency)
The time period of partition switch is frequency * batchingMaxPublishDelay. During this period, all messages arrives will be route to the same partition.
frequency
- the frequency of partition switchmessageRoutingMode(MessageRoutingMode)
,
batchingMaxPublishDelay(long, TimeUnit)
ProducerBuilder<T> batchingMaxMessages(int batchMessagesMaxMessagesPerBatch)
All messages in batch will be published as a single batch message. The consumer will be delivered individual messages in the batch in the same order they were enqueued.
batchMessagesMaxMessagesPerBatch
- maximum number of messages in a batchbatchingMaxPublishDelay(long, TimeUnit)
,
batchingMaxBytes(int)
ProducerBuilder<T> batchingMaxBytes(int batchingMaxBytes)
All messages in a batch will be published as a single batched message. The consumer will be delivered individual messages in the batch in the same order they were enqueued.
batchingMaxBytes
- maximum number of bytes in a batchbatchingMaxPublishDelay(long, TimeUnit)
,
batchingMaxMessages(int)
ProducerBuilder<T> batcherBuilder(BatcherBuilder batcherBuilder)
BatcherBuilder
of the producer. Producer will use the batcher builder to
build a batch message container.This is only be used when batching is enabled.batcherBuilder
- batcher builderProducerBuilder<T> initialSequenceId(long initialSequenceId)
First message will be using (initialSequenceId + 1)
as its sequence id and
subsequent messages will be assigned incremental sequence ids, if not otherwise specified.
initialSequenceId
- the initial sequence id for the producerProducerBuilder<T> property(String key, String value)
Properties are application defined metadata that can be attached to the producer. When getting the topic stats, this metadata will be associated to the producer stats for easier identification.
key
- the property keyvalue
- the property valueProducerBuilder<T> properties(Map<String,String> properties)
Properties are application defined metadata that can be attached to the producer. When getting the topic stats, this metadata will be associated to the producer stats for easier identification.
properties
- the map of properties@Deprecated ProducerBuilder<T> intercept(ProducerInterceptor<T>... interceptors)
ProducerInterceptor
to the producer.
Interceptors can be used to trace the publish and acknowledgments operation happening in a producer.
interceptors
- the list of interceptors to intercept the producer created by this builder.ProducerBuilder<T> intercept(ProducerInterceptor... interceptors)
ProducerInterceptor
to the producer.
Interceptors can be used to trace the publish and acknowledgments operation happening in a producer.
interceptors
- the list of interceptors to intercept the producer created by this builder.ProducerBuilder<T> autoUpdatePartitions(boolean autoUpdate)
Default is true.
autoUpdate
- whether to auto discover the partition configuration changesProducerBuilder<T> enableMultiSchema(boolean multiSchema)
Enabled by default.
multiSchema
- indicates to enable or disable multiple schema modeCopyright © 2017–2020 Apache Software Foundation. All rights reserved.