Interface Producer<T>
- All Superinterfaces:
AutoCloseable
,Closeable
A single producer instance can be used across multiple threads.
-
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
Close the producer and releases resources allocated.Close the producer and releases resources allocated.void
flush()
Flush all the messages buffered in the client and wait until all messages have been successfully persisted.Flush all the messages buffered in the client and wait until all messages have been successfully persisted.long
long
Get the last sequence id that was published by this producer.int
getStats()
Get statistics for the producer.getTopic()
boolean
Create a new message builder.<V> TypedMessageBuilder<V>
newMessage
(Schema<V> schema) Create a new message builder with schema, not required same parameterized type with the producer.newMessage
(Transaction txn) Create a new message builder with transaction.Sends a message.Send a message asynchronously.
-
Method Details
-
getTopic
String getTopic()- Returns:
- the topic which producer is publishing to
-
getProducerName
String getProducerName()- Returns:
- the producer name which could have been assigned by the system or specified by the client
-
send
Sends a message.This call will be blocking until is successfully acknowledged by the Pulsar broker.
Use
newMessage()
to specify more properties than just the value on the message to be sent.- Parameters:
message
- a message- Returns:
- the message id assigned to the published message
- Throws:
PulsarClientException.TimeoutException
- if the message was not correctly received by the system within the timeout periodPulsarClientException.AlreadyClosedException
- if the producer was already closedPulsarClientException
-
sendAsync
Send a message asynchronously.When the producer queue is full, by default this method will complete the future with an exception
PulsarClientException.ProducerQueueIsFullError
See
ProducerBuilder.maxPendingMessages(int)
to configure the producer queue size andProducerBuilder.blockIfQueueFull(boolean)
to change the blocking behavior.Use
newMessage()
to specify more properties than just the value on the message to be sent.- Parameters:
message
- a byte array with the payload of the message- Returns:
- a future that can be used to track when the message will have been safely persisted
-
flush
Flush all the messages buffered in the client and wait until all messages have been successfully persisted.- Throws:
PulsarClientException
- Since:
- 2.1.0
- See Also:
-
flushAsync
CompletableFuture<Void> flushAsync()Flush all the messages buffered in the client and wait until all messages have been successfully persisted.- Returns:
- a future that can be used to track when all the messages have been safely persisted.
- Since:
- 2.1.0
- See Also:
-
newMessage
TypedMessageBuilder<T> newMessage()Create a new message builder.This message builder allows to specify additional properties on the message. For example:
producer.newMessage() .key(messageKey) .value(myValue) .property("user-defined-property", "value") .send();
- Returns:
- a typed message builder that can be used to construct the message to be sent through this producer
-
newMessage
Create a new message builder with schema, not required same parameterized type with the producer.- Returns:
- a typed message builder that can be used to construct the message to be sent through this producer
- See Also:
-
newMessage
Create a new message builder with transaction.After the transaction commit, it will be made visible to consumer.
After the transaction abort, it will never be visible to consumer.
- Returns:
- a typed message builder that can be used to construct the message to be sent through this producer
- Since:
- 2.7.0
- See Also:
-
getLastSequenceId
long getLastSequenceId()Get the last sequence id that was published by this producer.This represent either the automatically assigned or custom sequence id (set on the
TypedMessageBuilder
) that was published and acknowledged by the broker.After recreating a producer with the same producer name, this will return the last message that was published in the previous producer session, or -1 if there no message was ever published.
- Returns:
- the last sequence id published by this producer
-
getStats
ProducerStats getStats()Get statistics for the producer.- numMsgsSent : Number of messages sent in the current interval
- numBytesSent : Number of bytes sent in the current interval
- numSendFailed : Number of messages failed to send in the current interval
- numAcksReceived : Number of acks received in the current interval
- totalMsgsSent : Total number of messages sent
- totalBytesSent : Total number of bytes sent
- totalSendFailed : Total number of messages failed to send
- totalAcksReceived: Total number of acks received
- Returns:
- statistic for the producer or null if ProducerStatsRecorderImpl is disabled.
-
close
Close the producer and releases resources allocated.No more writes will be accepted from this producer. Waits until all pending write request are persisted. In case of errors, pending writes will not be retried.
- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Throws:
PulsarClientException.AlreadyClosedException
- if the producer was already closedPulsarClientException
-
closeAsync
CompletableFuture<Void> closeAsync()Close the producer and releases resources allocated.No more writes will be accepted from this producer. Waits until all pending write request are persisted. In case of errors, pending writes will not be retried.
- Returns:
- a future that can used to track when the producer has been closed
-
isConnected
boolean isConnected()- Returns:
- Whether the producer is currently connected to the broker
-
getLastDisconnectedTimestamp
long getLastDisconnectedTimestamp()- Returns:
- The last disconnected timestamp of the producer
-
getNumOfPartitions
int getNumOfPartitions()- Returns:
- the number of partitions per topic.
-