Search in sources :

Example 6 with MqttException

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);
    }
}
Also used : Mqtt(com.microsoft.azure.sdk.iot.device.transport.mqtt.Mqtt) MemoryPersistence(org.eclipse.paho.client.mqttv3.persist.MemoryPersistence) IOException(java.io.IOException) InvalidParameterException(java.security.InvalidParameterException) Test(org.junit.Test)

Example 7 with MqttException

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();
    }
}
Also used : IMqttActionListener(org.eclipse.paho.client.mqttv3.IMqttActionListener) MqttConnectOptions(org.eclipse.paho.client.mqttv3.MqttConnectOptions) InputStream(java.io.InputStream) MqttException(org.eclipse.paho.client.mqttv3.MqttException) DisconnectedBufferOptions(org.eclipse.paho.client.mqttv3.DisconnectedBufferOptions) IMqttToken(org.eclipse.paho.client.mqttv3.IMqttToken)

Aggregations

Mqtt (com.microsoft.azure.sdk.iot.device.transport.mqtt.Mqtt)3 MqttException (org.eclipse.paho.client.mqttv3.MqttException)3 MemoryPersistence (org.eclipse.paho.client.mqttv3.persist.MemoryPersistence)3 Test (org.junit.Test)3 IOException (java.io.IOException)2 InvalidParameterException (java.security.InvalidParameterException)2 MqttMessage (org.eclipse.paho.client.mqttv3.MqttMessage)2 InputStream (java.io.InputStream)1 ArrayList (java.util.ArrayList)1 Timer (java.util.Timer)1 DisconnectedBufferOptions (org.eclipse.paho.client.mqttv3.DisconnectedBufferOptions)1 IMqttActionListener (org.eclipse.paho.client.mqttv3.IMqttActionListener)1 IMqttToken (org.eclipse.paho.client.mqttv3.IMqttToken)1 MqttConnectOptions (org.eclipse.paho.client.mqttv3.MqttConnectOptions)1 MqttPersistenceException (org.eclipse.paho.client.mqttv3.MqttPersistenceException)1