use of org.eclipse.paho.client.mqttv3.MqttException in project openhab1-addons by openhab.
the class MqttBrokerConnection method connectionLost.
@Override
public synchronized void connectionLost(Throwable t) {
logger.error("MQTT connection to broker was lost", t);
if (t instanceof MqttException) {
MqttException e = (MqttException) t;
logger.error("MQTT connection to '{}' was lost: {} : ReasonCode {} : Cause : {}", new Object[] { name, e.getMessage(), e.getReasonCode(), (e.getCause() == null ? "Unknown" : e.getCause().getMessage()) });
} else {
logger.error("MQTT connection to '{}' was lost: {}", name, t.getMessage());
}
started = false;
logger.info("Starting connection helper to periodically try restore connection to broker '{}'", name);
MqttBrokerConnectionHelper helper = new MqttBrokerConnectionHelper(this);
reconnectTimer = new Timer(true);
reconnectTimer.schedule(helper, 10000, RECONNECT_FREQUENCY);
}
use of org.eclipse.paho.client.mqttv3.MqttException in project ignite by apache.
the class IgniteMqttStreamerTest method sendMessages.
/**
* @param topics Topics.
* @param fromIdx From index.
* @param cnt Count.
* @param singleMsg Single message flag.
* @throws MqttException If failed.
*/
private void sendMessages(final List<String> topics, int fromIdx, int cnt, boolean singleMsg) throws MqttException {
if (singleMsg) {
final List<StringBuilder> sbs = new ArrayList<>(topics.size());
// initialize String Builders for each topic
F.forEach(topics, new IgniteInClosure<String>() {
@Override
public void apply(String s) {
sbs.add(new StringBuilder());
}
});
// fill String Builders for each topic
F.forEach(F.range(fromIdx, fromIdx + cnt), new IgniteInClosure<Integer>() {
@Override
public void apply(Integer integer) {
sbs.get(integer % topics.size()).append(integer.toString() + "," + TEST_DATA.get(integer) + "\n");
}
});
// send each buffer out
for (int i = 0; i < topics.size(); i++) {
MqttMessage msg = new MqttMessage(sbs.get(i).toString().getBytes());
client.publish(topics.get(i % topics.size()), msg);
}
} else {
for (int i = fromIdx; i < fromIdx + cnt; i++) {
byte[] payload = (i + "," + TEST_DATA.get(i)).getBytes();
MqttMessage msg = new MqttMessage(payload);
client.publish(topics.get(i % topics.size()), msg);
}
}
}
use of org.eclipse.paho.client.mqttv3.MqttException 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());
}
}
use of org.eclipse.paho.client.mqttv3.MqttException in project azure-iot-sdk-java by Azure.
the class MqttTest method constructorThrowsExceptionIfMqttAsyncClientFails.
/*
**Tests_SRS_Mqtt_25_045: [**The constructor throws IOException if MqttException is thrown and doesn't instantiate this instance.**]**
*/
@Test(expected = IOException.class)
public void constructorThrowsExceptionIfMqttAsyncClientFails() throws IOException, MqttException {
Mqtt mockMqtt = null;
try {
//arrange
new NonStrictExpectations() {
{
new MemoryPersistence();
result = mockMemoryPersistence;
new MqttAsyncClient(serverUri, clientId, mockMemoryPersistence);
result = mockMqttException;
}
};
//act
mockMqtt = instantiateMqtt(true);
} finally {
testCleanUp(mockMqtt);
}
}
use of org.eclipse.paho.client.mqttv3.MqttException in project azure-iot-sdk-java by Azure.
the class MqttTest method connectionLostAttemptsToReconnect.
/*
**Tests_SRS_Mqtt_25_026: [**The function shall notify all its concrete classes by calling abstract method onReconnect at the entry of the function**]**
*/
/*
**Tests_SRS_Mqtt_25_029: [**The function shall notify all its concrete classes by calling abstract method onReconnectComplete at the exit of the function**]**
*/
@Test
public void connectionLostAttemptsToReconnect() throws IOException, MqttException {
//arrange
Mqtt mockMqtt = null;
Throwable t = new Throwable();
try {
new StrictExpectations() {
{
new MemoryPersistence();
result = mockMemoryPersistence;
new MqttAsyncClient(serverUri, clientId, mockMemoryPersistence);
result = mockMqttAsyncClient;
mockMqttAsyncClient.setCallback((Mqtt) any);
new MqttConnectOptions();
result = mockMqttConnectionOptions;
mockMqttConnectionOptions.setKeepAliveInterval(anyInt);
mockMqttConnectionOptions.setCleanSession(anyBoolean);
mockMqttConnectionOptions.setMqttVersion(anyInt);
mockMqttConnectionOptions.setUserName(anyString);
mockMqttConnectionOptions.setPassword(password.toCharArray());
mockMqttConnectionOptions.setSocketFactory(mockIotHubSSLContext.getIotHubSSlContext().getSocketFactory());
mockMqttAsyncClient.isConnected();
result = false;
mockMqttAsyncClient.isConnected();
result = false;
mockMqttAsyncClient.connect(mockMqttConnectionOptions);
result = mockMqttToken;
mockMqttToken.waitForCompletion();
mockMqttAsyncClient.isConnected();
result = true;
}
};
//act
try {
mockMqtt = instantiateMqtt(true);
mockMqtt.connectionLost(t);
} catch (Exception e) {
System.out.print("Completed throwing exception - " + e.getCause() + e.getMessage());
}
} finally {
testCleanUp(mockMqtt);
}
}
Aggregations