Interface Producer<T>

  • All Superinterfaces:
    java.lang.AutoCloseable, java.io.Closeable

    @Public
    @Stable
    public interface Producer<T>
    extends java.io.Closeable
    Producer is used to publish messages on a topic.

    A single producer instance can be used across multiple threads.

    • Method Detail

      • getTopic

        java.lang.String getTopic()
        Returns:
        the topic which producer is publishing to
      • getProducerName

        java.lang.String getProducerName()
        Returns:
        the producer name which could have been assigned by the system or specified by the client
      • flushAsync

        java.util.concurrent.CompletableFuture<java.lang.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:
        flush()
      • 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

        <V> TypedMessageBuilder<V> newMessage​(Schema<V> schema)
        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()
      • newMessage

        TypedMessageBuilder<T> newMessage​(Transaction txn)
        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:
        newMessage()
      • 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

        void close()
            throws PulsarClientException
        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 interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
        Throws:
        PulsarClientException.AlreadyClosedException - if the producer was already closed
        PulsarClientException
      • closeAsync

        java.util.concurrent.CompletableFuture<java.lang.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