MqttConnection Class

  • java.lang.Object
    • MqttCallback
      • com.microsoft.azure.sdk.iot.provisioning.device.transport.mqtt.MqttConnection

public class MqttConnection

Constructor Summary

Constructor Description
MqttConnection(String hostname, String clientId, String userName, String password, SSLContext sslContext, MqttListener listener, boolean useWebSockets)

Constructor to create MqttAsync Client with Paho

Method Summary

Modifier and Type Method and Description
synchronized void connect()

Connects to the MQTT broker

synchronized void connectionLost(Throwable throwable)

Event fired when the connection is lost on the MQTT broker

synchronized void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken)

Event fired when the message arrived on the MQTT broker.

synchronized void disconnect()

Disconnects from the MQTT broker

String getConnectionId()
String getHostName()
boolean isMqttConnected()

Return whether the MQTT broker is connected to the endpoint

synchronized void messageArrived(String topic, org.eclipse.paho.client.mqttv3.MqttMessage mqttMessage)

Event fired when the message arrived on the MQTT broker.

synchronized void publishMessage(MqttMessage message)

Sends a PUBLISH message to the MQTT broker

synchronized void publishMessage(String topic, MqttQos qos, byte[] message)

Sends a PUBLISH message to the MQTT broker

synchronized void subscribe(String topic, MqttQos qos)

Send the SUBSCRIBE message to the MQTT broker

synchronized void unsubscribe(String topic)

Send the UNSUBSCRIBE message to the MQTT broker

Constructor Details

MqttConnection

public MqttConnection(String hostname, String clientId, String userName, String password, SSLContext sslContext, MqttListener listener, boolean useWebSockets)

Constructor to create MqttAsync Client with Paho

Parameters:

hostname - Uri to connect to
clientId - Client Id to connect to
userName - Username
password - password
sslContext - SSLContext for the connection
listener - Mqtt listener
useWebSockets - true to use Mqtt over web sockets

Throws:

IOException - is thrown if any of the parameters are null or empty or client cannot be instantiated

Method Details

connect

public synchronized void connect()

Connects to the MQTT broker

Throws:

IOException - if there is a Mqtt exception.

connectionLost

public synchronized void connectionLost(Throwable throwable)

Event fired when the connection is lost on the MQTT broker

Parameters:

throwable - the disconnection reason.

deliveryComplete

public synchronized void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken)

Event fired when the message arrived on the MQTT broker.

Parameters:

iMqttDeliveryToken - the MqttDeliveryToken for which the message was successfully sent.

disconnect

public synchronized void disconnect()

Disconnects from the MQTT broker

Throws:

IOException - if there is a Mqtt exception.

getConnectionId

public String getConnectionId()

getHostName

public String getHostName()

isMqttConnected

public boolean isMqttConnected()

Return whether the MQTT broker is connected to the endpoint

Returns:

true if connected using Mqtt

messageArrived

public synchronized void messageArrived(String topic, org.eclipse.paho.client.mqttv3.MqttMessage mqttMessage)

Event fired when the message arrived on the MQTT broker.

Parameters:

topic - the topic on which message arrived.
mqttMessage - the message arrived on the Mqtt broker.

publishMessage

public synchronized void publishMessage(MqttMessage message)

Sends a PUBLISH message to the MQTT broker

Parameters:

message - The message to be sent

Throws:

IOException - if there is a Mqtt exception.

publishMessage

public synchronized void publishMessage(String topic, MqttQos qos, byte[] message)

Sends a PUBLISH message to the MQTT broker

Parameters:

topic - The topic of the message
qos - The QOS of the message
message - The message to be sent

Throws:

IOException - if there is a Mqtt exception.

subscribe

public synchronized void subscribe(String topic, MqttQos qos)

Send the SUBSCRIBE message to the MQTT broker

Parameters:

topic - The topic of the message
qos - The QOS of the message

Throws:

IOException - if there is a Mqtt exception.

unsubscribe

public synchronized void unsubscribe(String topic)

Send the UNSUBSCRIBE message to the MQTT broker

Parameters:

topic - Name of the Topic to unsubscribe.

Throws:

IOException - if there is a Mqtt exception.

Applies to