Skip to content

Commit 7db2c0f

Browse files
author
tzihan
committed
Fix bug 443365:[feature] App is killed, after mqtt service is restarted, mqtt service should reconnect to the mqtt server
Change-Id: Icd43a73cbde91e0451a905b737b7dad071c33755 Signed-off-by: tzihan <zhtang@cn.ibm.com>
1 parent 103f3eb commit 7db2c0f

2 files changed

Lines changed: 64 additions & 7 deletions

File tree

org.eclipse.paho.android.service/org.eclipse.paho.android.service/src/org/eclipse/paho/android/service/MqttConnection.java

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -234,10 +234,7 @@ public void connect(MqttConnectOptions options, String invocationContext,
234234
persistence = new MqttDefaultFilePersistence(
235235
myDir.getAbsolutePath());
236236
}
237-
238-
myClient = new MqttAsyncClient(serverURI, clientId, persistence,
239-
new AlarmPingSender(service));
240-
myClient.setCallback(this);
237+
241238
IMqttActionListener listener = new MqttConnectionListener(
242239
resultBundle) {
243240

@@ -268,10 +265,35 @@ public void onFailure(IMqttToken asyncActionToken,
268265

269266
}
270267
};
271-
service.traceDebug(TAG, "Do Real connect!");
272-
setConnectingState(true);
273-
myClient.connect(connectOptions, invocationContext, listener);
274268

269+
if (myClient != null) {
270+
if (isConnecting ) {
271+
service.traceDebug(TAG,
272+
"myClient != null and the client is connecting. Connect return directly.");
273+
service.traceDebug(TAG,"Connect return:isConnecting:"+isConnecting+".disconnected:"+disconnected);
274+
return;
275+
}else if(!disconnected){
276+
service.traceDebug(TAG,"myClient != null and the client is connected and notify!");
277+
doAfterConnectSuccess(resultBundle);
278+
}
279+
else {
280+
service.traceDebug(TAG, "myClient != null and the client is not connected");
281+
service.traceDebug(TAG,"Do Real connect!");
282+
setConnectingState(true);
283+
myClient.connect(connectOptions, invocationContext, listener);
284+
}
285+
}
286+
287+
// if myClient is null, then create a new connection
288+
else {
289+
myClient = new MqttAsyncClient(serverURI, clientId,
290+
persistence, new AlarmPingSender(service));
291+
myClient.setCallback(this);
292+
293+
service.traceDebug(TAG,"Do Real connect!");
294+
setConnectingState(true);
295+
myClient.connect(connectOptions, invocationContext, listener);
296+
}
275297
} catch (Exception e) {
276298
handleException(resultBundle, e);
277299
}

org.eclipse.paho.android.service/org.eclipse.paho.android.service/src/org/eclipse/paho/android/service/MqttService.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,18 @@ public void connect(String clientHandle, MqttConnectOptions connectOptions,
354354
void reconnect() {
355355
traceDebug(TAG, "Reconnect to server, client size=" + connections.size());
356356
for (MqttConnection client : connections.values()) {
357+
traceDebug("Reconnect Client:",
358+
client.getClientId() + '/' + client.getServerURI());
359+
// persist model connection data to retrieve persistId with
360+
// clientHandle;
361+
String[] connectionsurl = client.getServerURI().split(":");
362+
boolean issll = connectionsurl[0].startsWith("ssl") ? true : false;
363+
ModelConnectionPersistence con = ModelConnectionPersistence
364+
.createConnection(client.getClientId(),
365+
connectionsurl[1].substring(2),
366+
Integer.valueOf(connectionsurl[2]),
367+
this.getApplicationContext(), issll, null);
368+
modelConnections.put(client.getClientHandle(), con);
357369
if(this.isOnline()){
358370
client.reconnect();
359371
}
@@ -623,6 +635,29 @@ public void onCreate() {
623635
//create database persistence instance for connections;
624636
persistence = new DatabaseConnectionPersistence(this);
625637

638+
try {
639+
List<ModelConnectionPersistence> l = persistence
640+
.restoreConnections(this.getApplicationContext());
641+
for (ModelConnectionPersistence c : l) {
642+
String handle = getClient(c.getServerURI(), c.getId(),
643+
c.getCtxId(), null);
644+
connections.get(handle).setConnectOptions(
645+
c.getConnectionOptions());
646+
if (isOnline()) {
647+
// we have an internet connection - have another try
648+
// at
649+
// connecting
650+
connect(handle, c.getConnectionOptions(), null, "0");
651+
}
652+
}
653+
654+
} catch (ConnectionPersistenceException e) {
655+
Log.e("MqttService", e.toString());
656+
} catch (MqttSecurityException e) {
657+
Log.e("MqttService", e.toString());
658+
} catch (MqttException e) {
659+
Log.e("MqttService", e.toString());
660+
}
626661
}
627662

628663
/**

0 commit comments

Comments
 (0)