Class MqttMessage
An immutable application message: a topic, a payload, a QoS, a retain flag, and the MQTT 5.0
message properties. The payload and the Correlation Data are copied on construction and on
access. Equality compares every component, including the payload contents and the order of
the User Properties. toString() reports the payload, the Correlation Data, and the User
Properties only by size, because they carry application data, and leaves out properties
that are not set. Each with method returns a copy with one setting changed.
All QoS values can be represented and published. The properties need MQTT 5.0: an MQTT 3.1.1 client refuses to publish a message that has any of them, and receives messages without them. A received message carries the properties that the broker forwarded.
-
Method Summary
Modifier and TypeMethodDescriptionReturns the content type that describes the payload, or empty when absent.Optional<byte[]> Returns a copy of the Correlation Data, or empty when absent.booleaninthashCode()Returns the lifetime of the message, or empty when it does not expire.static MqttMessageCreates a message without properties, with a copy of the application bytes.byte[]payload()Returns a copy of the application bytes.intReturns the payload byte count without copying its contents.booleanReturns whether the payload is declared as UTF-8 encoded character data.qos()Returns the requested or received quality of service.Returns the topic for a reply, or empty when absent.booleanretain()Returns whether publication requests retention, or reception is a retained delivery: one that the broker sent from its retained messages as it made the subscription.topic()Returns the published topic name.toString()Returns the User Properties in order, possibly repeating a name.withContentType(String contentType) Returns a copy with a content type (the MQTT 5.0 Content Type) that describes the payload, such as a MIME type.withCorrelationData(byte[] correlationData) Returns a copy with a copy ofcorrelationData(the MQTT 5.0 Correlation Data), at most 65535 bytes and possibly empty, which a reply can return to identify its request.withMessageExpiry(Duration messageExpiry) Returns a copy with a lifetime (the MQTT 5.0 Message Expiry Interval) from zero to 4294967295 seconds; a fraction of a second is rounded up when sent.withPayload(byte[] payload) Returns a copy with a different payload, copying the supplied bytes.withPayloadUtf8(boolean payloadUtf8) Returns a copy that declares the payload as UTF-8 encoded character data (true, the MQTT 5.0 Payload Format Indicator) or as unspecified bytes (false).Returns a copy with a different requested or received quality of service.withResponseTopic(Topic responseTopic) Returns a copy that names the topic for a reply (the MQTT 5.0 Response Topic).withRetain(boolean retain) Returns a copy with a different retention request or retained-delivery flag.Returns a copy with a different published topic.withUserProperties(List<UserProperty> userProperties) Returns a copy whose User Properties are a copy ofuserProperties, in the same order.
-
Method Details
-
of
Creates a message without properties, with a copy of the application bytes.- Parameters:
topic- the published topic namepayload- the application bytes, possibly emptyqos- the requested or received quality of serviceretain- whether publication requests retention, or reception is a retained delivery- Returns:
the message
-
withTopic
Returns a copy with a different published topic.- Parameters:
topic- the published topic name- Returns:
- a copy with a different published topic
-
withPayload
Returns a copy with a different payload, copying the supplied bytes.- Parameters:
payload- the application bytes, possibly empty- Returns:
- a copy with a different payload, copying the supplied bytes
-
withQos
Returns a copy with a different requested or received quality of service.- Parameters:
qos- the requested or received quality of service- Returns:
- a copy with a different requested or received quality of service
-
withRetain
Returns a copy with a different retention request or retained-delivery flag.- Parameters:
retain- whether publication requests retention, or reception is a retained delivery- Returns:
- a copy with a different retention request or retained-delivery flag
-
withPayloadUtf8
Returns a copy that declares the payload as UTF-8 encoded character data (true, the MQTT 5.0 Payload Format Indicator) or as unspecified bytes (false). Neither publication nor reception checks the payload against the declaration.- Parameters:
payloadUtf8- whether the payload is declared as UTF-8 encoded character data- Returns:
- a copy that declares the payload as UTF-8 encoded character data (true, the MQTT 5.0 Payload Format Indicator) or as unspecified bytes (false)
-
withMessageExpiry
Returns a copy with a lifetime (the MQTT 5.0 Message Expiry Interval) from zero to 4294967295 seconds; a fraction of a second is rounded up when sent. When the lifetime passes before the broker starts delivering the message to a subscriber, the broker discards it for that subscriber. The client sends the same lifetime however long the message waits to be sent, including a resend in a later connection.- Parameters:
messageExpiry- the lifetime of the message- Returns:
- a copy with a lifetime (the MQTT 5.0 Message Expiry Interval) from zero to 4294967295 seconds; a fraction of a second is rounded up when sent
- Throws:
IllegalArgumentException-if `messageExpiry` is negative or exceeds 4294967295 seconds
-
withContentType
Returns a copy with a content type (the MQTT 5.0 Content Type) that describes the payload, such as a MIME type. It is an MQTT UTF-8 string of at most 65535 bytes and may be empty.- Parameters:
contentType- the content type that describes the payload- Returns:
- a copy with a content type (the MQTT 5.0 Content Type) that describes the payload, such as a MIME type
- Throws:
IllegalArgumentException-if `contentType` contains U+0000 or an unpaired surrogate, or exceeds 65535 bytes in UTF-8
-
withResponseTopic
Returns a copy that names the topic for a reply (the MQTT 5.0 Response Topic).- Parameters:
responseTopic- the topic for a reply- Returns:
- a copy that names the topic for a reply (the MQTT 5.0 Response Topic)
-
withCorrelationData
Returns a copy with a copy ofcorrelationData(the MQTT 5.0 Correlation Data), at most 65535 bytes and possibly empty, which a reply can return to identify its request.- Parameters:
correlationData- the Correlation Data- Returns:
- a copy with a copy of
correlationData(the MQTT 5.0 Correlation Data), at most 65535 bytes and possibly empty, which a reply can return to identify its request - Throws:
IllegalArgumentException-if `correlationData` exceeds 65535 bytes
-
withUserProperties
Returns a copy whose User Properties are a copy ofuserProperties, in the same order. An empty list leaves the copy without User Properties.- Parameters:
userProperties- the User Properties in order- Returns:
- a copy whose User Properties are a copy of
userProperties, in the same order - Throws:
NullPointerException-if `userProperties` or one of its elements is null
-
topic
-
payload
public byte[] payload()Returns a copy of the application bytes.- Returns:
- a copy of the application bytes
-
qos
Returns the requested or received quality of service.- Returns:
- the requested or received quality of service
-
retain
public boolean retain()Returns whether publication requests retention, or reception is a retained delivery: one that the broker sent from its retained messages as it made the subscription. A subscription withSubscribe.retainAsPublished()instead receives the retain flag that the publisher set.- Returns:
- whether publication requests retention, or reception is a retained delivery: one that the broker sent from its retained messages as it made the subscription
-
payloadSize
public int payloadSize()Returns the payload byte count without copying its contents.- Returns:
- the payload byte count without copying its contents
-
payloadUtf8
public boolean payloadUtf8()Returns whether the payload is declared as UTF-8 encoded character data. False means unspecified bytes, which is also what MQTT 5.0 assumes without a Payload Format Indicator.- Returns:
- whether the payload is declared as UTF-8 encoded character data
-
messageExpiry
-
contentType
-
responseTopic
-
correlationData
Returns a copy of the Correlation Data, or empty when absent. Present Correlation Data can be an empty array.- Returns:
- a copy of the Correlation Data, or empty when absent
-
userProperties
Returns the User Properties in order, possibly repeating a name. The list cannot be modified.- Returns:
- the User Properties in order, possibly repeating a name
-
equals
-
hashCode
-
toString
-