Skip to content

Instantly share code, notes, and snippets.

@dirkmoors
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dirkmoors/ee37f7990701bc61e306 to your computer and use it in GitHub Desktop.
Save dirkmoors/ee37f7990701bc61e306 to your computer and use it in GitHub Desktop.
Test class for Smack thread leak
package smacktest;
import java.io.IOException;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.SmackException.NotConnectedException;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.ConnectionConfiguration.SecurityMode;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.tcp.XMPPTCPConnection;
public class SmackTest {
public static void main(String[] args) throws SmackException, IOException, XMPPException, InterruptedException{
//Connect and login
XMPPConnection connection = connect(
"service.vrijhof.com", 5222, "smacktest", "smacktest", "test_resource");
//Sleep
Thread.sleep(2000);
//Disconnect and cleanup
disconnect(connection);
/**
* At this point, the app should automatically and gracefully quit
*/
}
private static XMPPConnection connect(String host, int port, String username, String password, String resource) throws SmackException, IOException, XMPPException{
System.out.println("Connecting...");
//Configure
ConnectionConfiguration config = new ConnectionConfiguration(
host, port);
config.setCompressionEnabled(true);
config.setReconnectionAllowed(true);
config.setSecurityMode(SecurityMode.required);
//Create connection
XMPPConnection connection = new XMPPTCPConnection(config);
//Connect
connection.connect();
//Login user
connection.login(username, password, resource);
System.out.println("Connected!");
return connection;
}
private static void disconnect(XMPPConnection connection) throws NotConnectedException{
System.out.println("Disconnecting...");
connection.disconnect();
connection = null;
System.gc();
System.out.println("Disconnected!");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment