org.apache.pulsar.client.api.ReaderBuilder Maven / Gradle / Ivy
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.pulsar.client.api;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import org.apache.pulsar.common.classification.InterfaceAudience;
import org.apache.pulsar.common.classification.InterfaceStability;
/**
* {@link ReaderBuilder} is used to configure and create instances of {@link Reader}.
*
* @see PulsarClient#newReader()
*
* @since 2.0.0
*/
@InterfaceAudience.Public
@InterfaceStability.Stable
public interface ReaderBuilder extends Cloneable {
/**
* Finalize the creation of the {@link Reader} instance.
*
* This method will block until the reader is created successfully or an exception is thrown.
*
* @return the reader instance
* @throws PulsarClientException
* if the reader creation fails
*/
Reader create() throws PulsarClientException;
/**
* Finalize the creation of the {@link Reader} instance in asynchronous mode.
*
* This method will return a {@link CompletableFuture} that can be used to access the instance when it's ready.
*
* @return the reader instance
* @throws PulsarClientException
* if the reader creation fails
*/
CompletableFuture> createAsync();
/**
* Load the configuration from provided config map.
*
* Example:
*
*
{@code
* Map config = new HashMap<>();
* config.put("topicName", "test-topic");
* config.put("receiverQueueSize", 2000);
*
* ReaderBuilder builder = ...;
* builder = builder.loadConf(config);
*
* Reader reader = builder.create();
* }
*
* @param config
* configuration to load
* @return the reader builder instance
*/
ReaderBuilder loadConf(Map config);
/**
* Create a copy of the current {@link ReaderBuilder}.
*
* Cloning the builder can be used to share an incomplete configuration and specialize it multiple times. For
* example:
*
*
{@code
* ReaderBuilder builder = client.newReader(Schema.STRING)
* .readerName("my-reader")
* .receiverQueueSize(10);
*
* Reader reader1 = builder.clone().topic("topic-1").create();
* Reader reader2 = builder.clone().topic("topic-2").create();
* }
*
* @return a clone of the reader builder instance
*/
ReaderBuilder clone();
/**
* Specify the topic this reader will read from.
*
* This argument is required when constructing the reader.
*
* @param topicName
* the name of the topic
* @return the reader builder instance
*/
ReaderBuilder topic(String topicName);
/**
* Specify topics this reader will read from.
* @param topicNames
* @return
*/
ReaderBuilder topics(List topicNames);
/**
* The initial reader positioning is done by specifying a message id. The options are:
*
* - {@link MessageId#earliest}: Start reading from the earliest message available in the topic
* - {@link MessageId#latest}: Start reading from end of the topic. The first message read will be the one
* published *after* the creation of the builder
* - {@link MessageId}: Position the reader on a particular message. The first message read will be the one
* immediately *after* the specified message
*
*
* If the first message *after* the specified message is not the desired behaviour, use
* {@link ReaderBuilder#startMessageIdInclusive()}.
*
* @param startMessageId the message id where the reader will be initially positioned on
* @return the reader builder instance
*/
ReaderBuilder startMessageId(MessageId startMessageId);
/**
* The initial reader positioning can be set at specific timestamp by providing total rollback duration. so, broker
* can find a latest message that was published before given duration.
* eg: rollbackDuration in minute = 5 suggests broker to find message which was published 5 mins back and set the
* inital position on that messageId.
*
* @param rollbackDuration
* duration which position should be rolled back.
* @return
*/
ReaderBuilder startMessageFromRollbackDuration(long rollbackDuration, TimeUnit timeunit);
/**
* Set the reader to include the given position of {@link ReaderBuilder#startMessageId(MessageId)}
*
* This configuration option also applies for any cursor reset operation like {@link Reader#seek(MessageId)}.
*
* @return the reader builder instance
*/
ReaderBuilder startMessageIdInclusive();
/**
* Sets a {@link ReaderListener} for the reader.
*
* When a {@link ReaderListener} is set, application will receive messages through it. Calls to
* {@link Reader#readNext()} will not be allowed.
*
* @param readerListener
* the listener object
* @return the reader builder instance
*/
ReaderBuilder readerListener(ReaderListener readerListener);
/**
* Sets a {@link CryptoKeyReader} to decrypt the message payloads.
*
* @param cryptoKeyReader
* CryptoKeyReader object
* @return the reader builder instance
*/
ReaderBuilder cryptoKeyReader(CryptoKeyReader cryptoKeyReader);
/**
* Sets the default implementation of {@link CryptoKeyReader}.
*
* Configure the key reader to be used to decrypt the message payloads.
*
* @param privateKey
* the private key that is always used to decrypt message payloads.
* @return the reader builder instance
* @since 2.8.0
*/
ReaderBuilder defaultCryptoKeyReader(String privateKey);
/**
* Sets the default implementation of {@link CryptoKeyReader}.
*
* Configure the key reader to be used to decrypt the message payloads.
*
* @param privateKeys
* the map of private key names and their URIs used to decrypt message payloads.
* @return the reader builder instance
* @since 2.8.0
*/
ReaderBuilder defaultCryptoKeyReader(Map privateKeys);
/**
* Sets the {@link ConsumerCryptoFailureAction} to specify.
*
* @param action
* The action to take when the decoding fails
* @return the reader builder instance
*/
ReaderBuilder cryptoFailureAction(ConsumerCryptoFailureAction action);
/**
* Sets a {@link MessageCrypto}.
*
* Contains methods to encrypt/decrypt message for End to End Encryption.
*
* @param messageCrypto message Crypto Object
* @return ReaderBuilder instance
*/
ReaderBuilder messageCrypto(MessageCrypto messageCrypto);
/**
* Sets the size of the consumer receive queue.
*
* The consumer receive queue controls how many messages can be accumulated by the {@link Consumer} before the
* application calls {@link Consumer#receive()}. Using a higher value could potentially increase the consumer
* throughput at the expense of bigger memory utilization.
*
*
Default value is {@code 1000} messages and should be good for most use cases.
*
* @param receiverQueueSize
* the new receiver queue size value
* @return the reader builder instance
*/
ReaderBuilder receiverQueueSize(int receiverQueueSize);
/**
* Specify a reader name.
*
* The reader name is purely informational and can used to track a particular reader in the reported stats.
* By default a randomly generated name is used.
*
* @param readerName
* the name to use for the reader
* @return the reader builder instance
*/
ReaderBuilder readerName(String readerName);
/**
* Set the subscription role prefix. The default prefix is "reader".
*
* @param subscriptionRolePrefix
* @return the reader builder instance
*/
ReaderBuilder subscriptionRolePrefix(String subscriptionRolePrefix);
/**
* Set the subscription name.
* If subscriptionRolePrefix is set at the same time, this configuration will prevail
*
* @param subscriptionName
* @return the reader builder instance
*/
ReaderBuilder subscriptionName(String subscriptionName);
/**
* If enabled, the reader will read messages from the compacted topic rather than reading the full message backlog
* of the topic. This means that, if the topic has been compacted, the reader will only see the latest value for
* each key in the topic, up until the point in the topic message backlog that has been compacted. Beyond that
* point, the messages will be sent as normal.
*
* readCompacted can only be enabled when reading from a persistent topic. Attempting to enable it
* on non-persistent topics will lead to the reader create call throwing a {@link PulsarClientException}.
*
* @param readCompacted
* whether to read from the compacted topic
* @return the reader builder instance
*/
ReaderBuilder readCompacted(boolean readCompacted);
/**
* Set key hash range of the reader, broker will only dispatch messages which hash of the message key contains by
* the specified key hash range. Multiple key hash ranges can be specified on a reader.
*
* Total hash range size is 65536, so the max end of the range should be less than or equal to 65535.
*
* @param ranges
* key hash ranges for a reader
* @return the reader builder instance
*/
ReaderBuilder keyHashRange(Range... ranges);
/**
* Enable pooling of messages and the underlying data buffers.
*
* When pooling is enabled, the application is responsible for calling Message.release() after the handling of every
* received message. If “release()” is not called on a received message, there will be a memory leak. If an
* application attempts to use and already “released” message, it might experience undefined behavior (for example,
* memory corruption, deserialization error, etc.).
*/
ReaderBuilder poolMessages(boolean poolMessages);
/**
* If enabled, the reader will auto subscribe for partitions increasement.
* This is only for partitioned reader.
*
* @param autoUpdate
* whether to auto update partition increasement
* @return the reader builder instance
*/
ReaderBuilder autoUpdatePartitions(boolean autoUpdate);
/**
* Set the interval of updating partitions (default: 1 minute). This only works if autoUpdatePartitions is
* enabled.
*
* @param interval
* the interval of updating partitions
* @param unit
* the time unit of the interval.
* @return the reader builder instance
*/
ReaderBuilder autoUpdatePartitionsInterval(int interval, TimeUnit unit);
/**
* Intercept {@link Reader}.
*
* @param interceptors the list of interceptors to intercept the reader created by this builder.
* @return the reader builder instance
*/
ReaderBuilder intercept(ReaderInterceptor... interceptors);
/**
* Consumer buffers chunk messages into memory until it receives all the chunks of the original message. While
* consuming chunk-messages, chunks from same message might not be contiguous in the stream and they might be mixed
* with other messages' chunks. so, consumer has to maintain multiple buffers to manage chunks coming from different
* messages. This mainly happens when multiple publishers are publishing messages on the topic concurrently or
* publisher failed to publish all chunks of the messages.
*
*
* eg: M1-C1, M2-C1, M1-C2, M2-C2
* Here, Messages M1-C1 and M1-C2 belong to original message M1, M2-C1 and M2-C2 messages belong to M2 message.
*
* Buffering large number of outstanding uncompleted chunked messages can create memory pressure and it can be
* guarded by providing this @maxPendingChunkedMessage threshold. Once, consumer reaches this threshold, it drops
* the outstanding unchunked-messages by silently acking or asking broker to redeliver later by marking it unacked.
* This behavior can be controlled by configuration: @autoAckOldestChunkedMessageOnQueueFull
*
* The default value is 10.
*
* @param maxPendingChunkedMessage
* @return
*/
ReaderBuilder maxPendingChunkedMessage(int maxPendingChunkedMessage);
/**
* Buffering large number of outstanding uncompleted chunked messages can create memory pressure and it can be
* guarded by providing this @maxPendingChunkedMessage threshold. Once, consumer reaches this threshold, it drops
* the outstanding unchunked-messages by silently acking if autoAckOldestChunkedMessageOnQueueFull is true else it
* marks them for redelivery.
*
* @default false
*
* @param autoAckOldestChunkedMessageOnQueueFull
* @return
*/
ReaderBuilder autoAckOldestChunkedMessageOnQueueFull(boolean autoAckOldestChunkedMessageOnQueueFull);
/**
* If producer fails to publish all the chunks of a message then consumer can expire incomplete chunks if consumer
* won't be able to receive all chunks in expire times (default 1 minute).
*
* @param duration
* @param unit
* @return
*/
ReaderBuilder expireTimeOfIncompleteChunkedMessage(long duration, TimeUnit unit);
}