@InterfaceAudience.Public @InterfaceStability.Stable public interface PulsarClient extends Closeable
Modifier and Type | Method and Description |
---|---|
static ClientBuilder |
builder()
Get a new builder instance that can used to configure and build a
PulsarClient instance. |
void |
close()
Close the PulsarClient and release all the resources.
|
CompletableFuture<Void> |
closeAsync()
Asynchronously close the PulsarClient and release all the resources.
|
CompletableFuture<List<String>> |
getPartitionsForTopic(String topic)
Get the list of partitions for a given topic.
|
boolean |
isClosed()
Return internal state of the client.
|
ConsumerBuilder<byte[]> |
newConsumer()
Create a consumer builder with no schema (
Schema.BYTES ) for subscribing to
one or more topics. |
<T> ConsumerBuilder<T> |
newConsumer(Schema<T> schema)
Create a consumer builder with a specific schema for subscribing on a specific topic
|
ProducerBuilder<byte[]> |
newProducer()
Create a producer builder that can be used to configure
and construct a producer with default
Schema.BYTES . |
<T> ProducerBuilder<T> |
newProducer(Schema<T> schema)
Create a producer builder that can be used to configure
and construct a producer with the specified schema.
|
ReaderBuilder<byte[]> |
newReader()
Create a topic reader builder with no schema (
Schema.BYTES ) to read from the specified topic. |
<T> ReaderBuilder<T> |
newReader(Schema<T> schema)
Create a topic reader builder with a specific
Schema ) to read from the specified topic. |
TransactionBuilder |
newTransaction()
Create a transaction builder that can be used to configure
and construct a transaction.
|
void |
shutdown()
Perform immediate shutdown of PulsarClient.
|
void |
updateServiceUrl(String serviceUrl)
Update the service URL this client is using.
|
static ClientBuilder builder()
PulsarClient
instance.ClientBuilder
ProducerBuilder<byte[]> newProducer()
Schema.BYTES
.
Example:
Producer<byte[]> producer = client.newProducer()
.topic("my-topic")
.create();
producer.send("test".getBytes());
ProducerBuilder
object to configure and construct the Producer
instance<T> ProducerBuilder<T> newProducer(Schema<T> schema)
Example:
Producer<String> producer = client.newProducer(Schema.STRING)
.topic("my-topic")
.create();
producer.send("test");
schema
- provide a way to convert between serialized data and domain objectsProducerBuilder
object to configure and construct the Producer
instanceConsumerBuilder<byte[]> newConsumer()
Schema.BYTES
) for subscribing to
one or more topics.
Consumer<byte[]> consumer = client.newConsumer()
.topic("my-topic")
.subscriptionName("my-subscription-name")
.subscribe();
while (true) {
Message<byte[]> message = consumer.receive();
System.out.println("Got message: " + message.getValue());
consumer.acknowledge(message);
}
ConsumerBuilder
object to configure and construct the Consumer
instance<T> ConsumerBuilder<T> newConsumer(Schema<T> schema)
Since 2.2, if you are creating a consumer with non-bytes schema on a non-existence topic, it will automatically create the topic with the provided schema.
Consumer<String> consumer = client.newConsumer(Schema.STRING)
.topic("my-topic")
.subscriptionName("my-subscription-name")
.subscribe();
while (true) {
Message<String> message = consumer.receive();
System.out.println("Got message: " + message.getValue());
consumer.acknowledge(message);
}
schema
- provide a way to convert between serialized data and domain objectsConsumerBuilder
object to configure and construct the Consumer
instanceReaderBuilder<byte[]> newReader()
Schema.BYTES
) to read from the specified topic.
The Reader provides a low-level abstraction that allows for manual positioning in the topic, without using a
subscription. A reader needs to be specified a ReaderBuilder.startMessageId(MessageId)
that can either be:
MessageId.earliest
: Start reading from the earliest message available in the topicMessageId.latest
: Start reading from end of the topic. The first message read will be the one
published *after* the creation of the builderMessageId
: Position the reader on a particular message. The first message read will be the one
immediately *after* the specified messageA Reader can only from non-partitioned topics. In case of partitioned topics, one can create the readers
directly on the individual partitions. See getPartitionsForTopic(String)
for how to get the
topic partitions names.
Example of usage of Reader:
Reader<byte[]> reader = client.newReader()
.topic("my-topic")
.startMessageId(MessageId.earliest)
.create();
while (true) {
Message<byte[]> message = reader.readNext();
System.out.println("Got message: " + message.getValue());
// Reader doesn't need acknowledgments
}
ReaderBuilder
that can be used to configure and construct a Reader
instance<T> ReaderBuilder<T> newReader(Schema<T> schema)
Schema
) to read from the specified topic.
The Reader provides a low-level abstraction that allows for manual positioning in the topic, without using a
subscription. A reader needs to be specified a ReaderBuilder.startMessageId(MessageId)
that can either
be:
MessageId.earliest
: Start reading from the earliest message available in the topicMessageId.latest
: Start reading from end of the topic. The first message read will be the one
published *after* the creation of the builderMessageId
: Position the reader on a particular message. The first message read will be the one
immediately *after* the specified messageA Reader can only from non-partitioned topics. In case of partitioned topics, one can create the readers
directly on the individual partitions. See getPartitionsForTopic(String)
for how to get the
topic partitions names.
Example of usage of Reader:
Reader<String> reader = client.newReader(Schema.STRING)
.topic("my-topic")
.startMessageId(MessageId.earliest)
.create();
while (true) {
Message<String> message = reader.readNext();
System.out.println("Got message: " + message.getValue());
// Reader doesn't need acknowledgments
}
ReaderBuilder
that can be used to configure and construct a Reader
instancevoid updateServiceUrl(String serviceUrl) throws PulsarClientException
This will force the client close all existing connections and to restart service discovery to the new service endpoint.
serviceUrl
- the new service URL this client should connect toPulsarClientException
- in case the serviceUrl is not validCompletableFuture<List<String>> getPartitionsForTopic(String topic)
If the topic is partitioned, this will return a list of partition names. If the topic is not partitioned, the returned list will contain the topic name itself.
This can be used to discover the partitions and create Reader
, Consumer
or Producer
instances directly on a particular partition.
topic
- the topic namePulsarClientException
if there was any
error in the operation.void close() throws PulsarClientException
This operation will trigger a graceful close of all producer, consumer and reader instances that this client has currently active. That implies that close will block and wait until all pending producer send requests are persisted.
close
in interface AutoCloseable
close
in interface Closeable
PulsarClientException
- if the close operation failsCompletableFuture<Void> closeAsync()
This operation will trigger a graceful close of all producer, consumer and reader instances that this client has currently active. That implies that close and wait, asynchronously, until all pending producer send requests are persisted.
PulsarClientException
- if the close operation failsvoid shutdown() throws PulsarClientException
Release all the resources and close all the producer, consumer and reader instances without waiting for ongoing operations to complete.
PulsarClientException
- if the forceful shutdown failsboolean isClosed()
shutdown()
,
close()
,
closeAsync()
TransactionBuilder newTransaction()
Example:
Transaction txn = client.newTransaction()
.withTransactionTimeout(1, TimeUnit.MINUTES)
.build().get();
TransactionBuilder
object to configure and construct
the Transaction
instanceCopyright © 2017–2021 Apache Software Foundation. All rights reserved.