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);
}
}
}
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());
}
}
Aggregations