use of org.eclipse.paho.client.mqttv3.MqttException in project azure-iot-sdk-java by Azure.
the class MqttTest method connectionLostAttemptsToReconnectAgainIfConnectFails.
/*
**Tests_SRS_Mqtt_25_027: [**The function shall attempt to reconnect to the IoTHub in a loop with exponential backoff until it succeeds**]**
*/
@Test
public void connectionLostAttemptsToReconnectAgainIfConnectFails() 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 = mockMqttException;
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);
}
}
use of org.eclipse.paho.client.mqttv3.MqttException in project Tusky by Vavassor.
the class PushNotificationClient method connect.
/** Connect to the MQTT broker. */
public void connect(Context context) {
MqttConnectOptions options = new MqttConnectOptions();
options.setAutomaticReconnect(true);
options.setCleanSession(false);
try {
String password = context.getString(R.string.tusky_api_keystore_password);
InputStream keystore = context.getResources().openRawResource(R.raw.keystore_tusky_api);
try {
options.setSocketFactory(mqttAndroidClient.getSSLSocketFactory(keystore, password));
} finally {
IOUtils.closeQuietly(keystore);
}
mqttAndroidClient.connect(options).setActionCallback(new IMqttActionListener() {
@Override
public void onSuccess(IMqttToken asyncActionToken) {
DisconnectedBufferOptions bufferOptions = new DisconnectedBufferOptions();
bufferOptions.setBufferEnabled(true);
bufferOptions.setBufferSize(100);
bufferOptions.setPersistBuffer(false);
bufferOptions.setDeleteOldestMessages(false);
mqttAndroidClient.setBufferOpts(bufferOptions);
onConnectionSuccess();
flushQueuedActions();
}
@Override
public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
Log.e(TAG, "An exception occurred while connecting. " + exception.getMessage() + " " + exception.getCause());
onConnectionFailure();
}
});
} catch (MqttException e) {
Log.e(TAG, "An exception occurred while connecpting. " + e.getMessage());
onConnectionFailure();
}
}
Aggregations