From f203b7d6605d55a1857b6ee42a93350a7f66937a Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Thu, 22 Mar 2018 01:29:31 +0300 Subject: juick-server: move websocket client to notifications server --- .../server/component/JuickServerComponent.java | 73 --------------------- .../component/JuickServerReconnectManager.java | 22 ------- .../JuickServerWebsocketConfiguration.java | 2 +- .../NotificationsAppConfiguration.java | 4 +- .../components/service/JuickServerComponent.java | 75 ++++++++++++++++++++++ .../service/JuickServerReconnectManager.java | 23 +++++++ .../java/com/juick/server/WebsocketManager.java | 2 + .../server/configuration/ApiAppConfiguration.java | 36 +---------- 8 files changed, 105 insertions(+), 132 deletions(-) delete mode 100644 juick-common/src/main/java/com/juick/server/component/JuickServerComponent.java delete mode 100644 juick-common/src/main/java/com/juick/server/component/JuickServerReconnectManager.java create mode 100644 juick-notifications/src/main/java/com/juick/components/service/JuickServerComponent.java create mode 100644 juick-notifications/src/main/java/com/juick/components/service/JuickServerReconnectManager.java diff --git a/juick-common/src/main/java/com/juick/server/component/JuickServerComponent.java b/juick-common/src/main/java/com/juick/server/component/JuickServerComponent.java deleted file mode 100644 index dba1f480..00000000 --- a/juick-common/src/main/java/com/juick/server/component/JuickServerComponent.java +++ /dev/null @@ -1,73 +0,0 @@ -package com.juick.server.component; - -import com.fasterxml.jackson.databind.ObjectMapper; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.context.ApplicationEventPublisher; -import org.springframework.scheduling.annotation.Scheduled; -import org.springframework.stereotype.Component; -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 javax.annotation.PostConstruct; -import javax.annotation.PreDestroy; -import javax.inject.Inject; -import java.io.IOException; -import java.util.concurrent.atomic.AtomicBoolean; - -@Component -public class JuickServerComponent extends TextWebSocketHandler { - private static Logger logger = LoggerFactory.getLogger(JuickServerComponent.class); - @Inject - private ApplicationEventPublisher applicationEventPublisher; - @Inject - private ObjectMapper jsonMapper; - - private WebSocketSession session; - private final AtomicBoolean closeFlag = new AtomicBoolean(false); - - @PostConstruct - public void init() { - closeFlag.set(false); - } - @Override - public void afterConnectionEstablished(WebSocketSession session) throws Exception { - logger.info("WebSocket connected"); - this.session = session; - } - - @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)); - } - - @Override - protected void handleTextMessage(WebSocketSession session, TextMessage text) throws Exception { - com.juick.Message jmsg = jsonMapper.readValue(text.asBytes(), com.juick.Message.class); - - if (logger.isInfoEnabled()) // prevent writeValueAsString execution if logger disabled - logger.info("got jmsg: {}", jsonMapper.writeValueAsString(jmsg)); - if (!closeFlag.get()) { - applicationEventPublisher.publishEvent(new MessageEvent(this, jmsg)); - } - } - - @Scheduled(fixedRate = 30000, initialDelay = 30000) - public void ping() throws IOException { - if (session != null && session.isOpen()) { - logger.debug("Sending WebSocket ping"); - session.sendMessage(new PingMessage()); - } else if (!closeFlag.get()) { - applicationEventPublisher.publishEvent(new DisconnectedEvent(this)); - } - } - @PreDestroy - public void close() { - closeFlag.set(true); - } - -} diff --git a/juick-common/src/main/java/com/juick/server/component/JuickServerReconnectManager.java b/juick-common/src/main/java/com/juick/server/component/JuickServerReconnectManager.java deleted file mode 100644 index a662e4fb..00000000 --- a/juick-common/src/main/java/com/juick/server/component/JuickServerReconnectManager.java +++ /dev/null @@ -1,22 +0,0 @@ -package com.juick.server.component; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.context.ApplicationListener; -import org.springframework.stereotype.Component; -import org.springframework.web.socket.client.WebSocketConnectionManager; - -import javax.inject.Inject; - -@Component -public class JuickServerReconnectManager implements ApplicationListener { - private static Logger logger = LoggerFactory.getLogger(JuickServerReconnectManager.class); - @Inject - private WebSocketConnectionManager webSocketConnectionManager; - @Override - public void onApplicationEvent(DisconnectedEvent event) { - logger.info("retrying..."); - webSocketConnectionManager.stop(); - webSocketConnectionManager.start(); - } -} diff --git a/juick-notifications/src/main/java/com/juick/components/configuration/JuickServerWebsocketConfiguration.java b/juick-notifications/src/main/java/com/juick/components/configuration/JuickServerWebsocketConfiguration.java index e94a3a8e..79459d72 100644 --- a/juick-notifications/src/main/java/com/juick/components/configuration/JuickServerWebsocketConfiguration.java +++ b/juick-notifications/src/main/java/com/juick/components/configuration/JuickServerWebsocketConfiguration.java @@ -1,7 +1,7 @@ package com.juick.components.configuration; import com.fasterxml.jackson.databind.ObjectMapper; -import com.juick.server.component.JuickServerComponent; +import com.juick.components.service.JuickServerComponent; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/juick-notifications/src/main/java/com/juick/components/configuration/NotificationsAppConfiguration.java b/juick-notifications/src/main/java/com/juick/components/configuration/NotificationsAppConfiguration.java index cb921fe5..5ccacf60 100644 --- a/juick-notifications/src/main/java/com/juick/components/configuration/NotificationsAppConfiguration.java +++ b/juick-notifications/src/main/java/com/juick/components/configuration/NotificationsAppConfiguration.java @@ -1,8 +1,8 @@ package com.juick.components.configuration; import com.juick.components.Notifications; -import com.juick.server.component.JuickServerComponent; -import com.juick.server.component.JuickServerReconnectManager; +import com.juick.components.service.JuickServerComponent; +import com.juick.components.service.JuickServerReconnectManager; import com.juick.server.configuration.BaseWebConfiguration; import org.springframework.context.annotation.*; import org.springframework.scheduling.annotation.EnableScheduling; diff --git a/juick-notifications/src/main/java/com/juick/components/service/JuickServerComponent.java b/juick-notifications/src/main/java/com/juick/components/service/JuickServerComponent.java new file mode 100644 index 00000000..eecddc70 --- /dev/null +++ b/juick-notifications/src/main/java/com/juick/components/service/JuickServerComponent.java @@ -0,0 +1,75 @@ +package com.juick.components.service; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.juick.server.component.DisconnectedEvent; +import com.juick.server.component.MessageEvent; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.context.ApplicationEventPublisher; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Component; +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 javax.annotation.PostConstruct; +import javax.annotation.PreDestroy; +import javax.inject.Inject; +import java.io.IOException; +import java.util.concurrent.atomic.AtomicBoolean; + +@Component +public class JuickServerComponent extends TextWebSocketHandler { + private static Logger logger = LoggerFactory.getLogger(JuickServerComponent.class); + @Inject + private ApplicationEventPublisher applicationEventPublisher; + @Inject + private ObjectMapper jsonMapper; + + private WebSocketSession session; + private final AtomicBoolean closeFlag = new AtomicBoolean(false); + + @PostConstruct + public void init() { + closeFlag.set(false); + } + @Override + public void afterConnectionEstablished(WebSocketSession session) throws Exception { + logger.info("WebSocket connected"); + this.session = session; + } + + @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)); + } + + @Override + protected void handleTextMessage(WebSocketSession session, TextMessage text) throws Exception { + com.juick.Message jmsg = jsonMapper.readValue(text.asBytes(), com.juick.Message.class); + + if (logger.isInfoEnabled()) // prevent writeValueAsString execution if logger disabled + logger.info("got jmsg: {}", jsonMapper.writeValueAsString(jmsg)); + if (!closeFlag.get()) { + applicationEventPublisher.publishEvent(new MessageEvent(this, jmsg)); + } + } + + @Scheduled(fixedRate = 30000, initialDelay = 30000) + public void ping() throws IOException { + if (session != null && session.isOpen()) { + logger.debug("Sending WebSocket ping"); + session.sendMessage(new PingMessage()); + } else if (!closeFlag.get()) { + applicationEventPublisher.publishEvent(new DisconnectedEvent(this)); + } + } + @PreDestroy + public void close() { + closeFlag.set(true); + } + +} diff --git a/juick-notifications/src/main/java/com/juick/components/service/JuickServerReconnectManager.java b/juick-notifications/src/main/java/com/juick/components/service/JuickServerReconnectManager.java new file mode 100644 index 00000000..f83a3aa0 --- /dev/null +++ b/juick-notifications/src/main/java/com/juick/components/service/JuickServerReconnectManager.java @@ -0,0 +1,23 @@ +package com.juick.components.service; + +import com.juick.server.component.DisconnectedEvent; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.context.ApplicationListener; +import org.springframework.stereotype.Component; +import org.springframework.web.socket.client.WebSocketConnectionManager; + +import javax.inject.Inject; + +@Component +public class JuickServerReconnectManager implements ApplicationListener { + private static Logger logger = LoggerFactory.getLogger(JuickServerReconnectManager.class); + @Inject + private WebSocketConnectionManager webSocketConnectionManager; + @Override + public void onApplicationEvent(DisconnectedEvent event) { + logger.info("retrying..."); + webSocketConnectionManager.stop(); + webSocketConnectionManager.start(); + } +} diff --git a/juick-server/src/main/java/com/juick/server/WebsocketManager.java b/juick-server/src/main/java/com/juick/server/WebsocketManager.java index 00d6dd28..b63377aa 100644 --- a/juick-server/src/main/java/com/juick/server/WebsocketManager.java +++ b/juick-server/src/main/java/com/juick/server/WebsocketManager.java @@ -34,6 +34,7 @@ import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.http.HttpHeaders; import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Component; import org.springframework.web.socket.CloseStatus; import org.springframework.web.socket.PingMessage; import org.springframework.web.socket.TextMessage; @@ -52,6 +53,7 @@ import java.util.List; /** * Created by vitalyster on 28.06.2016. */ +@Component public class WebsocketManager extends TextWebSocketHandler { private static final Logger logger = LoggerFactory.getLogger(WebsocketManager.class); diff --git a/juick-server/src/main/java/com/juick/server/configuration/ApiAppConfiguration.java b/juick-server/src/main/java/com/juick/server/configuration/ApiAppConfiguration.java index 9d31c247..1ca0130f 100644 --- a/juick-server/src/main/java/com/juick/server/configuration/ApiAppConfiguration.java +++ b/juick-server/src/main/java/com/juick/server/configuration/ApiAppConfiguration.java @@ -21,26 +21,19 @@ import com.juick.configuration.DataConfiguration; import com.juick.server.WebsocketManager; import com.juick.server.api.rss.MessagesView; import com.juick.server.api.rss.RepliesView; -import com.juick.server.component.JuickServerComponent; -import com.juick.service.UserService; -import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; import org.springframework.scheduling.annotation.EnableAsync; import org.springframework.scheduling.annotation.EnableScheduling; -import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import org.springframework.web.servlet.view.BeanNameViewResolver; import org.springframework.web.servlet.view.feed.AbstractRssFeedView; -import org.springframework.web.socket.client.WebSocketConnectionManager; -import org.springframework.web.socket.client.standard.StandardWebSocketClient; import org.springframework.web.socket.config.annotation.EnableWebSocket; import org.springframework.web.socket.config.annotation.WebSocketConfigurer; import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry; import org.springframework.web.socket.server.standard.ServletServerContainerFactoryBean; -import org.springframework.web.util.UriComponentsBuilder; import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.service.ApiInfo; @@ -64,23 +57,7 @@ import java.util.Collections; @ComponentScan(basePackages = "com.juick.server") public class ApiAppConfiguration implements WebMvcConfigurer, WebSocketConfigurer { @Inject - UserService userService; - @Value("${api_user:juick}") - private String serviceUser; - @Value("${websocket_url:ws://localhost:8080/ws/}") - private String baseUri; - @Inject - public JuickServerComponent juickServerComponent; - @Bean - public WebSocketConnectionManager connectionManager() { - String websocketURI = UriComponentsBuilder.fromUriString(baseUri) - .queryParam("hash", userService.getHashByUID(userService.getUIDbyName(serviceUser))).build().toUriString(); - return new WebSocketConnectionManager(client(), juickServerComponent, websocketURI); - } - @Bean - public StandardWebSocketClient client() { - return new StandardWebSocketClient(); - } + WebsocketManager websocketManager; @Bean public Docket api() { return new Docket(DocumentationType.SWAGGER_2) @@ -90,15 +67,11 @@ public class ApiAppConfiguration implements WebMvcConfigurer, WebSocketConfigure "2.0", "https://juick.com/help/tos", null, "AGPLv3", "https://www.gnu.org/licenses/agpl-3.0.html", Collections.emptyList())); } - @Bean - public WebsocketManager wsHandler() { - return new WebsocketManager(); - } @Override public void registerWebSocketHandlers(@Nonnull WebSocketHandlerRegistry registry) { //((ServletWebSocketHandlerRegistry) registry).setOrder(Ordered.LOWEST_PRECEDENCE); - registry.addHandler(wsHandler(), "/ws/**").setAllowedOrigins("*"); + registry.addHandler(websocketManager, "/ws/**").setAllowedOrigins("*"); } @Bean @@ -120,9 +93,4 @@ public class ApiAppConfiguration implements WebMvcConfigurer, WebSocketConfigure AbstractRssFeedView repliesView() { return new RepliesView(); } - @Override - public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) { - configurer.enable(); - } - } -- cgit v1.2.3