Class MqttClientPublishBuilder


  • public final class MqttClientPublishBuilder
    extends Object
    High-level builder for one client-side MQTT PUBLISH operation.

    Use this builder when application code wants to publish one value and then fluently choose MQTT options such as QoS, retain handling and MQTT 5 metadata. The builder creates an immutable publish message when send() is called and lets the client perform the required MQTT validation and acknowledgement flow.

    • Method Detail

      • qos2

        public MqttClientPublishBuilder qos2()
        Uses QoS 2 for the publish.

        QoS 2 uses the MQTT exactly-once acknowledgement flow. It is useful when duplicate processing would be more expensive than the additional protocol round trips.

        Returns:
        this builder
      • retain

        public MqttClientPublishBuilder retain​(boolean value)
        Sets the MQTT Retain flag.
        Parameters:
        value - true to ask the broker to retain the message
        Returns:
        this builder
      • payloadFormatUtf8

        public MqttClientPublishBuilder payloadFormatUtf8​(Boolean value)
        Sets the MQTT 5 Payload Format Indicator property.

        Use true for UTF-8 character data and false for unspecified binary data. Supplying null omits the property. The property is encoded only when the negotiated connection uses MQTT 5.0.

        Parameters:
        value - payload format hint, or null to omit the property
        Returns:
        this builder
      • payloadFormatUtf8

        public MqttClientPublishBuilder payloadFormatUtf8()
        Declares the payload as UTF-8 character data for MQTT 5 receivers.
        Returns:
        this builder
      • payloadFormatBytes

        public MqttClientPublishBuilder payloadFormatBytes()
        Declares the payload as unspecified binary data for MQTT 5 receivers.
        Returns:
        this builder
      • contentType

        public MqttClientPublishBuilder contentType​(String value)
        Sets the MQTT 5 Content Type property.

        The property is encoded only when the negotiated connection uses MQTT 5.0. Use media types such as application/json when receivers should be able to inspect the payload format without parsing the topic name.

        Parameters:
        value - content type, or null to omit the property
        Returns:
        this builder
      • messageExpirySeconds

        public MqttClientPublishBuilder messageExpirySeconds​(Long value)
        Sets the MQTT 5 Message Expiry Interval property.
        Parameters:
        value - expiry interval in seconds, or null to omit the property
        Returns:
        this builder
        Throws:
        IllegalArgumentException - if value is outside the MQTT unsigned 32-bit range
      • messageExpirySeconds

        public MqttClientPublishBuilder messageExpirySeconds​(long value)
        Sets the MQTT 5 Message Expiry Interval property.
        Parameters:
        value - expiry interval in seconds
        Returns:
        this builder
        Throws:
        IllegalArgumentException - if value is outside the MQTT unsigned 32-bit range
      • responseTopic

        public MqttClientPublishBuilder responseTopic​(String value)
        Sets the MQTT 5 Response Topic property.

        Response Topic is the standard MQTT 5 request/response hint. It tells a receiver where a reply should be published. The value must be a Topic Name, not a Topic Filter; wildcard characters are rejected by the underlying protocol encoder before the packet is sent.

        Parameters:
        value - response topic, or null to omit the property
        Returns:
        this builder
      • correlationData

        public MqttClientPublishBuilder correlationData​(byte[] value)
        Sets the MQTT 5 Correlation Data property.

        Correlation Data is opaque application-owned binary data. A common pattern is to place a request identifier here so the response handler can match a later message on the response topic to the original request.

        Parameters:
        value - correlation data bytes, or null to omit the property
        Returns:
        this builder
      • userProperty

        public MqttClientPublishBuilder userProperty​(String name,
                                                     String value)
        Adds one MQTT 5 User Property.

        User Properties are application metadata and may be repeated. The builder keeps insertion order so receivers see the same order in the decoded message.

        Parameters:
        name - property name
        value - property value
        Returns:
        this builder
        Throws:
        NullPointerException - if name or value is null
      • send

        public MqttPublishResult send()
        Sends the publish through the underlying client.
        Returns:
        publish result describing the message sent by this operation