Search in sources :

Example 1 with MqttPersistenceException

use of org.eclipse.paho.client.mqttv3.MqttPersistenceException in project ha-bridge by bwssytems.

the class MQTTHandler method publishMessage.

public void publishMessage(String topic, String content) {
    MqttMessage message = new MqttMessage(StringEscapeUtils.unescapeJava(content).getBytes());
    message.setQos(qos);
    try {
        myClient.publish(topic, message);
    } catch (MqttPersistenceException e) {
        log.error("Could not publish to MQTT client for name: " + myConfig.getName() + " and ip: " + myConfig.getIp() + " with message: " + e.getMessage());
    } catch (MqttException e) {
        log.error("Could not publish to MQTT client for name: " + myConfig.getName() + " and ip: " + myConfig.getIp() + " with message: " + e.getMessage());
    }
}
Also used : MqttMessage(org.eclipse.paho.client.mqttv3.MqttMessage) MqttException(org.eclipse.paho.client.mqttv3.MqttException) MqttPersistenceException(org.eclipse.paho.client.mqttv3.MqttPersistenceException)

Example 2 with MqttPersistenceException

use of org.eclipse.paho.client.mqttv3.MqttPersistenceException in project aware-client by denzilferreira.

the class Mqtt method publish.

/**
 * Publish message to topic
 *
 * @param topicName
 * @param messageText
 */
public static boolean publish(String topicName, String messageText) {
    if (MQTT_CLIENT != null && MQTT_CLIENT.isConnected()) {
        try {
            MqttMessage message = new MqttMessage();
            message.setPayload(messageText.getBytes());
            message.setQos(Integer.parseInt(MQTT_QoS));
            message.setRetained(true);
            MQTT_CLIENT.publish(topicName, message);
        } catch (MqttPersistenceException e) {
            if (Aware.DEBUG)
                Log.e(TAG, e.getMessage());
            return false;
        } catch (MqttException e) {
            if (Aware.DEBUG)
                Log.e(TAG, e.getMessage());
            return false;
        }
    }
    return true;
}
Also used : MqttMessage(org.eclipse.paho.client.mqttv3.MqttMessage) MqttException(org.eclipse.paho.client.mqttv3.MqttException) MqttPersistenceException(org.eclipse.paho.client.mqttv3.MqttPersistenceException)

Example 3 with MqttPersistenceException

use of org.eclipse.paho.client.mqttv3.MqttPersistenceException in project nifi by apache.

the class MqttTestClient method publish.

@Override
public void publish(String topic, byte[] payload, int qos, boolean retained) throws MqttException, MqttPersistenceException {
    MqttMessage message = new MqttMessage(payload);
    message.setQos(qos);
    message.setRetained(retained);
    switch(type) {
        case Publisher:
            publishedMessage = new MQTTQueueMessage(topic, message);
            break;
        case Subscriber:
            try {
                mqttCallback.messageArrived(topic, message);
            } catch (Exception e) {
                throw new MqttException(e);
            }
            break;
    }
}
Also used : MqttMessage(org.eclipse.paho.client.mqttv3.MqttMessage) MqttException(org.eclipse.paho.client.mqttv3.MqttException) MqttException(org.eclipse.paho.client.mqttv3.MqttException) MqttPersistenceException(org.eclipse.paho.client.mqttv3.MqttPersistenceException) MqttSecurityException(org.eclipse.paho.client.mqttv3.MqttSecurityException)

Aggregations

MqttException (org.eclipse.paho.client.mqttv3.MqttException)3 MqttMessage (org.eclipse.paho.client.mqttv3.MqttMessage)3 MqttPersistenceException (org.eclipse.paho.client.mqttv3.MqttPersistenceException)3 MqttSecurityException (org.eclipse.paho.client.mqttv3.MqttSecurityException)1