1
0
Fork 0
mirror of https://codeberg.org/Mercury-IM/Smack synced 2024-11-22 06:12:05 +01:00

Improve ReconnectionManager

Fixes SMACK-778.
This commit is contained in:
Florian Schmaus 2017-09-29 15:19:10 +02:00
parent 1d943aed20
commit 941f29e928

View file

@ -43,7 +43,10 @@ import org.jivesoftware.smack.util.Async;
* </ol> * </ol>
* *
* {@link ReconnectionPolicy#FIXED_DELAY} - The reconnection mechanism will try to reconnect after a fixed delay * {@link ReconnectionPolicy#FIXED_DELAY} - The reconnection mechanism will try to reconnect after a fixed delay
* independently from the number of reconnection attempts already performed * independently from the number of reconnection attempts already performed.
* <p>
* Interrupting the reconnection thread will abort the reconnection mechanism.
* </p>
* *
* @author Francisco Vives * @author Francisco Vives
* @author Luca Stucchi * @author Luca Stucchi
@ -163,7 +166,7 @@ public final class ReconnectionManager {
private ReconnectionManager(AbstractXMPPConnection connection) { private ReconnectionManager(AbstractXMPPConnection connection) {
weakRefConnection = new WeakReference<AbstractXMPPConnection>(connection); weakRefConnection = new WeakReference<AbstractXMPPConnection>(connection);
reconnectionRunnable = new Thread() { reconnectionRunnable = new Runnable() {
/** /**
* Holds the current number of reconnection attempts * Holds the current number of reconnection attempts
@ -211,6 +214,10 @@ public final class ReconnectionManager {
if (connection == null) { if (connection == null) {
return; return;
} }
// Reset attempts to zero since a new reconnection cycle is started once this runs.
attempts = 0;
// The process will try to reconnect until the connection is established or // The process will try to reconnect until the connection is established or
// the user cancel the reconnection process AbstractXMPPConnection.disconnect(). // the user cancel the reconnection process AbstractXMPPConnection.disconnect().
while (isReconnectionPossible(connection)) { while (isReconnectionPossible(connection)) {
@ -219,7 +226,10 @@ public final class ReconnectionManager {
// Sleep until we're ready for the next reconnection attempt. Notify // Sleep until we're ready for the next reconnection attempt. Notify
// listeners once per second about how much time remains before the next // listeners once per second about how much time remains before the next
// reconnection attempt. // reconnection attempt.
while (isReconnectionPossible(connection) && remainingSeconds > 0) { while (remainingSeconds > 0) {
if (!isReconnectionPossible(connection)) {
return;
}
try { try {
Thread.sleep(1000); Thread.sleep(1000);
remainingSeconds--; remainingSeconds--;
@ -228,8 +238,9 @@ public final class ReconnectionManager {
} }
} }
catch (InterruptedException e) { catch (InterruptedException e) {
LOGGER.log(Level.FINE, "waiting for reconnection interrupted", e); LOGGER.log(Level.FINE, "Reconnection Thread was interrupted, aborting reconnection mechanism", e);
break; // Exit the reconnection thread in case it was interrupted.
return;
} }
} }
@ -237,24 +248,18 @@ public final class ReconnectionManager {
listener.reconnectingIn(0); listener.reconnectingIn(0);
} }
if (!isReconnectionPossible(connection)) {
return;
}
// Makes a reconnection attempt // Makes a reconnection attempt
try { try {
if (isReconnectionPossible(connection)) { try {
try { connection.connect();
connection.connect();
} catch (SmackException.AlreadyConnectedException e) {
LOGGER.log(Level.FINER, "Connection was already connected on reconnection attempt", e);
}
} }
// TODO Starting with Smack 4.2, connect() will no catch (SmackException.AlreadyConnectedException e) {
// longer login automatically. So change this and the LOGGER.log(Level.FINER, "Connection was already connected on reconnection attempt", e);
// previous lines to connection.connect().login() in the
// 4.2, or any later, branch.
if (!connection.isAuthenticated()) {
connection.login();
} }
// Successfully reconnected. connection.login();
attempts = 0;
} }
catch (SmackException.AlreadyLoggedInException e) { catch (SmackException.AlreadyLoggedInException e) {
// This can happen if another thread concurrently triggers a reconnection // This can happen if another thread concurrently triggers a reconnection
@ -262,12 +267,21 @@ public final class ReconnectionManager {
// failure. See also SMACK-725. // failure. See also SMACK-725.
LOGGER.log(Level.FINER, "Reconnection not required, was already logged in", e); LOGGER.log(Level.FINER, "Reconnection not required, was already logged in", e);
} }
catch (SmackException | IOException | XMPPException | InterruptedException e) { catch (SmackException | IOException | XMPPException e) {
// Fires the failed reconnection notification // Fires the failed reconnection notification
for (ConnectionListener listener : connection.connectionListeners) { for (ConnectionListener listener : connection.connectionListeners) {
listener.reconnectionFailed(e); listener.reconnectionFailed(e);
} }
// Failed to reconnect, try again.
continue;
} catch (InterruptedException e) {
LOGGER.log(Level.FINE, "Reconnection Thread was interrupted, aborting reconnection mechanism", e);
// Exit the reconnection thread in case it was interrupted.
return;
} }
// Successfully reconnected .
return;
} }
} }
}; };
@ -314,7 +328,7 @@ public final class ReconnectionManager {
* *
* @return true, if the reconnection mechanism is enabled. * @return true, if the reconnection mechanism is enabled.
*/ */
public boolean isAutomaticReconnectEnabled() { public synchronized boolean isAutomaticReconnectEnabled() {
return automaticReconnectEnabled; return automaticReconnectEnabled;
} }
@ -348,6 +362,20 @@ public final class ReconnectionManager {
"Smack Reconnection Manager (" + connection.getConnectionCounter() + ')'); "Smack Reconnection Manager (" + connection.getConnectionCounter() + ')');
} }
/**
* Abort a possibly running reconnection mechanism.
*
* @since 4.2.2
*/
public synchronized void abortPossiblyRunningReconnection() {
if (reconnectionThread == null) {
return;
}
reconnectionThread.interrupt();
reconnectionThread = null;
}
private final ConnectionListener connectionListener = new AbstractConnectionListener() { private final ConnectionListener connectionListener = new AbstractConnectionListener() {
@Override @Override