Interface Producer
- All Superinterfaces:
AutoCloseable,Closeable
- All Known Implementing Classes:
ProducerV1Impl
-
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Close the producer and releases resources allocated.Close the producer and releases resources allocated.voidflush()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 asynchronously.longGet the last sequence id that was published by this producer.org.apache.pulsar.client.api.ProducerStatsgetStats()Get statistics for the producer.getTopic()booleanorg.apache.pulsar.client.api.MessageIdsend(byte[] message) Sends a message.org.apache.pulsar.client.api.MessageIdsend(org.apache.pulsar.client.api.Message<byte[]> message) Deprecated.since 2.0.CompletableFuture<org.apache.pulsar.client.api.MessageId>sendAsync(byte[] message) Send a message asynchronouslyCompletableFuture<org.apache.pulsar.client.api.MessageId>sendAsync(org.apache.pulsar.client.api.Message<byte[]> message) Deprecated.since 2.0.
-
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
org.apache.pulsar.client.api.MessageId send(byte[] message) throws org.apache.pulsar.client.api.PulsarClientException 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:
org.apache.pulsar.client.api.PulsarClientException.TimeoutException- if the message was not correctly received by the system within the timeout periodorg.apache.pulsar.client.api.PulsarClientException.AlreadyClosedException- if the producer was already closedorg.apache.pulsar.client.api.PulsarClientException
-
sendAsync
Send a message asynchronouslyWhen the producer queue is full, by default this method will complete the future with an exception
PulsarClientException.ProducerQueueIsFullErrorSee
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
void flush() throws org.apache.pulsar.client.api.PulsarClientExceptionFlush all the messages buffered in the client and wait until all messages have been successfully persisted.- Throws:
org.apache.pulsar.client.api.PulsarClientException- Since:
- 2.1.0
- See Also:
-
flushAsync
CompletableFuture<Void> flushAsync()Flush all the messages buffered in the client asynchronously.- Returns:
- a future that can be used to track when all the messages have been safely persisted.
- Since:
- 2.1.0
- See Also:
-
send
@Deprecated org.apache.pulsar.client.api.MessageId send(org.apache.pulsar.client.api.Message<byte[]> message) throws org.apache.pulsar.client.api.PulsarClientException Deprecated.since 2.0. UseTypedMessageBuilderas returned by {@link Producer.newMessage()} to create a new message builder.Send a message.- Parameters:
message- a message- Returns:
- the message id assigned to the published message
- Throws:
org.apache.pulsar.client.api.PulsarClientException.TimeoutException- if the message was not correctly received by the system within the timeout periodorg.apache.pulsar.client.api.PulsarClientException
-
sendAsync
@Deprecated CompletableFuture<org.apache.pulsar.client.api.MessageId> sendAsync(org.apache.pulsar.client.api.Message<byte[]> message) Deprecated.since 2.0. UseTypedMessageBuilderas returned byProducer#newMessage()to create a new message builder.Send a message asynchronously.When the returned
CompletableFutureis marked as completed successfully, the provided message will contain theMessageIdassigned by the broker to the published message.Example:
Message msg = MessageBuilder.create().setContent(myContent).build(); producer.sendAsync(msg).thenRun(v -> { System.out.println("Published message: " + msg.getMessageId()); }).exceptionally(e -> { // Failed to publish });When the producer queue is full, by default this method will complete the future with an exception
PulsarClientException.ProducerQueueIsFullErrorSee
ProducerBuilder.maxPendingMessages(int)to configure the producer queue size andProducerBuilder.blockIfQueueFull(boolean)to change the blocking behavior.- Parameters:
message- a message- Returns:
- a future that can be used to track when the message will have been safely persisted
-
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
MessageBuilder) 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
org.apache.pulsar.client.api.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
void close() throws org.apache.pulsar.client.api.PulsarClientExceptionClose 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:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Throws:
org.apache.pulsar.client.api.PulsarClientException.AlreadyClosedException- if the producer was already closedorg.apache.pulsar.client.api.PulsarClientException
-
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 connected to the broker
-