|
| 1 | +package org.igniterealtime.smack.examples; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | + |
| 5 | +import org.jivesoftware.smack.ConnectionListener; |
| 6 | +import org.jivesoftware.smack.ReconnectionListener; |
| 7 | +import org.jivesoftware.smack.ReconnectionManager; |
| 8 | +import org.jivesoftware.smack.ReconnectionManager.ReconnectionPolicy; |
| 9 | +import org.jivesoftware.smack.SmackException; |
| 10 | +import org.jivesoftware.smack.XMPPConnection; |
| 11 | +import org.jivesoftware.smack.XMPPException; |
| 12 | +import org.jivesoftware.smack.debugger.ConsoleDebugger; |
| 13 | +import org.jivesoftware.smack.tcp.XMPPTCPConnection; |
| 14 | +import org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration; |
| 15 | +import org.jxmpp.jid.BareJid; |
| 16 | +import org.jxmpp.jid.impl.JidCreate; |
| 17 | + |
| 18 | +public class ReconnectionManagerAndStreamManagement { |
| 19 | + |
| 20 | + public static void reconnectionManagerAndStreamManagement(BareJid jid, String password) |
| 21 | + throws XMPPException, SmackException, IOException, InterruptedException { |
| 22 | + XMPPTCPConnectionConfiguration config = XMPPTCPConnectionConfiguration.builder() |
| 23 | + .setXmppAddressAndPassword(jid, password) |
| 24 | + .setDebuggerFactory(ConsoleDebugger.Factory.INSTANCE) |
| 25 | + .build(); |
| 26 | + XMPPTCPConnection connection = new XMPPTCPConnection(config); |
| 27 | + |
| 28 | + var reconnectionManager = ReconnectionManager.getInstanceFor(connection); |
| 29 | + reconnectionManager.setReconnectionPolicy(ReconnectionPolicy.FIXED_DELAY); |
| 30 | + reconnectionManager.setFixedDelay(1); |
| 31 | + reconnectionManager.enableAutomaticReconnection(); |
| 32 | + |
| 33 | + reconnectionManager.addReconnectionListener(new ReconnectionListener() { |
| 34 | + @Override |
| 35 | + public void reconnectingIn(int seconds) { |
| 36 | + System.out.println("Reconnecting in " + seconds + " seconds"); |
| 37 | + } |
| 38 | + @Override |
| 39 | + public void reconnectionFailed(Exception e) { |
| 40 | + System.out.println("Reconnection failed: " + e); |
| 41 | + e.printStackTrace(); |
| 42 | + } |
| 43 | + }); |
| 44 | + connection.addConnectionListener(new ConnectionListener() { |
| 45 | + public void connected(XMPPConnection connection) { |
| 46 | + System.out.println("Connected"); |
| 47 | + } |
| 48 | + public void authenticated(XMPPConnection connection, boolean resumed) { |
| 49 | + System.out.println("Authenticated"); |
| 50 | + } |
| 51 | + public void connectionClosed() { |
| 52 | + System.out.println("Connection closed"); |
| 53 | + } |
| 54 | + public void connectionClosedOnError(Exception e) { |
| 55 | + System.out.println("Connection closed on error: " + e); |
| 56 | + e.printStackTrace(); |
| 57 | + } |
| 58 | + }); |
| 59 | + |
| 60 | + connection.connect().login(); |
| 61 | + Thread.sleep(10000000); |
| 62 | + } |
| 63 | + |
| 64 | + public static void main(String[] args) throws XMPPException, SmackException, IOException, InterruptedException { |
| 65 | + BareJid jid = JidCreate.bareFrom(args[0]); |
| 66 | + String password = args[1]; |
| 67 | + reconnectionManagerAndStreamManagement(jid, password); |
| 68 | + } |
| 69 | +} |
0 commit comments