Search in sources :

Example 1 with MqttSecurityException

use of org.eclipse.paho.client.mqttv3.MqttSecurityException in project joynr by bmwcarit.

the class MqttPahoClient method start.

@Override
public void start() {
    while (!shutdown.get() && !mqttClient.isConnected()) {
        try {
            synchronized (this) {
                logger.debug("Started MqttPahoClient");
                mqttClient.setCallback(this);
                mqttClient.setTimeToWait(timeToWaitMs);
                mqttClient.connect(getConnectOptions());
                logger.debug("MQTT Connected client");
                mqttStatusReceiver.notifyConnectionStatusChanged(MqttStatusReceiver.ConnectionStatus.CONNECTED);
                reestablishSubscriptions();
            }
        } catch (MqttException mqttError) {
            logger.error("MQTT Connect failed. Error code {}", mqttError.getReasonCode(), mqttError);
            switch(mqttError.getReasonCode()) {
                case MqttException.REASON_CODE_CLIENT_EXCEPTION:
                    if (isSecureConnection) {
                        logger.error("Failed to establish TLS connection, error: " + mqttError);
                        if (mqttError instanceof MqttSecurityException || (mqttError.getCause() != null && mqttError.getCause() instanceof SSLHandshakeException)) {
                            throw new JoynrIllegalStateException("Unable to create TLS MqttPahoClient: " + mqttError);
                        }
                    }
                case MqttException.REASON_CODE_BROKER_UNAVAILABLE:
                case MqttException.REASON_CODE_CLIENT_DISCONNECTING:
                case MqttException.REASON_CODE_CLIENT_NOT_CONNECTED:
                case MqttException.REASON_CODE_CLIENT_TIMEOUT:
                case MqttException.REASON_CODE_CONNECT_IN_PROGRESS:
                case MqttException.REASON_CODE_CONNECTION_LOST:
                case MqttException.REASON_CODE_MAX_INFLIGHT:
                case MqttException.REASON_CODE_NO_MESSAGE_IDS_AVAILABLE:
                case MqttException.REASON_CODE_SERVER_CONNECT_ERROR:
                case MqttException.REASON_CODE_SUBSCRIBE_FAILED:
                case MqttException.REASON_CODE_UNEXPECTED_ERROR:
                case MqttException.REASON_CODE_WRITE_TIMEOUT:
                    if (shutdown.get()) {
                        return;
                    }
                    synchronized (this) {
                        try {
                            this.wait(reconnectSleepMs);
                        } catch (InterruptedException e) {
                            Thread.currentThread().interrupt();
                            return;
                        }
                    }
                    continue;
                case MqttException.REASON_CODE_CLIENT_CONNECTED:
                    continue;
            }
        } catch (Exception e) {
            throw new JoynrIllegalStateException("Unable to start MqttPahoClient: " + e.getMessage(), e);
        }
    }
}
Also used : MqttException(org.eclipse.paho.client.mqttv3.MqttException) MqttSecurityException(org.eclipse.paho.client.mqttv3.MqttSecurityException) SSLHandshakeException(javax.net.ssl.SSLHandshakeException) JoynrIllegalStateException(io.joynr.exceptions.JoynrIllegalStateException) URISyntaxException(java.net.URISyntaxException) JoynrMessageNotSentException(io.joynr.exceptions.JoynrMessageNotSentException) SSLHandshakeException(javax.net.ssl.SSLHandshakeException) MqttException(org.eclipse.paho.client.mqttv3.MqttException) JoynrRuntimeException(io.joynr.exceptions.JoynrRuntimeException) JoynrDelayMessageException(io.joynr.exceptions.JoynrDelayMessageException) JoynrIllegalStateException(io.joynr.exceptions.JoynrIllegalStateException) MqttSecurityException(org.eclipse.paho.client.mqttv3.MqttSecurityException)

Example 2 with MqttSecurityException

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

the class MQTTHandler method publishMessage.

public void publishMessage(String topic, String content, Integer qos, Boolean retain) {
    MqttMessage message = new MqttMessage(StringEscapeUtils.unescapeJava(content).getBytes());
    message.setQos(Optional.ofNullable(qos).orElse(1));
    message.setRetained(Optional.ofNullable(retain).orElse(false));
    try {
        if (!myClient.isConnected()) {
            try {
                myClient.connect();
            } catch (MqttSecurityException e1) {
                log.error("Could not retry connect to MQTT client for name: " + myConfig.getName() + " and ip: " + myConfig.getIp() + " with message: " + e1.getMessage());
                return;
            } catch (MqttException e1) {
                log.error("Could not retry connect to MQTT client for name: " + myConfig.getName() + " and ip: " + myConfig.getIp() + " with message: " + e1.getMessage());
                return;
            }
        }
        myClient.publish(topic, message);
    } 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) MqttSecurityException(org.eclipse.paho.client.mqttv3.MqttSecurityException)

Aggregations

MqttException (org.eclipse.paho.client.mqttv3.MqttException)2 MqttSecurityException (org.eclipse.paho.client.mqttv3.MqttSecurityException)2 JoynrDelayMessageException (io.joynr.exceptions.JoynrDelayMessageException)1 JoynrIllegalStateException (io.joynr.exceptions.JoynrIllegalStateException)1 JoynrMessageNotSentException (io.joynr.exceptions.JoynrMessageNotSentException)1 JoynrRuntimeException (io.joynr.exceptions.JoynrRuntimeException)1 URISyntaxException (java.net.URISyntaxException)1 SSLHandshakeException (javax.net.ssl.SSLHandshakeException)1 MqttMessage (org.eclipse.paho.client.mqttv3.MqttMessage)1