aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Alexander Alexeev2016-11-18 13:48:45 +0700
committerGravatar Vitaly Takmazov2016-11-18 10:07:20 +0300
commit7128df991d737d23ee77f89bfe79ec4b79a362df (patch)
treee02cdb213fb52f2215919f38d0b3244d50a3e94b
parent5af964e05a79e64f02ebcfb3e1b19f109e32deaf (diff)
one way for inititialization and finalization: using constructor's initialization or method init() and AutoCloseable inteface and method close()
-rw-r--r--juick-api/src/main/java/com/juick/api/Main.java31
-rw-r--r--juick-crosspost/src/main/java/com/juick/components/Crosspost.java11
-rw-r--r--juick-notifications/src/main/java/com/juick/components/Notifications.java11
-rw-r--r--juick-server/src/main/java/com/juick/configuration/DataConfiguration.java6
-rw-r--r--juick-ws/src/main/java/com/juick/ws/XMPPConnection.java11
-rw-r--r--juick-xmpp-bot/src/main/java/com/juick/components/XMPPBot.java3
-rw-r--r--juick-xmpp-bot/src/main/java/com/juick/components/configuration/BotAppConfiguration.java11
-rw-r--r--juick-xmpp-ft/src/main/java/com/juick/components/configuration/FileTransferAppConfiguration.java11
-rw-r--r--juick-xmpp/src/main/java/com/juick/components/XMPPServer.java9
9 files changed, 42 insertions, 62 deletions
diff --git a/juick-api/src/main/java/com/juick/api/Main.java b/juick-api/src/main/java/com/juick/api/Main.java
index 6d7deeac..669dc11d 100644
--- a/juick-api/src/main/java/com/juick/api/Main.java
+++ b/juick-api/src/main/java/com/juick/api/Main.java
@@ -55,8 +55,8 @@ import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
+
/**
- *
* @author Ugnich Anton
*/
@WebServlet(name = "Main", urlPatterns = {"/"})
@@ -103,6 +103,19 @@ public class Main extends HttpServlet {
}
}
+ @Override
+ public void destroy() {
+ try {
+ if (xmpp != null)
+ xmpp.close();
+
+ log("ExternalComponent on juick-api destroyed");
+ } catch (Exception e) {
+ log("Exception occurs on juick-api destroy", e);
+ }
+
+ }
+
public void setupXmppComponent(final String host, final int port, final String jid, final String password) {
XmppSessionConfiguration configuration = XmppSessionConfiguration.builder()
.extensions(Extension.of(com.juick.Message.class))
@@ -115,12 +128,13 @@ public class Main extends HttpServlet {
}
}
- /**
+ /**
* Handles the HTTP <code>GET</code> method.
- * @param request servlet request
+ *
+ * @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
- * @throws IOException if an I/O error occurs
+ * @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
@@ -182,12 +196,13 @@ public class Main extends HttpServlet {
}
}
- /**
+ /**
* Handles the HTTP <code>POST</code> method.
- * @param request servlet request
+ *
+ * @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
- * @throws IOException if an I/O error occurs
+ * @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
@@ -272,7 +287,7 @@ public class Main extends HttpServlet {
}
String paramImg = request.getParameter("img");
- if (attachmentFName == null && paramImg != null && paramImg.length() > 10 ) {
+ if (attachmentFName == null && paramImg != null && paramImg.length() > 10) {
try {
URL imgUrl = new URL(paramImg);
attachmentFName = Utils.downloadImage(imgUrl);
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 8b56f86d..5dc86f27 100644
--- a/juick-crosspost/src/main/java/com/juick/components/Crosspost.java
+++ b/juick-crosspost/src/main/java/com/juick/components/Crosspost.java
@@ -23,8 +23,6 @@ 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;
import rocks.xmpp.core.XmppException;
@@ -34,6 +32,7 @@ import rocks.xmpp.core.session.XmppSessionConfiguration;
import rocks.xmpp.core.stanza.model.Message;
import rocks.xmpp.extensions.component.accept.ExternalComponent;
+import javax.annotation.PostConstruct;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import javax.net.ssl.HttpsURLConnection;
@@ -46,7 +45,7 @@ import java.util.UUID;
/**
* @author Ugnich Anton
*/
-public class Crosspost implements InitializingBean, DisposableBean {
+public class Crosspost implements AutoCloseable {
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";
@@ -85,8 +84,8 @@ public class Crosspost implements InitializingBean, DisposableBean {
return ret;
}
- @Override
- public void afterPropertiesSet() {
+ @PostConstruct
+ public void init() {
XmppSessionConfiguration configuration = XmppSessionConfiguration.builder()
.extensions(Extension.of(com.juick.Message.class))
.build();
@@ -112,7 +111,7 @@ public class Crosspost implements InitializingBean, DisposableBean {
}
@Override
- public void destroy() throws Exception {
+ public void close() throws Exception {
if (xmpp != null)
xmpp.close();
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 ea428a2d..ebc69bd1 100644
--- a/juick-notifications/src/main/java/com/juick/components/Notifications.java
+++ b/juick-notifications/src/main/java/com/juick/components/Notifications.java
@@ -38,8 +38,6 @@ 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;
import org.springframework.http.HttpMethod;
@@ -49,6 +47,7 @@ import rocks.xmpp.core.session.Extension;
import rocks.xmpp.core.session.XmppSessionConfiguration;
import rocks.xmpp.extensions.component.accept.ExternalComponent;
+import javax.annotation.PostConstruct;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
@@ -57,7 +56,7 @@ import java.util.List;
/**
* @author Ugnich Anton
*/
-public class Notifications implements InitializingBean, DisposableBean {
+public class Notifications implements AutoCloseable {
private static Logger logger = LoggerFactory.getLogger(Notifications.class);
private final RestTemplate rest;
@@ -84,8 +83,8 @@ public class Notifications implements InitializingBean, DisposableBean {
xmppPushPassword = env.getProperty("push_xmpp_password", "");
}
- @Override
- public void afterPropertiesSet() {
+ @PostConstruct
+ public void init() {
XmppSessionConfiguration configuration = XmppSessionConfiguration.builder()
.extensions(Extension.of(com.juick.Message.class))
.build();
@@ -211,7 +210,7 @@ public class Notifications implements InitializingBean, DisposableBean {
}
@Override
- public void destroy() throws Exception {
+ public void close() throws Exception {
if (xmpp != null)
xmpp.close();
diff --git a/juick-server/src/main/java/com/juick/configuration/DataConfiguration.java b/juick-server/src/main/java/com/juick/configuration/DataConfiguration.java
index 88fc00b3..9f733184 100644
--- a/juick-server/src/main/java/com/juick/configuration/DataConfiguration.java
+++ b/juick-server/src/main/java/com/juick/configuration/DataConfiguration.java
@@ -1,6 +1,7 @@
package com.juick.configuration;
import org.apache.commons.dbcp2.BasicDataSource;
+import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@@ -13,7 +14,6 @@ import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.TransactionManagementConfigurer;
import javax.annotation.Resource;
-import javax.sql.DataSource;
/**
* Created by aalexeev on 11/11/16.
@@ -26,8 +26,10 @@ public class DataConfiguration implements TransactionManagementConfigurer {
@Resource
private Environment env;
+ // NOTE: The close() method will be called automatically with default @Bean settings
+ // But Datasource interface has no close() method
@Bean
- public DataSource dataSource() {
+ public BasicDataSource dataSource() {
BasicDataSource dataSource = new BasicDataSource();
dataSource.setDriverClassName(env.getProperty("datasource_driver", "com.mysql.jdbc.Driver"));
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 fb510306..30dad3f3 100644
--- a/juick-ws/src/main/java/com/juick/ws/XMPPConnection.java
+++ b/juick-ws/src/main/java/com/juick/ws/XMPPConnection.java
@@ -4,12 +4,9 @@ import com.juick.User;
import com.juick.json.MessageSerializer;
import com.juick.server.MessagesQueries;
import com.juick.server.SubscriptionsQueries;
-import com.juick.util.ThreadHelper;
import org.apache.commons.lang3.math.NumberUtils;
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.jdbc.core.JdbcTemplate;
import org.springframework.util.Assert;
@@ -23,13 +20,12 @@ import rocks.xmpp.extensions.component.accept.ExternalComponent;
import java.io.IOException;
import java.util.List;
-import java.util.concurrent.ExecutorService;
import java.util.stream.Collectors;
/**
* @author ugnich
*/
-public class XMPPConnection implements InitializingBean, DisposableBean {
+public class XMPPConnection implements AutoCloseable {
private static final Logger logger = LoggerFactory.getLogger(XMPPConnection.class);
private final JdbcTemplate jdbc;
@@ -56,10 +52,7 @@ public class XMPPConnection implements InitializingBean, DisposableBean {
wsJid = env.getProperty("ws_jid", "ws.juick.local");
ms = new MessageSerializer();
- }
- @Override
- public void afterPropertiesSet() {
XmppSessionConfiguration configuration = XmppSessionConfiguration.builder()
.extensions(Extension.of(com.juick.Message.class))
.build();
@@ -91,7 +84,7 @@ public class XMPPConnection implements InitializingBean, DisposableBean {
}
@Override
- public void destroy() throws Exception {
+ public void close() throws Exception {
if (xmpp != null)
xmpp.close();
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 bced456b..43ed3dde 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,7 +21,6 @@ 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;
@@ -29,7 +28,7 @@ import java.net.URL;
/**
* Created by vt on 12/11/2016.
*/
-public class XMPPBot implements AutoCloseable{
+public class XMPPBot implements AutoCloseable {
private static final Logger logger = LoggerFactory.getLogger(XMPPBot.class);
@Inject
JdbcTemplate jdbc;
diff --git a/juick-xmpp-bot/src/main/java/com/juick/components/configuration/BotAppConfiguration.java b/juick-xmpp-bot/src/main/java/com/juick/components/configuration/BotAppConfiguration.java
index 8eb45170..d1a1ed11 100644
--- a/juick-xmpp-bot/src/main/java/com/juick/components/configuration/BotAppConfiguration.java
+++ b/juick-xmpp-bot/src/main/java/com/juick/components/configuration/BotAppConfiguration.java
@@ -7,11 +7,8 @@ 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.jdbc.core.JdbcTemplate;
import javax.inject.Inject;
-import java.util.concurrent.ExecutorService;
-import java.util.concurrent.Executors;
/**
* Created by aalexeev on 11/12/16.
@@ -22,17 +19,9 @@ import java.util.concurrent.Executors;
public class BotAppConfiguration {
@Inject
private Environment env;
- @Inject
- private JdbcTemplate jdbc;
@Bean
public XMPPBot xmpp() {
return new XMPPBot(env);
}
-
- @Bean
- public ExecutorService service() {
- return Executors.newCachedThreadPool();
- }
-
}
diff --git a/juick-xmpp-ft/src/main/java/com/juick/components/configuration/FileTransferAppConfiguration.java b/juick-xmpp-ft/src/main/java/com/juick/components/configuration/FileTransferAppConfiguration.java
index 88579328..ce7509c4 100644
--- a/juick-xmpp-ft/src/main/java/com/juick/components/configuration/FileTransferAppConfiguration.java
+++ b/juick-xmpp-ft/src/main/java/com/juick/components/configuration/FileTransferAppConfiguration.java
@@ -7,11 +7,8 @@ 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.jdbc.core.JdbcTemplate;
import javax.inject.Inject;
-import java.util.concurrent.ExecutorService;
-import java.util.concurrent.Executors;
/**
* Created by aalexeev on 11/12/16.
@@ -22,17 +19,9 @@ import java.util.concurrent.Executors;
public class FileTransferAppConfiguration {
@Inject
private Environment env;
- @Inject
- private JdbcTemplate jdbc;
@Bean
public XMPPFTServer xmpp() {
return new XMPPFTServer(env);
}
-
- @Bean
- public ExecutorService service() {
- return Executors.newCachedThreadPool();
- }
-
}
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 cb5172d6..a2f593ee 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,6 @@
package com.juick.components;
import com.juick.components.s2s.*;
-import com.juick.util.ThreadHelper;
import com.juick.xmpp.JID;
import com.juick.xmpp.Stanza;
import com.juick.xmpp.StanzaChild;
@@ -10,7 +9,6 @@ import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import org.springframework.beans.factory.DisposableBean;
import org.springframework.core.env.Environment;
import org.springframework.jdbc.core.JdbcTemplate;
import org.xmlpull.v1.XmlPullParserException;
@@ -29,7 +27,7 @@ import java.util.concurrent.ExecutorService;
/**
* @author ugnich
*/
-public class XMPPServer implements DisposableBean {
+public class XMPPServer implements AutoCloseable {
private static final Logger logger = LoggerFactory.getLogger(XMPPServer.class);
public ExecutorService service;
@@ -99,7 +97,7 @@ public class XMPPServer implements DisposableBean {
}
@Override
- public void destroy() throws Exception {
+ public void close() throws Exception {
synchronized (getOutConnections()) {
for (Iterator<ConnectionOut> i = getOutConnections().iterator(); i.hasNext(); ) {
ConnectionOut c = i.next();
@@ -115,9 +113,6 @@ public class XMPPServer implements DisposableBean {
i.remove();
}
}
-
- ThreadHelper.shutdownAndAwaitTermination(service);
-
logger.info("Xmpp server destroyed");
}