net.sf.xenqtt.client
Class PublishMessage

java.lang.Object
  extended by net.sf.xenqtt.client.PublishMessage

public class PublishMessage
extends Object

Message published either by the client to a topic or published to the client from subscribed topic. Users may extend this class to add user defined data. This can be especially useful when using the AsyncMqttClient.


Constructor Summary
PublishMessage(String topicName, QoS qos, byte[] payload)
          Creates a binary message with retain set to false.
PublishMessage(String topicName, QoS qos, byte[] payload, boolean retain)
          Creates a binary message.
PublishMessage(String topicName, QoS qos, String payload)
          Creates a message with a string as the payload with retain set to false.
PublishMessage(String topicName, QoS qos, String payload, boolean retain)
          Creates a message with a string as the payload.
 
Method Summary
 void ack()
          Sends an acknowledgment to the broker for this message unless getQoS() is QoS.AT_MOST_ONCE in which case this does nothing.
 byte[] getPayload()
           
 String getPayloadString()
           
 QoS getQoS()
           
 long getReceivedTimestamp()
           
 String getTopic()
           
 boolean isDuplicate()
           
 boolean isRetain()
          If the Retain flag is set (1), the server should hold on to the message after it has been delivered to the current subscribers.
 String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

PublishMessage

public PublishMessage(String topicName,
                      QoS qos,
                      byte[] payload,
                      boolean retain)
Creates a binary message.

Parameters:
topicName - The name of the topic to publish to. This may not contain wildcards ('+' and '#')
qos - The level of assurance for delivery.
payload - The payload as a byte array. It is valid to publish a zero length payload.
retain - If the Retain flag is set (1), the broker should hold on to the message after it has been delivered to the current subscribers. This is useful where publishers send messages on a "report by exception" basis, where it might be some time between messages. This allows new subscribers to instantly receive data with the retained, or Last Known Good, value. A broker may delete a retained message if it receives a message with a zero-length payload and the Retain flag set on the same topic.

PublishMessage

public PublishMessage(String topicName,
                      QoS qos,
                      byte[] payload)
Creates a binary message with retain set to false. Delegates to #publish(String, QoS, byte[], boolean).

See Also:
PublishMessage(String, QoS, byte[], boolean)

PublishMessage

public PublishMessage(String topicName,
                      QoS qos,
                      String payload)
Creates a message with a string as the payload with retain set to false. The string is converted to a byte[] using UTF8 encoding and used as the binary message payload. Delegates to PublishMessage(String, QoS, byte[], boolean).

See Also:
PublishMessage(String, QoS, byte[], boolean)

PublishMessage

public PublishMessage(String topicName,
                      QoS qos,
                      String payload,
                      boolean retain)
Creates a message with a string as the payload. The string is converted to a byte[] using UTF8 encoding and used as the binary message payload. Delegates to PublishMessage(String, QoS, byte[], boolean).

See Also:
PublishMessage(String, QoS, byte[], boolean)
Method Detail

getReceivedTimestamp

public long getReceivedTimestamp()
Returns:
Timestamp (from System.currentTimeMillis()) when the message was received. 0 if this is not a received message.

getTopic

public final String getTopic()
Returns:
The topic the message was published to. When received by a client that subscribed using wildcard characters, this string will be the absolute topic specified by the originating publisher and not the subscription string used by the client. This will never contain wildcards.

getPayload

public final byte[] getPayload()
Returns:
The message's payload as a byte[]

getPayloadString

public final String getPayloadString()
Returns:
The message's payload as a string. The payload is converted to a string using the UTF8 character set.

isRetain

public final boolean isRetain()
If the Retain flag is set (1), the server should hold on to the message after it has been delivered to the current subscribers.

When a new subscription is established on a topic, the last retained message on that topic should be sent to the subscriber with the Retain flag set. If there is no retained message, nothing is sent

This is useful where publishers send messages on a "report by exception" basis, where it might be some time between messages. This allows new subscribers to instantly receive data with the retained, or Last Known Good, value.

When a server sends a PUBLISH to a client as a result of a subscription that already existed when the original PUBLISH arrived, the Retain flag should not be set, regardless of the Retain flag of the original PUBLISH. This allows a client to distinguish messages that are being received because they were retained and those that are being received "live".

Retained messages should be kept over restarts of the server.

A server may delete a retained message if it receives a message with a zero-length payload and the Retain flag set on the same topic.


isDuplicate

public final boolean isDuplicate()
Returns:
True if the broker is re-delivering the message and the message's QoS is not QoS.AT_MOST_ONCE. The recipient should treat this flag as a hint as to whether the message may have been previously received. It should not be relied on to detect duplicates.

getQoS

public final QoS getQoS()
Returns:
The level of assurance for delivery

ack

public final void ack()
Sends an acknowledgment to the broker for this message unless getQoS() is QoS.AT_MOST_ONCE in which case this does nothing. This method is always asynchronous. If called on an instance created by the client to send to the broker this method does nothing.


toString

public String toString()
Overrides:
toString in class Object
See Also:
Object.toString()


Copyright © 2013. All Rights Reserved.