Class MqttMessage


  • public class MqttMessage
    extends Object
    An MQTT message holds the application payload and options specifying how the message is to be delivered The message includes a "payload" (the body of the message) represented as a byte[].
    • Constructor Summary

      Constructors 
      Constructor Description
      MqttMessage()
      Constructs a message with an empty payload, and all other values set to defaults.
      MqttMessage​(byte[] payload)
      Constructs a message with the specified byte array as a payload, and all other values set to defaults.
    • Constructor Detail

      • MqttMessage

        public MqttMessage()
        Constructs a message with an empty payload, and all other values set to defaults. The defaults are:
        • Message QoS set to 1
        • Message will not be "retained" by the server
      • MqttMessage

        public MqttMessage​(byte[] payload)
        Constructs a message with the specified byte array as a payload, and all other values set to defaults.
        Parameters:
        payload - The Bytearray of the payload. Important Note : The array is not cloned for memory efficiency reasons. Be careful with it.
    • Method Detail

      • validateQos

        public static void validateQos​(int qos)
        Utility method to validate the supplied QoS value.
        Parameters:
        qos - The QoS Level
        Throws:
        IllegalArgumentException - if value of QoS is not 0, 1 or 2.
      • getPayload

        public byte[] getPayload()
        Returns the payload as a byte array.
        Returns:
        the payload as a byte array. Important Note : The array is not cloned for memory efficiency reasons. Be careful with it.
      • setPayload

        public void setPayload​(byte[] payload)
        Sets the payload of this message to be the specified byte array.
        Parameters:
        payload - the payload for this message. Important Note : The array is not cloned for memory efficiency reasons. Be careful with it.
      • isRetained

        public boolean isRetained()
        Returns whether or not this message should be/was retained by the server. For messages received from the server, this method returns whether or not the message was from a current publisher, or was "retained" by the server as the last message published on the topic.
        Returns:
        true if the message should be, or was, retained by the server.
        See Also:
        setRetained(boolean)
      • setRetained

        public void setRetained​(boolean retained)
        Whether or not the publish message should be retained by the messaging engine. Sending a message with retained set to true and with an empty byte array as the payload e.g. new byte[0] will clear the retained message from the server. The default value is false
        Parameters:
        retained - whether or not the messaging engine should retain the message.
      • getQos

        public int getQos()
        Returns the quality of service for this message.
        Returns:
        the quality of service to use, either 0, 1.
        See Also:
        setQos(int)
      • setQos

        public void setQos​(int qos)
        Sets the quality of service for this message.
        • Quality of Service 0 - indicates that a message should be delivered at most once (zero or one times). The message will not be persisted to disk, and will not be acknowledged across the network. This QoS is the fastest, but should only be used for messages which are not valuable. Also known as "fire and forget".
        • Quality of Service 1 - indicates that a message should be delivered at least once (one or more times). The message will be acknowledged across the network. This is the default QoS.
        Parameters:
        qos - the "quality of service" to use. Set to 0 or 1.
        Throws:
        IllegalArgumentException - if value of QoS is not 0 or 1.
        IllegalStateException - if this message cannot be edited
      • toString

        public String toString()
        Returns a string representation of this message's payload. Makes an attempt to return the payload as a string. As the MQTT client has no control over the content of the payload it may fail.
        Overrides:
        toString in class Object
        Returns:
        a string representation of this message.