From c9c253f24601cc172058fd980b73791da4ec4f9c Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Mon, 5 Dec 2016 22:54:22 +0300 Subject: juick-xmpp: CleaningUp refactoring --- .../main/java/com/juick/components/XMPPServer.java | 2 - .../configuration/XmppAppConfiguration.java | 7 +++ .../java/com/juick/components/s2s/CleaningUp.java | 71 +++++++++------------- 3 files changed, 37 insertions(+), 43 deletions(-) (limited to 'juick-xmpp') 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 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 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 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 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 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) { - } - } } } -- cgit v1.2.3