diff options
author | Vitaly Takmazov | 2017-10-09 14:46:43 +0300 |
---|---|---|
committer | Vitaly Takmazov | 2017-10-09 14:46:43 +0300 |
commit | 0c68eb43910b49669ace45de51aa2a4ea3d6f6f0 (patch) | |
tree | 2b90d9e76a13006f9a5875709db70c9778b4bae5 /juick-xmpp/src | |
parent | 51bfc341be1975b7a11e0b3a59cfbb4710e78446 (diff) |
xmpp: Java 8 Time API
Diffstat (limited to 'juick-xmpp/src')
-rw-r--r-- | juick-xmpp/src/main/java/com/juick/components/CleaningUp.java | 37 | ||||
-rw-r--r-- | juick-xmpp/src/main/java/com/juick/components/s2s/Connection.java | 9 |
2 files changed, 23 insertions, 23 deletions
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++; } |