aboutsummaryrefslogtreecommitdiff
path: root/juick-www/src/main/java/com/juick/www
diff options
context:
space:
mode:
authorGravatar Vitaly Takmazov2016-08-01 17:35:14 +0300
committerGravatar Vitaly Takmazov2016-08-02 00:30:04 +0300
commitd6d55af49e57a9950d3f646dea8e3307ac6c6987 (patch)
treea6b8be31dc92d92ad9e73d5f9a3bf0c2f2c202fe /juick-www/src/main/java/com/juick/www
parent2262c9c81fee46f21b3f1f2f3441cdfd41cb1f64 (diff)
www: reconnect XMPP component using failsafe library
Diffstat (limited to 'juick-www/src/main/java/com/juick/www')
-rw-r--r--juick-www/src/main/java/com/juick/www/Main.java40
1 files changed, 24 insertions, 16 deletions
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 d8096ff4..63194fbd 100644
--- a/juick-www/src/main/java/com/juick/www/Main.java
+++ b/juick-www/src/main/java/com/juick/www/Main.java
@@ -21,8 +21,11 @@ import com.juick.server.UserQueries;
import com.juick.xmpp.JID;
import com.juick.xmpp.Stream;
import com.juick.xmpp.StreamComponent;
+import net.jodah.failsafe.Execution;
+import net.jodah.failsafe.RetryPolicy;
import org.apache.commons.dbcp2.BasicDataSource;
import org.springframework.jdbc.core.JdbcTemplate;
+import org.xmlpull.v1.XmlPullParserException;
import ru.sape.Sape;
import javax.servlet.ServletException;
@@ -32,13 +35,13 @@ import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
-import java.net.ConnectException;
import java.net.Socket;
import java.net.URLEncoder;
import java.util.Objects;
import java.util.Properties;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
+import java.util.concurrent.TimeUnit;
/**
*
@@ -66,7 +69,8 @@ public class Main extends HttpServlet implements Stream.StreamListener {
SignUp signup = new SignUp();
Settings settings = new Settings();
RSS rss = new RSS();
- ExecutorService executorService = Executors.newSingleThreadExecutor();
+ ExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
+ Execution execution;
@Override
public void init() throws ServletException {
@@ -101,27 +105,31 @@ public class Main extends HttpServlet implements Stream.StreamListener {
}
}
- @Override
- public void destroy() {
- executorService.shutdownNow();
- super.destroy();
- }
-
public void setupXmppComponent(final JID componentJid, final String password) {
+ @SuppressWarnings("unchecked") RetryPolicy retryPolicy = new RetryPolicy()
+ .withBackoff(1, 30, TimeUnit.SECONDS)
+ .withJitter(0.1)
+ .retryOn(IOException.class, XmlPullParserException.class);
+ execution = new Execution(retryPolicy);
executorService.submit(() -> {
- try {
- Socket socket = new Socket("localhost", 5347);
- xmpp = new StreamComponent(componentJid, socket.getInputStream(), socket.getOutputStream(), password);
- xmpp.addListener(Main.this);
- xmpp.startParsing();
- } catch (IOException e) {
- log("XMPP is not initialized");
+ while (!execution.isComplete()) {
+ try {
+ Socket socket = new Socket("localhost", 5347);
+ xmpp = new StreamComponent(componentJid, socket.getInputStream(), socket.getOutputStream(), password);
+ xmpp.addListener(Main.this);
+ xmpp.startParsing();
+ } catch (IOException e) {
+ log("XMPP router disconnected, reconnecting...");
+ }
}
});
}
@Override
- public void onStreamFail(Exception e) {log("XMPP STREAM FAIL:" + e);}
+ public void onStreamFail(Exception e) {
+ log("XMPP STREAM FAIL:" + e);
+ execution.recordFailure(e);
+ }
@Override
public void onStreamReady() {