From 4679da47ffedf2a0f29c324880fa537fe9447f39 Mon Sep 17 00:00:00 2001 From: Alexander Alexeev Date: Fri, 18 Nov 2016 03:47:00 +0700 Subject: correct closing babble components implemented --- .../src/main/java/com/juick/components/Crosspost.java | 11 ++++++++++- .../src/main/java/com/juick/components/Notifications.java | 11 ++++++++++- juick-ws/src/main/java/com/juick/ws/XMPPConnection.java | 10 +++++++++- juick-www/src/main/java/com/juick/www/Main.java | 11 +++++++++++ .../src/main/java/com/juick/components/XMPPBot.java | 15 +++++++++++++-- .../src/main/java/com/juick/components/XMPPFTServer.java | 14 ++++++++++++-- 6 files changed, 65 insertions(+), 7 deletions(-) diff --git a/juick-crosspost/src/main/java/com/juick/components/Crosspost.java b/juick-crosspost/src/main/java/com/juick/components/Crosspost.java index f14749eb..8b56f86d 100644 --- a/juick-crosspost/src/main/java/com/juick/components/Crosspost.java +++ b/juick-crosspost/src/main/java/com/juick/components/Crosspost.java @@ -23,6 +23,7 @@ import org.apache.commons.lang3.math.NumberUtils; import org.apache.commons.lang3.tuple.Pair; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.DisposableBean; import org.springframework.beans.factory.InitializingBean; import org.springframework.core.env.Environment; import org.springframework.util.Assert; @@ -45,7 +46,7 @@ import java.util.UUID; /** * @author Ugnich Anton */ -public class Crosspost implements InitializingBean { +public class Crosspost implements InitializingBean, DisposableBean { final static String TWITTERURL = "https://api.twitter.com/1.1/statuses/update.json"; final static String FBURL = "https://graph.facebook.com/me/feed"; final static String VKURL = "https://api.vk.com/method/wall.post"; @@ -110,6 +111,14 @@ public class Crosspost implements InitializingBean { } } + @Override + public void destroy() throws Exception { + if (xmpp != null) + xmpp.close(); + + logger.info("XmppSession on crosspost destroyed"); + } + public boolean facebookPost(final com.juick.Message jmsg) { String token = crosspostService.getFacebookToken(jmsg.getUser().getUid()).orElse(""); if (token.isEmpty()) { diff --git a/juick-notifications/src/main/java/com/juick/components/Notifications.java b/juick-notifications/src/main/java/com/juick/components/Notifications.java index 21e85c37..ea428a2d 100644 --- a/juick-notifications/src/main/java/com/juick/components/Notifications.java +++ b/juick-notifications/src/main/java/com/juick/components/Notifications.java @@ -38,6 +38,7 @@ import org.apache.http.util.TextUtils; import org.json.JSONObject; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.DisposableBean; import org.springframework.beans.factory.InitializingBean; import org.springframework.core.ParameterizedTypeReference; import org.springframework.core.env.Environment; @@ -56,7 +57,7 @@ import java.util.List; /** * @author Ugnich Anton */ -public class Notifications implements InitializingBean { +public class Notifications implements InitializingBean, DisposableBean { private static Logger logger = LoggerFactory.getLogger(Notifications.class); private final RestTemplate rest; @@ -209,6 +210,14 @@ public class Notifications implements InitializingBean { } } + @Override + public void destroy() throws Exception { + if (xmpp != null) + xmpp.close(); + + logger.info("ExternalComponent on notifications destroyed"); + } + String getWnsAccessToken() throws IOException, IllegalStateException { if (TextUtils.isEmpty(wns_application_sip)) { throw new IllegalStateException("'wns_application_sip' is not initialized"); diff --git a/juick-ws/src/main/java/com/juick/ws/XMPPConnection.java b/juick-ws/src/main/java/com/juick/ws/XMPPConnection.java index c6ac5b50..fb510306 100644 --- a/juick-ws/src/main/java/com/juick/ws/XMPPConnection.java +++ b/juick-ws/src/main/java/com/juick/ws/XMPPConnection.java @@ -29,7 +29,7 @@ import java.util.stream.Collectors; /** * @author ugnich */ -public class XMPPConnection implements InitializingBean { +public class XMPPConnection implements InitializingBean, DisposableBean { private static final Logger logger = LoggerFactory.getLogger(XMPPConnection.class); private final JdbcTemplate jdbc; @@ -90,6 +90,14 @@ public class XMPPConnection implements InitializingBean { } } + @Override + public void destroy() throws Exception { + if (xmpp != null) + xmpp.close(); + + logger.info("XmppSession on WS destroyed"); + } + MessageSerializer messageSerializer = new MessageSerializer(); private void onJuickPM(final int uid_to, final com.juick.Message jmsg) { diff --git a/juick-www/src/main/java/com/juick/www/Main.java b/juick-www/src/main/java/com/juick/www/Main.java index 9934d4e0..075b33fa 100644 --- a/juick-www/src/main/java/com/juick/www/Main.java +++ b/juick-www/src/main/java/com/juick/www/Main.java @@ -111,6 +111,17 @@ public class Main extends HttpServlet { } } + @Override + public void destroy() { + try { + if (xmpp != null) + xmpp.close(); + log("ExternalComponent on WWW destroyed"); + } catch (Exception e) { + log("exception on destroy", e); + } + } + public void setupXmppComponent(final Jid componentJid, final String password, final int port) { XmppSessionConfiguration configuration = XmppSessionConfiguration.builder() .extensions(Extension.of(Message.class)) diff --git a/juick-xmpp-bot/src/main/java/com/juick/components/XMPPBot.java b/juick-xmpp-bot/src/main/java/com/juick/components/XMPPBot.java index c95c94e3..bced456b 100644 --- a/juick-xmpp-bot/src/main/java/com/juick/components/XMPPBot.java +++ b/juick-xmpp-bot/src/main/java/com/juick/components/XMPPBot.java @@ -21,6 +21,7 @@ import rocks.xmpp.extensions.version.SoftwareVersionManager; import rocks.xmpp.extensions.version.model.SoftwareVersion; import javax.inject.Inject; +import java.io.Closeable; import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.net.URL; @@ -28,13 +29,15 @@ import java.net.URL; /** * Created by vt on 12/11/2016. */ -public class XMPPBot { +public class XMPPBot implements AutoCloseable{ private static final Logger logger = LoggerFactory.getLogger(XMPPBot.class); @Inject JdbcTemplate jdbc; + private ExternalComponent component; + public XMPPBot(Environment env) { - ExternalComponent component = ExternalComponent.create(env.getProperty("component_name", "juick.com"), + component = ExternalComponent.create(env.getProperty("component_name", "juick.com"), env.getProperty("component_password", "secret"), env.getProperty("component_host", "localhost"), NumberUtils.toInt(env.getProperty("component_port", "5347"), 5347)); Jid juickJid = Jid.of(env.getProperty("xmppbot_jid", "juick@juick.com/Juick")); @@ -88,4 +91,12 @@ public class XMPPBot { logger.error("bot initialization error", e); } } + + @Override + public void close() throws Exception { + if (component != null) + component.close(); + + logger.info("ExternalComponent on xmpp-bot destroyed"); + } } diff --git a/juick-xmpp-ft/src/main/java/com/juick/components/XMPPFTServer.java b/juick-xmpp-ft/src/main/java/com/juick/components/XMPPFTServer.java index 93baba70..d411179c 100644 --- a/juick-xmpp-ft/src/main/java/com/juick/components/XMPPFTServer.java +++ b/juick-xmpp-ft/src/main/java/com/juick/components/XMPPFTServer.java @@ -26,11 +26,13 @@ import java.util.concurrent.ExecutionException; /** * Created by vitalyster on 11.11.2016. */ -public class XMPPFTServer { +public class XMPPFTServer implements AutoCloseable { private static final Logger logger = LoggerFactory.getLogger(XMPPFTServer.class); + private ExternalComponent component; + public XMPPFTServer(Environment env) { - ExternalComponent component = ExternalComponent.create(env.getProperty("component_name", "files"), + component = ExternalComponent.create(env.getProperty("component_name", "files"), env.getProperty("component_password", "secret"), env.getProperty("component_host", "localhost"), NumberUtils.toInt(env.getProperty("component_port", "5347"), 5347)); String tmpDir = env.getProperty("upload_tmp_dir", "/tmp"); @@ -80,4 +82,12 @@ public class XMPPFTServer { logger.error("ft initialization error", e); } } + + @Override + public void close() throws Exception { + if (component != null) + component.close(); + + logger.info("ExternalComponent on xmpp-ft destroyed"); + } } -- cgit v1.2.3