From d6d55af49e57a9950d3f646dea8e3307ac6c6987 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Mon, 1 Aug 2016 17:35:14 +0300 Subject: www: reconnect XMPP component using failsafe library --- juick-www/build.gradle | 1 + juick-www/src/main/java/com/juick/www/Main.java | 40 +++++++++++++++---------- 2 files changed, 25 insertions(+), 16 deletions(-) diff --git a/juick-www/build.gradle b/juick-www/build.gradle index f609cc90..6089ca15 100644 --- a/juick-www/build.gradle +++ b/juick-www/build.gradle @@ -36,6 +36,7 @@ dependencies { compile 'org.json:json:20151123' compile 'net.coobird:thumbnailator:0.4.8' compile 'com.github.ooxi:serialized-php-parser:0.5.0' + compile 'net.jodah:failsafe:0.9.2' providedCompile 'javax.servlet:javax.servlet-api:3.1.0' providedRuntime 'mysql:mysql-connector-java:5.1.39' } 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() { -- cgit v1.2.3