aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Vitaly Takmazov2017-11-01 13:53:50 +0300
committerGravatar Vitaly Takmazov2017-11-01 14:11:08 +0300
commita224f31fe050fe1eeb4435677caa27d523897704 (patch)
treed4e81d11a326a8a6db58a5c4daca88ec49f6f230
parente2c2cf416db9637ebd41c489cf5a533824d17d4c (diff)
api: try reconnect ws component
-rw-r--r--juick-api/src/main/java/com/juick/api/ApiServer.java22
-rw-r--r--juick-api/src/main/java/com/juick/api/TelegramBotManager.java5
-rw-r--r--juick-server-web/src/main/java/com/juick/server/component/DisconnectedEvent.java14
-rw-r--r--juick-ws/src/main/java/com/juick/ws/WebsocketComponent.java7
4 files changed, 40 insertions, 8 deletions
diff --git a/juick-api/src/main/java/com/juick/api/ApiServer.java b/juick-api/src/main/java/com/juick/api/ApiServer.java
index a5bf5d38..110d8e2f 100644
--- a/juick-api/src/main/java/com/juick/api/ApiServer.java
+++ b/juick-api/src/main/java/com/juick/api/ApiServer.java
@@ -17,10 +17,15 @@
package com.juick.api;
import com.juick.User;
+import com.juick.server.component.DisconnectedEvent;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
+import org.springframework.beans.factory.support.DefaultListableBeanFactory;
+import org.springframework.context.ApplicationListener;
+import org.springframework.web.socket.client.WebSocketConnectionManager;
+import org.springframework.web.socket.client.standard.StandardWebSocketClient;
import rocks.xmpp.addr.Jid;
import rocks.xmpp.core.XmppException;
import rocks.xmpp.core.session.Extension;
@@ -30,14 +35,16 @@ import rocks.xmpp.core.stanza.model.Message;
import rocks.xmpp.extensions.component.accept.ExternalComponent;
import rocks.xmpp.extensions.oob.model.x.OobX;
+import javax.annotation.Nonnull;
import javax.annotation.PostConstruct;
+import javax.inject.Inject;
import java.net.URI;
import java.net.URISyntaxException;
/**
* @author Ugnich Anton
*/
-public class ApiServer implements AutoCloseable {
+public class ApiServer implements ApplicationListener<DisconnectedEvent>, AutoCloseable {
private static Logger logger = LoggerFactory.getLogger(ApiServer.class);
private ExternalComponent xmpp;
@@ -52,6 +59,12 @@ public class ApiServer implements AutoCloseable {
private int xmppPort;
@Value("${xmpp_disabled:false}")
private boolean isXmppDisabled;
+ @Inject
+ private WebSocketConnectionManager webSocketConnectionManager;
+ @Inject
+ private StandardWebSocketClient client;
+ @Inject
+ private DefaultListableBeanFactory beanFactory;
@PostConstruct
public void init() {
@@ -106,4 +119,11 @@ public class ApiServer implements AutoCloseable {
logger.warn("attachment error", e1);
}
}
+
+ @Override
+ public void onApplicationEvent(@Nonnull DisconnectedEvent event) {
+ logger.info("reconnecting websocket...");
+ beanFactory.destroySingleton("client");
+ beanFactory.destroySingleton("webSocketConnectionManager");
+ }
}
diff --git a/juick-api/src/main/java/com/juick/api/TelegramBotManager.java b/juick-api/src/main/java/com/juick/api/TelegramBotManager.java
index b9154424..44f0fdc6 100644
--- a/juick-api/src/main/java/com/juick/api/TelegramBotManager.java
+++ b/juick-api/src/main/java/com/juick/api/TelegramBotManager.java
@@ -18,6 +18,7 @@
package com.juick.api;
import com.fasterxml.jackson.databind.ObjectMapper;
+import com.juick.server.component.DisconnectedEvent;
import com.juick.service.MessagesService;
import com.juick.service.SubscriptionService;
import com.juick.service.TelegramService;
@@ -33,6 +34,7 @@ import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
+import org.springframework.context.ApplicationEventPublisher;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.CloseStatus;
@@ -68,6 +70,8 @@ public class TelegramBotManager extends TextWebSocketHandler {
private SubscriptionService subscriptionService;
@Inject
private ObjectMapper jsonMapper;
+ @Inject
+ private ApplicationEventPublisher applicationEventPublisher;
private WebSocketSession session;
@@ -99,6 +103,7 @@ public class TelegramBotManager extends TextWebSocketHandler {
@Override
public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception {
logger.info("WebSocket disconnected with code {}: {}", status.getCode(), status.getReason());
+ applicationEventPublisher.publishEvent(new DisconnectedEvent(this));
}
@Scheduled(fixedRate = 30000)
diff --git a/juick-server-web/src/main/java/com/juick/server/component/DisconnectedEvent.java b/juick-server-web/src/main/java/com/juick/server/component/DisconnectedEvent.java
new file mode 100644
index 00000000..9da6d7a9
--- /dev/null
+++ b/juick-server-web/src/main/java/com/juick/server/component/DisconnectedEvent.java
@@ -0,0 +1,14 @@
+package com.juick.server.component;
+
+import org.springframework.context.ApplicationEvent;
+
+public class DisconnectedEvent extends ApplicationEvent {
+ /**
+ * Create a new ApplicationEvent.
+ *
+ * @param source the object on which the event initially occurred (never {@code null})
+ */
+ public DisconnectedEvent(Object source) {
+ super(source);
+ }
+}
diff --git a/juick-ws/src/main/java/com/juick/ws/WebsocketComponent.java b/juick-ws/src/main/java/com/juick/ws/WebsocketComponent.java
index 599d1394..45814ecb 100644
--- a/juick-ws/src/main/java/com/juick/ws/WebsocketComponent.java
+++ b/juick-ws/src/main/java/com/juick/ws/WebsocketComponent.java
@@ -17,15 +17,11 @@
package com.juick.ws;
-import com.juick.Message;
import com.juick.User;
import com.juick.server.helpers.AnonymousUser;
-import com.juick.server.protocol.JuickProtocol;
-import com.juick.server.protocol.ProtocolListener;
import com.juick.service.MessagesService;
import com.juick.service.SubscriptionService;
import com.juick.service.UserService;
-import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -33,16 +29,13 @@ import org.springframework.http.HttpHeaders;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.PingMessage;
-import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.TextWebSocketHandler;
import org.springframework.web.util.UriComponents;
import org.springframework.web.util.UriComponentsBuilder;
-import javax.annotation.PostConstruct;
import javax.inject.Inject;
import java.io.IOException;
-import java.lang.reflect.InvocationTargetException;
import java.net.URI;
import java.util.Collections;
import java.util.LinkedList;