AnsweredAssumed Answered

Smack connection shutdown in Android

Question asked by Robin on Apr 16, 2016

Hello everyone!

 

I'm using Smack 4.1.7 in my Android app, and I can't figure out, how to properly shutdown an XMPPTCPConnection when exiting (pressing back from my main activity). Doing conn.disconnect() closes Smack Packet Writer and Smack Packet Reader daemon threads, but Smack-Ping, Smack-Incoming Processor, Smack-Single Threaded Executor and Smack-Remove Callbacks daemon threads remain running. When I launch my app again and establish the connection, another pack of these daemon threads is created, and so on.

 

Trying to debug, I ended up with this test code in my IntentService:

 

      try

      {

         XMPPTCPConnection conn = new XMPPTCPConnection(

            XMPPTCPConnectionConfiguration.builder()

            .setHost("gcm-xmpp.googleapis.com") //$NON-NLS-1$

            .setPort(5235)

            .setUsernameAndPassword(

               "project #", //$NON-NLS-1$

               "api key") //$NON-NLS-1$

            .setSocketFactory(SSLSocketFactory.getDefault())

            .setServiceName("gcm.googleapis.com") //$NON-NLS-1$

            .setDebuggerEnabled(true)

            .build());

      

         conn.connect();

         conn.login();

         conn.disconnect();      

         conn = null;

      }

   

      catch (Exception e)

      {

         // Something.

      }

 

As far as I understand, conn is eligible for finalization, but finalize() is not invoked. For now, I use the solution, given in Not closing smack threads by disconnect (4.1.0-alpha1-SNAPSHOT), but I think there should be a better way.

 

So, what's the proper way of closing a connection?

 

Any help would be greatly appreciated.

Outcomes