Class MqttMessage
- java.lang.Object
-
- com.plccom.mqtt.client.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 Summary
Constructors Constructor Description MqttMessage(PlccomMqttPublishMessage message)Creates a high-level message view.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description StringcontentType()Returns the MQTT 5 Content Type property.byte[]correlationData()Returns the MQTT 5 Correlation Data property.LongmessageExpirySeconds()Returns the MQTT 5 Message Expiry Interval property.booleanpayloadAsBoolean()Returns the payload parsed as a boolean.byte[]payloadAsBytes()Returns the payload bytes.doublepayloadAsDouble()Returns the payload parsed as a double.intpayloadAsInt()Returns the payload parsed as an integer.longpayloadAsLong()Returns the payload parsed as a long.StringpayloadAsString()Returns the payload decoded as UTF-8 text.BooleanpayloadFormatUtf8()Returns the MQTT 5 Payload Format Indicator property.PlccomMqttQualityOfServiceqos()Returns the message QoS.StringresponseTopic()Returns the MQTT 5 Response Topic property.booleanretain()Returns whether the message carried the MQTT Retain flag.List<Integer>subscriptionIdentifiers()Returns MQTT 5 Subscription Identifiers carried by this message.Stringtopic()Returns the message topic.PlccomMqttPublishMessagetoPublishMessage()Returns the prepared publish message behind this high-level view.List<PlccomMqttUserProperty>userProperties()Returns the MQTT 5 User Properties.
-
-
-
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- ifmessageisnull
-
-
Method Detail
-
topic
public String topic()
Returns the message topic.- Returns:
- MQTT topic name
-
qos
public PlccomMqttQualityOfService qos()
Returns the message QoS.- Returns:
- message QoS
-
retain
public boolean retain()
Returns whether the message carried the MQTT Retain flag.- Returns:
truewhen the message was retained by the broker
-
payloadFormatUtf8
public Boolean payloadFormatUtf8()
Returns the MQTT 5 Payload Format Indicator property.Boolean.TRUEmeans the sender declared the payload as UTF-8 character data.Boolean.FALSEmeans the sender declared the payload as unspecified binary data.nullmeans the message did not carry the property.- Returns:
- payload format hint, or
nullwhen omitted
-
contentType
public String contentType()
Returns the MQTT 5 Content Type property.- Returns:
- content type, or
nullwhen 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
nullwhen the message did not carry the property
-
responseTopic
public String responseTopic()
Returns the MQTT 5 Response Topic property.- Returns:
- response topic, or
nullwhen 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
nullwhen 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 neithertruenorfalse, ignoring case and surrounding whitespace
-
toPublishMessage
public PlccomMqttPublishMessage toPublishMessage()
Returns the prepared publish message behind this high-level view.- Returns:
- prepared publish message
-
-