Class MqttMessage


  • public final class MqttMessage
    extends Object
    High-level view of an MQTT application message.

    This type is passed to message listeners so application code can read the Topic Name, QoS, retain flag, MQTT 5 metadata and payload without handling protocol packets directly. Payload helper methods cover the common text, number, boolean, binary and JSON cases while still keeping access to the prepared publish message for advanced integrations.

    • Constructor Detail

      • MqttMessage

        public MqttMessage​(PlccomMqttPublishMessage message)
        Creates a high-level message view.
        Parameters:
        message - prepared publish message to expose through this view
        Throws:
        NullPointerException - if message is null
    • Method Detail

      • topic

        public String topic()
        Returns the message topic.
        Returns:
        MQTT topic name
      • retain

        public boolean retain()
        Returns whether the message carried the MQTT Retain flag.
        Returns:
        true when the message was retained by the broker
      • payloadFormatUtf8

        public Boolean payloadFormatUtf8()
        Returns the MQTT 5 Payload Format Indicator property.

        Boolean.TRUE means the sender declared the payload as UTF-8 character data. Boolean.FALSE means the sender declared the payload as unspecified binary data. null means the message did not carry the property.

        Returns:
        payload format hint, or null when omitted
      • contentType

        public String contentType()
        Returns the MQTT 5 Content Type property.
        Returns:
        content type, or null when the message did not carry the property
      • messageExpirySeconds

        public Long messageExpirySeconds()
        Returns the MQTT 5 Message Expiry Interval property.
        Returns:
        expiry interval in seconds, or null when the message did not carry the property
      • responseTopic

        public String responseTopic()
        Returns the MQTT 5 Response Topic property.
        Returns:
        response topic, or null when the message did not carry the property
      • correlationData

        public byte[] correlationData()
        Returns the MQTT 5 Correlation Data property.

        The returned array is a defensive copy. Correlation Data is opaque application data and may be text, binary identifiers or any other byte sequence agreed between the MQTT applications.

        Returns:
        correlation data bytes, or null when the message did not carry the property
      • userProperties

        public List<PlccomMqttUserProperty> userProperties()
        Returns the MQTT 5 User Properties.
        Returns:
        immutable user-property list, never null
      • subscriptionIdentifiers

        public List<Integer> subscriptionIdentifiers()
        Returns MQTT 5 Subscription Identifiers carried by this message.

        The list is populated when an MQTT 5 broker forwards a PUBLISH because it matched one or more subscriptions that were created with Subscription Identifiers. It is empty for MQTT 3.1.1 messages and MQTT 5 messages that did not carry the property.

        Returns:
        immutable Subscription Identifier list, never null
      • payloadAsBytes

        public byte[] payloadAsBytes()
        Returns the payload bytes.
        Returns:
        defensive payload copy, never null
      • payloadAsString

        public String payloadAsString()
        Returns the payload decoded as UTF-8 text.
        Returns:
        UTF-8 decoded payload text
      • payloadAsInt

        public int payloadAsInt()
        Returns the payload parsed as an integer.
        Returns:
        integer payload value
        Throws:
        NumberFormatException - if the payload text is not an integer
      • payloadAsLong

        public long payloadAsLong()
        Returns the payload parsed as a long.
        Returns:
        long payload value
        Throws:
        NumberFormatException - if the payload text is not a long
      • payloadAsDouble

        public double payloadAsDouble()
        Returns the payload parsed as a double.
        Returns:
        double payload value
        Throws:
        NumberFormatException - if the payload text is not a double
      • payloadAsBoolean

        public boolean payloadAsBoolean()
        Returns the payload parsed as a boolean.
        Returns:
        boolean payload value
        Throws:
        IllegalArgumentException - if the payload is neither true nor false, ignoring case and surrounding whitespace
      • toPublishMessage

        public PlccomMqttPublishMessage toPublishMessage()
        Returns the prepared publish message behind this high-level view.
        Returns:
        prepared publish message