src/main/java/org/jivesoftware/smack/ReconnectionManager.java
synchronized protected void reconnect() {
        if (this.isReconnectionAllowed()) {
            // Since there is no thread running, creates a new one to attempt
            // the reconnection.
            // avoid to run duplicated reconnectionThread -- fd: 16/09/2010
            if (reconnectionThread != null && reconnectionThread.isAlive())
                return;

            reconnectionThread = new Thread() {

                /**
                 * Holds the current number of reconnection attempts
                 */
                private int attempts = 0;

                /**
                 * Returns the number of seconds until the next reconnection
                 * attempt.
                 * 
                 * @return the number of seconds until the next reconnection
                 *         attempt.
                 */
                private int timeDelay() {
                    attempts++;
                    if (attempts > 13) {
                        return randomBase * 6 * 5; // between 2.5 and 7.5
                                                   // minutes (~5 minutes)
                    }
                    if (attempts > 7) {
                        return randomBase * 6; // between 30 and 90 seconds (~1
                                               // minutes)
                    }
                    return randomBase; // 10 seconds
                }

                /**
                 * The process will try the reconnection until the connection
                 * succeed or the user cancell it
                 */
                public void run() {
                    // The process will try to reconnect until the connection is
                    // established or
                    // the user cancel the reconnection process {@link
                    // Connection#disconnect()}
                    while (ReconnectionManager.this.isReconnectionAllowed()) {
                        // Find how much time we should wait until the next
                        // reconnection
                        int remainingSeconds = timeDelay();
                        // Sleep until we're ready for the next reconnection
                        // attempt. Notify
                        // listeners once per second about how much time remains
                        // before the next
                        // reconnection attempt.
                        while (ReconnectionManager.this.isReconnectionAllowed()
                                && remainingSeconds > 0) {
                            try {
                                Thread.sleep(1000);
                                remainingSeconds--;
                                ReconnectionManager.this
                                        .notifyAttemptToReconnectIn(remainingSeconds);
                            } catch (InterruptedException e1) {
                                log.warn("Sleeping thread interrupted");
                                // Notify the reconnection has failed
                                ReconnectionManager.this
                                        .notifyReconnectionFailed(e1);
                            }
                        }

                        // Makes a reconnection attempt
                        try {
                            if (ReconnectionManager.this
                                    .isReconnectionAllowed()) {
                                connection.connect();
                            }
                        } catch (XMPPException e) {
                            // Fires the failed reconnection notification
                            ReconnectionManager.this
                                    .notifyReconnectionFailed(e);
                        }
                    }
                }
            };
            reconnectionThread.setName("Smack Reconnection Manager");
            reconnectionThread.setDaemon(true);
            reconnectionThread.start();
        }
    }
synchronized protected void reconnect() {
        if (this.isReconnectionAllowed()) {
            // Since there is no thread running, creates a new one to attempt
            // the reconnection.
            // avoid to run duplicated reconnectionThread -- fd: 16/09/2010
            if (reconnectionThread != null && reconnectionThread.isAlive())
                return;

            reconnectionThread = new Thread() {

                /**
                 * Holds the current number of reconnection attempts
                 */
                private int attempts = 0;

                /**
                 * Returns the number of seconds until the next reconnection
                 * attempt.
                 * 
                 * @return the number of seconds until the next reconnection
                 *         attempt.
                 */
                private int timeDelay() {
                    attempts++;
                    if (attempts > 13) {
                        return randomBase * 6 * 5; // between 2.5 and 7.5
                                                   // minutes (~5 minutes)
                    }
                    if (attempts > 7) {
                        return randomBase * 6; // between 30 and 90 seconds (~1
                                               // minutes)
                    }
                    return randomBase; // 10 seconds
                }

                /**
                 * The process will try the reconnection until the connection
                 * succeed or the user cancell it
                 */
                public void run() {
                    // The process will try to reconnect until the connection is
                    // established or
                    // the user cancel the reconnection process {@link
                    // Connection#disconnect()}
                    while (ReconnectionManager.this.isReconnectionAllowed()) {
                        // Find how much time we should wait until the next
                        // reconnection
                        int remainingSeconds = timeDelay();
                        // Sleep until we're ready for the next reconnection
                        // attempt. Notify
                        // listeners once per second about how much time remains
                        // before the next
                        // reconnection attempt.
                        while (ReconnectionManager.this.isReconnectionAllowed()
                                && remainingSeconds > 0) {
                            try {
                                Thread.sleep(1000);
                                remainingSeconds--;
                                ReconnectionManager.this
                                        .notifyAttemptToReconnectIn(remainingSeconds);
                            } catch (InterruptedException e1) {
                                log.warn("Sleeping thread interrupted");
                                // Notify the reconnection has failed
                                ReconnectionManager.this
                                        .notifyReconnectionFailed(e1);
                            }
                        }

                        // Makes a reconnection attempt
                        try {
                            if (ReconnectionManager.this
                                    .isReconnectionAllowed()) {
                                connection.connect();
                            }
                        } catch (XMPPException e) {
                            // Fires the failed reconnection notification
                            ReconnectionManager.this
                                    .notifyReconnectionFailed(e);
                        }
                    }
                }
            };
            reconnectionThread.setName("Smack Reconnection Manager");
            reconnectionThread.setDaemon(true);
            reconnectionThread.start();
        }
    }
Starts a reconnection mechanism if it was configured to do that. The
algorithm is been executed when the first connection error is detected.
<p/>
The reconnection mechanism will try to reconnect periodically in this
way:
<ol>
<li>First it will try 6 times every 10 seconds.
<li>Then it will try 10 times every 1 minute.
<li>Finally it will try indefinitely every 5 minutes.
</ol>