Repository: camel Updated Branches: refs/heads/master 6051a0ea0 -> 38bbd4560
CAMEL-6256: Re-creating xmpp connection should clear out old stale connection during creation so you do not get a non working connection next time. Thanks to Prakash Shetty for details. Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/38bbd456 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/38bbd456 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/38bbd456 Branch: refs/heads/master Commit: 38bbd4560c0252129543feafd6d3b5d038f525f9 Parents: 6051a0e Author: Claus Ibsen <davscl...@apache.org> Authored: Thu Apr 28 16:03:53 2016 +0200 Committer: Claus Ibsen <davscl...@apache.org> Committed: Thu Apr 28 16:04:59 2016 +0200 ---------------------------------------------------------------------- .../camel/component/xmpp/XmppEndpoint.java | 36 ++++++++++++-------- 1 file changed, 21 insertions(+), 15 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/38bbd456/components/camel-xmpp/src/main/java/org/apache/camel/component/xmpp/XmppEndpoint.java ---------------------------------------------------------------------- diff --git a/components/camel-xmpp/src/main/java/org/apache/camel/component/xmpp/XmppEndpoint.java b/components/camel-xmpp/src/main/java/org/apache/camel/component/xmpp/XmppEndpoint.java index 6ded5b3..3146940 100644 --- a/components/camel-xmpp/src/main/java/org/apache/camel/component/xmpp/XmppEndpoint.java +++ b/components/camel-xmpp/src/main/java/org/apache/camel/component/xmpp/XmppEndpoint.java @@ -54,7 +54,7 @@ import org.slf4j.LoggerFactory; public class XmppEndpoint extends DefaultEndpoint implements HeaderFilterStrategyAware { private static final Logger LOG = LoggerFactory.getLogger(XmppEndpoint.class); - private XMPPConnection connection; + private volatile XMPPConnection connection; private XmppBinding binding; @UriPath @Metadata(required = "true") @@ -154,56 +154,62 @@ public class XmppEndpoint extends DefaultEndpoint implements HeaderFilterStrateg public synchronized XMPPConnection createConnection() throws XMPPException, SmackException, IOException { if (connection != null && connection.isConnected()) { + // use existing working connection return connection; } - if (connection == null) { - connection = createConnectionInternal(); - } + // prepare for creating new connection + connection = null; + + LOG.trace("Creating new connection ..."); + XMPPConnection newConnection = createConnectionInternal(); - connection.connect(); + newConnection.connect(); - connection.addPacketListener(new XmppLogger("INBOUND"), new PacketFilter() { + newConnection.addPacketListener(new XmppLogger("INBOUND"), new PacketFilter() { public boolean accept(Packet packet) { return true; } }); - connection.addPacketSendingListener(new XmppLogger("OUTBOUND"), new PacketFilter() { + newConnection.addPacketSendingListener(new XmppLogger("OUTBOUND"), new PacketFilter() { public boolean accept(Packet packet) { return true; } }); - if (!connection.isAuthenticated()) { + if (!newConnection.isAuthenticated()) { if (user != null) { if (LOG.isDebugEnabled()) { - LOG.debug("Logging in to XMPP as user: {} on connection: {}", user, getConnectionMessage(connection)); + LOG.debug("Logging in to XMPP as user: {} on connection: {}", user, getConnectionMessage(newConnection)); } if (password == null) { - LOG.warn("No password configured for user: {} on connection: {}", user, getConnectionMessage(connection)); + LOG.warn("No password configured for user: {} on connection: {}", user, getConnectionMessage(newConnection)); } if (createAccount) { - AccountManager accountManager = AccountManager.getInstance(connection); + AccountManager accountManager = AccountManager.getInstance(newConnection); accountManager.createAccount(user, password); } if (login) { if (resource != null) { - connection.login(user, password, resource); + newConnection.login(user, password, resource); } else { - connection.login(user, password); + newConnection.login(user, password); } } } else { if (LOG.isDebugEnabled()) { - LOG.debug("Logging in anonymously to XMPP on connection: {}", getConnectionMessage(connection)); + LOG.debug("Logging in anonymously to XMPP on connection: {}", getConnectionMessage(newConnection)); } - connection.loginAnonymously(); + newConnection.loginAnonymously(); } // presence is not needed to be sent after login } + // okay new connection was created successfully so assign it as the connection + LOG.debug("Created new connection successfully: {}", newConnection); + connection = newConnection; return connection; }