Smack: XMPPConnection Management


Creating a Connection

The org.jivesoftware.smack.XMPPConnection class manages your connection to an XMPP server. The default implementation is the org.jivesoftware.smack.XMPPTCPConnection class. Two constructors are mainly used. The first, XMPPTCPConnection(String) takes the server name you'd like to connect to as an argument. All default connection settings will be used:

Connect and Disconnect

// Create the configuration for this new connection_
ConnectionConfiguration config = new ConnectionConfiguration("", 5222);

AbstractXMPPConnection connection = new XMPPTCPConnection(config);
// Connect to the server_
// Log into the server_
connection.login("username", "password", "SomeResource");


// Disconnect from the server_

By default Smack will try to reconnect the connection in case it was abruptly disconnected. Use ConnectionConfiguration#setReconnectionAllowed(boolean) to turn on/off this feature. The reconnection manager will try to immediately reconnect to the server and increase the delay between attempts as successive reconnections keep failing.

In case you want to force a reconnection while the reconnetion manager is waiting for the next reconnection, you can just use AbstractXMPPConnection#connect() and a new attempt will be made. If the manual attempt also failed then the reconnection manager will still continue the reconnection job.

Copyright (C) Jive Software 2002-2008