Skip to content
Permalink
Browse files

[JENKINS-25222] Implement keep-alive for HipChat server

  • Loading branch information
kutzi committed Oct 26, 2014
1 parent 5c80a89 commit cd9eaf5fa28877fc3d2e42339219168ee3f0c0a3
Showing with 61 additions and 1 deletion.
  1. +61 −1 src/main/java/hudson/plugins/jabber/im/transport/JabberIMConnection.java
@@ -32,6 +32,8 @@
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;

@@ -57,6 +59,7 @@
import org.jivesoftware.smack.RosterEntry;
import org.jivesoftware.smack.SmackConfiguration;
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.SmackException.NotConnectedException;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.filter.MessageTypeFilter;
@@ -71,9 +74,10 @@
import org.jivesoftware.smack.proxy.ProxyInfo.ProxyType;
import org.jivesoftware.smack.tcp.XMPPTCPConnection;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smackx.muc.MultiUserChat;
import org.jivesoftware.smackx.delay.packet.DelayInformation;
import org.jivesoftware.smackx.muc.MultiUserChat;
import org.jivesoftware.smackx.nick.packet.Nick;
import org.jivesoftware.smackx.ping.packet.Ping;
import org.jivesoftware.smackx.vcardtemp.packet.VCard;
import org.springframework.util.Assert;

@@ -144,6 +148,10 @@

private final boolean acceptAllCerts;

private Runnable keepAliveCommand;

private ScheduledExecutorService scheduler;

static {
SmackConfiguration.setDefaultPacketReplyTimeout(20000);

@@ -239,6 +247,12 @@ public void close() {

this.groupChatCache.clear();
this.chatCache.clear();

if (this.scheduler != null) {
this.scheduler.shutdownNow();
this.scheduler = null;
}

if (this.connection.isConnected()) {
this.connection.disconnect();
}
@@ -415,12 +429,58 @@ public boolean verify(String hostname, SSLSession session) {

setupSubscriptionMode();
createVCardIfNeeded();

installServerTypeHacks();

listenForPrivateChats();
}

return this.connection.isAuthenticated();
}

private void installServerTypeHacks() {
if (this.connection.getServiceName().contains("hipchat")) {
// JENKINS-25222: HipChat connections time out after 150 seconds (http://help.hipchat.com/knowledgebase/articles/64377-xmpp-jabber-support-details)
addConnectionKeepAlivePings();
}
}

private void addConnectionKeepAlivePings() {
if (this.scheduler == null) {
this.scheduler = Executors.newScheduledThreadPool(1);
}

if (keepAliveCommand != null) {
return;
}

keepAliveCommand = new Runnable() {

@Override
public void run() {
// prevent long waits for lock
try {
if (!tryLock(5, TimeUnit.SECONDS)) {
return;
}

try {
connection.sendPacket(new Ping());
} catch (NotConnectedException e) {
// connection died, so lets scheduled task die, too
throw new RuntimeException(e);
} finally {
unlock();
}

} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
};
scheduler.scheduleAtFixedRate(keepAliveCommand, 60, 60, TimeUnit.SECONDS);
}

/**
* Transparently retries the connection attempt with legacy SSL if original attempt fails.
* @param originalException the exception of the original attempt (may be null)

0 comments on commit cd9eaf5

Please sign in to comment.