Interface Message<T>


  • @Public
    @Stable
    public interface Message<T>
    The message abstraction used in Pulsar.
    • Method Summary

      All Methods Instance Methods Abstract Methods Default Methods 
      Modifier and Type Method Description
      java.util.Optional<java.lang.Long> getBrokerPublishTime()
      Get broker publish time from broker entry metadata.
      byte[] getData()
      Get the raw payload of the message.
      java.util.Optional<EncryptionContext> getEncryptionCtx()
      EncryptionContext contains encryption and compression information in it using which application can decrypt consumed message with encrypted-payload.
      long getEventTime()
      Get the event time associated with this message.
      java.util.Optional<java.lang.Long> getIndex()
      Get index from broker entry metadata.
      java.lang.String getKey()
      Get the key of the message.
      byte[] getKeyBytes()
      Get bytes in key.
      MessageId getMessageId()
      Get the unique message ID associated with this message.
      byte[] getOrderingKey()
      Get the ordering key of the message.
      java.lang.String getProducerName()
      Get the producer name who produced this message.
      java.util.Map<java.lang.String,​java.lang.String> getProperties()
      Return the properties attached to the message.
      java.lang.String getProperty​(java.lang.String name)
      Get the value of a specific property.
      long getPublishTime()
      Get the publish time of this message.
      default java.util.Optional<Schema<?>> getReaderSchema()
      Get the schema associated to the message.
      int getRedeliveryCount()
      Get message redelivery count, redelivery count maintain in pulsar broker.
      java.lang.String getReplicatedFrom()
      Get name of cluster, from which the message is replicated.
      byte[] getSchemaVersion()
      Get schema version of the message.
      long getSequenceId()
      Get the sequence id associated with this message.
      java.lang.String getTopicName()
      Get the topic the message was published to.
      T getValue()
      Get the de-serialized value of the message, according the configured Schema.
      boolean hasBase64EncodedKey()
      Check whether the key has been base64 encoded.
      boolean hasBrokerPublishTime()
      Check whether the message has a broker publish time
      boolean hasIndex()
      Check whether the message has a index.
      boolean hasKey()
      Check whether the message has a key.
      boolean hasOrderingKey()
      Check whether the message has a ordering key.
      boolean hasProperty​(java.lang.String name)
      Check whether the message has a specific property attached.
      boolean isReplicated()
      Check whether the message is replicated from other cluster.
      void release()
      Release a message back to the pool.
      int size()
      Get the uncompressed message payload size in bytes.
    • Method Detail

      • getProperties

        java.util.Map<java.lang.String,​java.lang.String> getProperties()
        Return the properties attached to the message.

        Properties are application defined key/value pairs that will be attached to the message.

        Returns:
        an unmodifiable view of the properties map
      • hasProperty

        boolean hasProperty​(java.lang.String name)
        Check whether the message has a specific property attached.
        Parameters:
        name - the name of the property to check
        Returns:
        true if the message has the specified property and false if the properties is not defined
      • getProperty

        java.lang.String getProperty​(java.lang.String name)
        Get the value of a specific property.
        Parameters:
        name - the name of the property
        Returns:
        the value of the property or null if the property was not defined
      • getData

        byte[] getData()
        Get the raw payload of the message.

        Even when using the Schema and type-safe API, an application has access to the underlying raw message payload.

        Returns:
        the byte array with the message payload
      • size

        int size()
        Get the uncompressed message payload size in bytes.
        Returns:
        size in bytes.
      • getValue

        T getValue()
        Get the de-serialized value of the message, according the configured Schema.
        Returns:
        the deserialized value of the message
      • getMessageId

        MessageId getMessageId()
        Get the unique message ID associated with this message.

        The message id can be used to univocally refer to a message without having the keep the entire payload in memory.

        Only messages received from the consumer will have a message id assigned.

        Returns:
        the message id null if this message was not received by this client instance
      • getPublishTime

        long getPublishTime()
        Get the publish time of this message. The publish time is the timestamp that a client publish the message.
        Returns:
        publish time of this message.
        See Also:
        getEventTime()
      • getEventTime

        long getEventTime()
        Get the event time associated with this message. It is typically set by the applications via MessageBuilder#setEventTime(long).

        If there isn't any event time associated with this event, it will return 0.

        Returns:
        the message event time or 0 if event time wasn't set
        Since:
        1.20.0
        See Also:
        MessageBuilder#setEventTime(long)
      • getSequenceId

        long getSequenceId()
        Get the sequence id associated with this message. It is typically set by the applications via MessageBuilder#setSequenceId(long).
        Returns:
        sequence id associated with this message.
        Since:
        1.22.0
        See Also:
        MessageBuilder#setEventTime(long)
      • getProducerName

        java.lang.String getProducerName()
        Get the producer name who produced this message.
        Returns:
        producer name who produced this message, null if producer name is not set.
        Since:
        1.22.0
      • hasKey

        boolean hasKey()
        Check whether the message has a key.
        Returns:
        true if the key was set while creating the message and false if the key was not set while creating the message
      • getKey

        java.lang.String getKey()
        Get the key of the message.
        Returns:
        the key of the message
      • hasBase64EncodedKey

        boolean hasBase64EncodedKey()
        Check whether the key has been base64 encoded.
        Returns:
        true if the key is base64 encoded, false otherwise
      • getKeyBytes

        byte[] getKeyBytes()
        Get bytes in key. If the key has been base64 encoded, it is decoded before being returned. Otherwise, if the key is a plain string, this method returns the UTF_8 encoded bytes of the string.
        Returns:
        the key in byte[] form
      • hasOrderingKey

        boolean hasOrderingKey()
        Check whether the message has a ordering key.
        Returns:
        true if the ordering key was set while creating the message false if the ordering key was not set while creating the message
      • getOrderingKey

        byte[] getOrderingKey()
        Get the ordering key of the message.
        Returns:
        the ordering key of the message
      • getTopicName

        java.lang.String getTopicName()
        Get the topic the message was published to.
        Returns:
        the topic the message was published to
      • getEncryptionCtx

        java.util.Optional<EncryptionContext> getEncryptionCtx()
        EncryptionContext contains encryption and compression information in it using which application can decrypt consumed message with encrypted-payload.
        Returns:
        the optiona encryption context
      • getRedeliveryCount

        int getRedeliveryCount()
        Get message redelivery count, redelivery count maintain in pulsar broker. When client acknowledge message timeout, broker will dispatch message again with message redelivery count in CommandMessage defined.

        Message redelivery increases monotonically in a broker, when topic switch ownership to a another broker redelivery count will be recalculated.

        Returns:
        message redelivery count
        Since:
        2.3.0
      • getSchemaVersion

        byte[] getSchemaVersion()
        Get schema version of the message.
        Returns:
        Schema version of the message if the message is produced with schema otherwise null.
        Since:
        2.4.0
      • getReaderSchema

        default java.util.Optional<Schema<?>> getReaderSchema()
        Get the schema associated to the message. Please note that this schema is usually equal to the Schema you passed during the construction of the Consumer or the Reader. But if you are consuming the topic using the GenericObject interface this method will return the schema associated with the message.
        Returns:
        The schema used to decode the payload of message.
        See Also:
        Schema.AUTO_CONSUME()
      • isReplicated

        boolean isReplicated()
        Check whether the message is replicated from other cluster.
        Returns:
        true if the message is replicated from other cluster. false otherwise.
        Since:
        2.4.0
      • getReplicatedFrom

        java.lang.String getReplicatedFrom()
        Get name of cluster, from which the message is replicated.
        Returns:
        the name of cluster, from which the message is replicated.
        Since:
        2.4.0
      • release

        void release()
        Release a message back to the pool. This is required only if the consumer was created with the option to pool messages, otherwise it will have no effect.
        Since:
        2.8.0
      • hasBrokerPublishTime

        boolean hasBrokerPublishTime()
        Check whether the message has a broker publish time
        Returns:
        true if the message has a broker publish time, otherwise false.
        Since:
        2.9.0
      • getBrokerPublishTime

        java.util.Optional<java.lang.Long> getBrokerPublishTime()
        Get broker publish time from broker entry metadata. Note that only if the feature is enabled in the broker then the value is available.
        Returns:
        broker publish time from broker entry metadata, or empty if the feature is not enabled in the broker.
        Since:
        2.9.0
      • hasIndex

        boolean hasIndex()
        Check whether the message has a index.
        Returns:
        true if the message has a index, otherwise false.
        Since:
        2.9.0
      • getIndex

        java.util.Optional<java.lang.Long> getIndex()
        Get index from broker entry metadata. Note that only if the feature is enabled in the broker then the value is available.
        Returns:
        index from broker entry metadata, or empty if the feature is not enabled in the broker.
        Since:
        2.9.0