@Override public void initializationCompleted() { if (isInitializationComplete()) { // Do we really need to do this again? return; } super.initializationCompleted(); initializationCompleted = true; for (Map<String, Object> params : waitingTasks) { reconnectService(params, connectionDelay); } waitingTasks.clear(); if (null != watchdog) { watchdog.start(); } }
protected void setupWatchdogThread() { watchdog = new Thread(new Watchdog(), "Watchdog - " + getName()); watchdog.setDaemon(true); }
@Override public void run() { while (true) { try { // Sleep... Thread.sleep(watchdogDelay); ++watchdogRuns; /** * Walk through all connections and check whether they are really alive. Depending on the * configuration send either whitespace or XMPP ping if the service is inactive for the * configured period of time */ doForAllServices( new ServiceChecker<IO>() { @Override public void check(final XMPPIOService service) { try { if (null != service) { long curr_time = System.currentTimeMillis(); long lastTransfer; switch (watchdogPingType) { case XMPP: lastTransfer = service.getLastXmppPacketReceiveTime(); break; case WHITESPACE: default: lastTransfer = service.getLastTransferTime(); break; } if (curr_time - lastTransfer >= maxInactivityTime) { // Stop the service if max keep-alive time is exceeded // for non-active connections. if (log.isLoggable(Level.INFO)) { log.log( Level.INFO, "{0}: Max inactive time exceeded, stopping: {1}", new Object[] {getName(), service}); } ++watchdogStopped; service.stop(); } else { if (curr_time - lastTransfer >= (watchdogTimeout)) { /** * At least once every configured timings check if the connection is still * alive with the use of configured ping type. */ switch (watchdogPingType) { case XMPP: pingPacket = Iq.packetInstance( pingElement.clone(), JID.jidInstanceNS( (String) service .getSessionData() .get(XMPPIOService.HOSTNAME_KEY)), JID.jidInstanceNS(service.getUserJid())); if (!writePacketToSocket((IO) service, pingPacket)) { // writing failed, stopp service ++watchdogStopped; service.stop(); } break; case WHITESPACE: service.writeRawData(" "); break; } ++watchdogTests; } } } } catch (IOException e) { // Close the service try { if (service != null) { log.info(getName() + "Found dead connection, stopping: " + service); ++watchdogStopped; service.forceStop(); } } catch (Exception ignore) { // Do nothing here as we expect Exception to be thrown here... } } } }); } catch (InterruptedException e) { /* Do nothing here */ } } }