-
-
Save dirkmoors/ee37f7990701bc61e306 to your computer and use it in GitHub Desktop.
Test class for Smack thread leak
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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