From 0c68eb43910b49669ace45de51aa2a4ea3d6f6f0 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Mon, 9 Oct 2017 14:46:43 +0300 Subject: xmpp: Java 8 Time API --- .../main/java/com/juick/components/CleaningUp.java | 37 +++++++++++----------- .../java/com/juick/components/s2s/Connection.java | 9 +++--- 2 files changed, 23 insertions(+), 23 deletions(-) (limited to 'juick-xmpp/src') diff --git a/juick-xmpp/src/main/java/com/juick/components/CleaningUp.java b/juick-xmpp/src/main/java/com/juick/components/CleaningUp.java index 067af6e8..16747ea4 100644 --- a/juick-xmpp/src/main/java/com/juick/components/CleaningUp.java +++ b/juick-xmpp/src/main/java/com/juick/components/CleaningUp.java @@ -23,6 +23,8 @@ import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import javax.inject.Inject; +import java.time.Duration; +import java.time.Instant; /** * @@ -33,29 +35,26 @@ public class CleaningUp { private static final Logger logger = LoggerFactory.getLogger(CleaningUp.class); + private static final int TIMEOUT_MINUTES = 15; + @Inject XMPPServer xmpp; @Scheduled(fixedDelay = 10000) public void cleanUp() { - long now = System.currentTimeMillis(); - - xmpp.getOutConnections().stream().filter(c -> { - int inactive = (int) ((double) (now - c.tsLocalData) / 1000.0); - return inactive > 900; - }).forEach(c -> { - logger.info("closing idle outgoing connection to {}", c.to); - c.closeConnection(); - xmpp.getOutConnections().remove(c); - }); - - xmpp.getInConnections().stream().filter(c -> { - int inactive = (int) ((double) (now - c.tsRemoteData) / 1000.0); - return inactive > 900; - }).forEach(c -> { - logger.info("closing idle incoming connection from {}", c.from); - c.closeConnection(); - xmpp.getInConnections().remove(c); - }); + Instant now = Instant.now(); + xmpp.getOutConnections().stream().filter(c -> Duration.between(now, c.updated).toMinutes() > TIMEOUT_MINUTES) + .forEach(c -> { + logger.info("closing idle outgoing connection to {}", c.to); + c.closeConnection(); + xmpp.getOutConnections().remove(c); + }); + + xmpp.getInConnections().stream().filter(c -> Duration.between(now, c.updated).toMinutes() > TIMEOUT_MINUTES) + .forEach(c -> { + logger.info("closing idle incoming connection from {}", c.from); + c.closeConnection(); + xmpp.getInConnections().remove(c); + }); } } diff --git a/juick-xmpp/src/main/java/com/juick/components/s2s/Connection.java b/juick-xmpp/src/main/java/com/juick/components/s2s/Connection.java index 7fd036eb..86f276b4 100644 --- a/juick-xmpp/src/main/java/com/juick/components/s2s/Connection.java +++ b/juick-xmpp/src/main/java/com/juick/components/s2s/Connection.java @@ -33,6 +33,7 @@ import java.net.Socket; import java.nio.charset.StandardCharsets; import java.security.*; import java.security.cert.CertificateException; +import java.time.Instant; import java.util.UUID; /** @@ -44,8 +45,8 @@ public class Connection { protected static final Logger logger = LoggerFactory.getLogger(Connection.class); public String streamID; - public long tsCreated = 0; - public long tsLocalData = 0; + public Instant created; + public Instant updated; public long bytesLocal = 0; public long packetsLocal = 0; XMPPServer xmpp; @@ -74,7 +75,7 @@ public class Connection { public Connection(XMPPServer xmpp) throws XmlPullParserException, KeyStoreException, CertificateException, NoSuchAlgorithmException, IOException, UnrecoverableKeyException, KeyManagementException { this.xmpp = xmpp; - tsCreated = System.currentTimeMillis(); + created = Instant.now(); KeyStore ks = KeyStore.getInstance("JKS"); try (InputStream ksIs = new FileInputStream(xmpp.keystore)) { ks.load(ksIs, xmpp.keystorePassword.toCharArray()); @@ -111,7 +112,7 @@ public class Connection { logger.error("send stanza failed", e); } - tsLocalData = System.currentTimeMillis(); + updated = Instant.now(); bytesLocal += xml.length(); packetsLocal++; } -- cgit v1.2.3