Auto-reconnection relies on the ReconnectionManager class being initialized (evil)

Hi everybody!

This is a bug report.

We are using a (private) fork of the asmack library provided by Flowdalic.

We discovered that the reconnection mechanism relies on the following code to be executed:

static {
        // Create a new PrivacyListManager on every established connection. In the init()
        // method of PrivacyListManager, we'll add a listener that will delete the
        // instance when the connection is closed.
        Connection.addConnectionCreationListener(new ConnectionCreationListener() {
            public void connectionCreated(Connection connection) {
                connection.addConnectionListener(new ReconnectionManager(connection));
            }
        });
    }

(org.jivesoftware.smack.ReconnectionManager:29)

However, this code only gets called if the ReconnectionManager class is (statically) initialized, which is not the case (at least in our configuration).

That code should be moved in another location, in order to make it sure that it is actually called in any case.

Our current workaround is to do the following before creating a XMPPConnection:

Class.forName(ReconnectionManager.class.getName());

(and this is horrible)

thank you!

Massimo

I wonder why the static blocks get evaluated on JRE and why it doesn’t get evaluated on Android.

I think that, if it gets evaluated on JRE, it’s because the class is referenced somewhere in some other initialization code (which is evaluated when the library is used the first time).

I don’t see any particular connection with Android.

Hey massimo,

I was wondering why reconnection wasn’t working on Smack, and I agree with you, ReconnectionManager isn’t being referenced anywhere else but by the class itself.

This definitely seems like a bug to me.

Smack preloads a bunch of classes by registering them in the smack-config.xml file, which of course executes the static block in question. Unfortunately, this setup doesn’t work in Android due to it’s restrictions on where files can be located, thus the config file is not found.

The ReconnectionManager is one of those classes, but on Android you do have to do it manually.

I don’t particularly like this setup with the heavy usage of static blocks, but it is how Smack was originally coded and it would be difficult to change this.

Oh and also note that we have ConfigureProviderManager in aSmack, which does the same and should take care of the static init blocks. But is seems that the ReconnectionManager is missing. I have never noticed that because I don’t use the ReconnectionManger. IMHO you need a finer control over the reconnections on Android.

I have create Issue 8 for aSmack to fix this.