diff options
author | 2016-12-05 22:54:22 +0300 | |
---|---|---|
committer | 2016-12-05 23:19:10 +0300 | |
commit | c9c253f24601cc172058fd980b73791da4ec4f9c (patch) | |
tree | f4393dcec1d44b80357b4a3622e942f268ceeb12 /juick-xmpp/src/main | |
parent | 4d9b066060bd78243616cb6f1fbdf0d06b09cbe7 (diff) |
juick-xmpp: CleaningUp refactoring
Diffstat (limited to 'juick-xmpp/src/main')
3 files changed, 37 insertions, 43 deletions
diff --git a/juick-xmpp/src/main/java/com/juick/components/XMPPServer.java b/juick-xmpp/src/main/java/com/juick/components/XMPPServer.java index b5065c74..c51810a5 100644 --- a/juick-xmpp/src/main/java/com/juick/components/XMPPServer.java +++ b/juick-xmpp/src/main/java/com/juick/components/XMPPServer.java @@ -96,8 +96,6 @@ public class XMPPServer implements AutoCloseable { } } }); - - service.submit(new CleaningUp(this)); } } catch (Exception e) { diff --git a/juick-xmpp/src/main/java/com/juick/components/configuration/XmppAppConfiguration.java b/juick-xmpp/src/main/java/com/juick/components/configuration/XmppAppConfiguration.java index 31ffae9e..a10e6553 100644 --- a/juick-xmpp/src/main/java/com/juick/components/configuration/XmppAppConfiguration.java +++ b/juick-xmpp/src/main/java/com/juick/components/configuration/XmppAppConfiguration.java @@ -5,12 +5,14 @@ package com.juick.components.configuration; */ import com.juick.components.XMPPServer; +import com.juick.components.s2s.CleaningUp; import com.juick.configuration.DataConfiguration; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; import org.springframework.context.annotation.PropertySource; import org.springframework.core.env.Environment; +import org.springframework.scheduling.annotation.EnableScheduling; import javax.inject.Inject; import java.util.concurrent.ExecutorService; @@ -19,6 +21,7 @@ import java.util.concurrent.Executors; @Configuration @PropertySource("classpath:juick.conf") @Import(DataConfiguration.class) +@EnableScheduling public class XmppAppConfiguration { @Inject private Environment env; @@ -32,4 +35,8 @@ public class XmppAppConfiguration { public ExecutorService service() { return Executors.newCachedThreadPool(); } + @Bean + public CleaningUp cleaningUp() { + return new CleaningUp(); + } } diff --git a/juick-xmpp/src/main/java/com/juick/components/s2s/CleaningUp.java b/juick-xmpp/src/main/java/com/juick/components/s2s/CleaningUp.java index 52d2f3a6..9c6cf278 100644 --- a/juick-xmpp/src/main/java/com/juick/components/s2s/CleaningUp.java +++ b/juick-xmpp/src/main/java/com/juick/components/s2s/CleaningUp.java @@ -1,61 +1,50 @@ package com.juick.components.s2s; import com.juick.components.XMPPServer; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.scheduling.annotation.Scheduled; +import javax.inject.Inject; import java.util.Iterator; /** * * @author ugnich */ -public class CleaningUp implements Runnable { - XMPPServer xmpp; - - public CleaningUp(XMPPServer xmpp) { - this.xmpp = xmpp; - } +public class CleaningUp { - @Override - public void run() { - while (true) { - long now = System.currentTimeMillis(); - - synchronized (xmpp.getOutConnections()) { - for (Iterator<ConnectionOut> i = xmpp.getOutConnections().iterator(); i.hasNext(); ) { - ConnectionOut c = i.next(); - int inactive = (int) ((double) (now - c.tsLocalData) / 1000.0); - if (inactive > 900) { - c.closeConnection(); - i.remove(); - } - } - } + private static final Logger logger = LoggerFactory.getLogger(CleaningUp.class); - synchronized (xmpp.getInConnections()) { - for (Iterator<ConnectionIn> i = xmpp.getInConnections().iterator(); i.hasNext(); ) { - ConnectionIn c = i.next(); - int inactive = (int) ((double) (now - c.tsRemoteData) / 1000.0); - if (inactive > 900) { - c.closeConnection(); - i.remove(); - } + @Inject + XMPPServer xmpp; + + @Scheduled(fixedDelay = 10000) + public void cleanUp() { + long now = System.currentTimeMillis(); + + synchronized (xmpp.getOutConnections()) { + for (Iterator<ConnectionOut> i = xmpp.getOutConnections().iterator(); i.hasNext(); ) { + ConnectionOut c = i.next(); + int inactive = (int) ((double) (now - c.tsLocalData) / 1000.0); + if (inactive > 900) { + logger.info("closing idle outgoing connection to {}", c.to); + c.closeConnection(); + i.remove(); } } + } - synchronized (xmpp.getOutCache()) { - for (Iterator<CacheEntry> i = xmpp.getOutCache().iterator(); i.hasNext(); ) { - CacheEntry c = i.next(); - int inactive = (int) ((double) (now - c.tsCreated) / 1000.0); - if (inactive > 600) { - i.remove(); - } + synchronized (xmpp.getInConnections()) { + for (Iterator<ConnectionIn> i = xmpp.getInConnections().iterator(); i.hasNext(); ) { + ConnectionIn c = i.next(); + int inactive = (int) ((double) (now - c.tsRemoteData) / 1000.0); + if (inactive > 900) { + logger.info("closing idle incoming connection from {}", c.from); + c.closeConnection(); + i.remove(); } } - try { - Thread.sleep(10000); - } catch (InterruptedException e) { - } - } } } |