From 194a9a763c6b11d207e682b2f93de94475c473b4 Mon Sep 17 00:00:00 2001 From: Alexander Alexeev Date: Sat, 12 Nov 2016 01:34:07 +0700 Subject: extracted application configuration from Mvc configuration with application specific beans; extracted bean initialization from constructor; force using properties; --- .../main/java/com/juick/components/XMPPServer.java | 60 +++++++++++++--------- 1 file changed, 35 insertions(+), 25 deletions(-) (limited to 'juick-xmpp/src/main/java/com/juick/components/XMPPServer.java') 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 463d8c2a..e26e0a1e 100644 --- a/juick-xmpp/src/main/java/com/juick/components/XMPPServer.java +++ b/juick-xmpp/src/main/java/com/juick/components/XMPPServer.java @@ -1,7 +1,10 @@ package com.juick.components; import com.juick.components.s2s.*; -import com.juick.xmpp.*; +import com.juick.util.ThreadHelper; +import com.juick.xmpp.JID; +import com.juick.xmpp.Stanza; +import com.juick.xmpp.StanzaChild; import com.juick.xmpp.extensions.JuickMessage; import org.apache.commons.lang3.math.NumberUtils; import org.slf4j.Logger; @@ -23,11 +26,9 @@ import java.util.*; import java.util.concurrent.ExecutorService; /** - * * @author ugnich */ public class XMPPServer implements DisposableBean { - private static final Logger logger = LoggerFactory.getLogger(XMPPServer.class); public ExecutorService service; @@ -39,15 +40,19 @@ public class XMPPServer implements DisposableBean { public String keystorePassword; public List brokenSSLhosts; public List bannedHosts; + private final List inConnections = Collections.synchronizedList(new ArrayList<>()); private final List outConnections = Collections.synchronizedList(new ArrayList<>()); private final List outCache = Collections.synchronizedList(new ArrayList<>()); + public JdbcTemplate jdbc; final public HashMap childParsers = new HashMap<>(); + public XMPPServer(Environment env, ExecutorService service, JdbcTemplate jdbc) { this.service = service; this.jdbc = jdbc; + logger.info("component initialized"); try { HOSTNAME = env.getProperty("hostname"); @@ -64,7 +69,9 @@ public class XMPPServer implements DisposableBean { childParsers.put(JuickMessage.XMLNS, new JuickMessage()); router = new ConnectionRouter(this, componentName, componentPort, env.getProperty("xmpp_password")); + service.submit(router); + service.submit(() -> { final ServerSocket listener = new ServerSocket(s2sPort); logger.info("s2s listener ready"); @@ -79,6 +86,7 @@ public class XMPPServer implements DisposableBean { } } }); + service.submit(new CleaningUp(this)); } catch (Exception e) { @@ -86,6 +94,29 @@ public class XMPPServer implements DisposableBean { } } + @Override + public void destroy() throws Exception { + synchronized (getOutConnections()) { + for (Iterator i = getOutConnections().iterator(); i.hasNext(); ) { + ConnectionOut c = i.next(); + c.closeConnection(); + i.remove(); + } + } + + synchronized (getInConnections()) { + for (Iterator i = getInConnections().iterator(); i.hasNext(); ) { + ConnectionIn c = i.next(); + c.closeConnection(); + i.remove(); + } + } + + ThreadHelper.shutdownAndAwaitTermination(service); + + logger.info("Xmpp server destroyed"); + } + public void addConnectionIn(ConnectionIn c) { synchronized (getInConnections()) { getInConnections().add(c); @@ -113,7 +144,7 @@ public class XMPPServer implements DisposableBean { public String getFromCache(String hostname) { CacheEntry ret = null; synchronized (getOutCache()) { - for (Iterator i = getOutCache().iterator(); i.hasNext();) { + for (Iterator i = getOutCache().iterator(); i.hasNext(); ) { CacheEntry c = i.next(); if (c.hostname != null && c.hostname.equals(hostname)) { ret = c; @@ -202,27 +233,6 @@ public class XMPPServer implements DisposableBean { } } - @Override - public void destroy() { - synchronized (getOutConnections()) { - for (Iterator i = getOutConnections().iterator(); i.hasNext();) { - ConnectionOut c = i.next(); - c.closeConnection(); - i.remove(); - } - } - - synchronized (getInConnections()) { - for (Iterator i = getInConnections().iterator(); i.hasNext();) { - ConnectionIn c = i.next(); - c.closeConnection(); - i.remove(); - } - } - service.shutdown(); - logger.info("component destroyed"); - } - public ConnectionRouter getRouter() { return router; } -- cgit v1.2.3