/**
* Reconnect
* Only appropriate if cleanSession is false and we were connected.
* Declare as synchronized to avoid multiple calls to this method to send connect
* multiple times
*/
synchronized void reconnect() {
if (myClient ==null) {
service.traceError(TAG,"Reconnect myClient = null. Will not do reconnect");
return;
}
if (isConnecting) {
service.traceDebug(TAG, "The client is connecting. Reconnect return directly.");
return ;
}
if(!service.isOnline()){
service.traceDebug(TAG,
"The network is not reachable. Will not do reconnect");
return;
}
if(connectOptions.isAutomaticReconnect()){
//The Automatic reconnect functionality is enabled here
Log.i(TAG, "Requesting Automatic reconnect using New Java AC");
final Bundle resultBundle =new Bundle();
resultBundle.putString(
MqttServiceConstants.CALLBACK_ACTIVITY_TOKEN,
reconnectActivityToken);
resultBundle.putString(
MqttServiceConstants.CALLBACK_INVOCATION_CONTEXT, null);
resultBundle.putString(MqttServiceConstants.CALLBACK_ACTION,
MqttServiceConstants.CONNECT_ACTION);
try {
myClient.reconnect();
}catch (MqttException ex){
Log.e(TAG, "Exception occurred attempting to reconnect: " + ex.getMessage());
setConnectingState(false);
handleException(resultBundle, ex);
}
}else if (disconnected && !cleanSession) {
// use the activityToke the same with action connect
service.traceDebug(TAG,"Do Real Reconnect!");
final Bundle resultBundle =new Bundle();
resultBundle.putString(
MqttServiceConstants.CALLBACK_ACTIVITY_TOKEN,
reconnectActivityToken);
resultBundle.putString(
MqttServiceConstants.CALLBACK_INVOCATION_CONTEXT, null);
resultBundle.putString(MqttServiceConstants.CALLBACK_ACTION,
MqttServiceConstants.CONNECT_ACTION);
try {
IMqttActionListener listener =new MqttConnectionListener(resultBundle) {
@Override
public void onSuccess(IMqttToken asyncActionToken) {
// since the device's cpu can go to sleep, acquire a
// wakelock and drop it later.
service.traceDebug(TAG,"Reconnect Success!");
service.traceDebug(TAG,"DeliverBacklog when reconnect.");
doAfterConnectSuccess(resultBundle);
}
@Override
public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
resultBundle.putString(
MqttServiceConstants.CALLBACK_ERROR_MESSAGE,
exception.getLocalizedMessage());
resultBundle.putSerializable(
MqttServiceConstants.CALLBACK_EXCEPTION,
exception);
service.callbackToActivity(clientHandle, Status.ERROR,
resultBundle);
doAfterConnectFail(resultBundle);
}
};
myClient.connect(connectOptions, null, listener);
setConnectingState(true);
}catch (MqttException e) {
service.traceError(TAG, "Cannot reconnect to remote server." + e.getMessage());
setConnectingState(false);
handleException(resultBundle, e);
}catch (Exception e){
/* TODO: Added Due to: https://github.com/eclipse/paho.mqtt.android/issues/101
For some reason in a small number of cases, myClient is null here and so
a NullPointer Exception is thrown. This is a workaround to pass the exception
up to the application. myClient should not be null so more investigation is
required.
*/
service.traceError(TAG, "Cannot reconnect to remote server." + e.getMessage());
setConnectingState(false);
MqttException newEx =new MqttException(MqttException.REASON_CODE_UNEXPECTED_ERROR, e.getCause());
handleException(resultBundle, newEx);
}
}
}