From 9ef168a03b75aeca0c2f7dda9ce87d4014c703a9 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Fri, 16 Mar 2018 10:46:23 +0300 Subject: merge common projects --- .../juick/server/component/DisconnectedEvent.java | 14 +++++ .../server/component/JuickServerComponent.java | 71 ++++++++++++++++++++++ .../component/JuickServerReconnectManager.java | 22 +++++++ .../com/juick/server/component/MessageEvent.java | 22 +++++++ .../juick/server/component/UserUpdatedEvent.java | 23 +++++++ 5 files changed, 152 insertions(+) create mode 100644 juick-common/src/main/java/com/juick/server/component/DisconnectedEvent.java create mode 100644 juick-common/src/main/java/com/juick/server/component/JuickServerComponent.java create mode 100644 juick-common/src/main/java/com/juick/server/component/JuickServerReconnectManager.java create mode 100644 juick-common/src/main/java/com/juick/server/component/MessageEvent.java create mode 100644 juick-common/src/main/java/com/juick/server/component/UserUpdatedEvent.java (limited to 'juick-common/src/main/java/com/juick/server/component') diff --git a/juick-common/src/main/java/com/juick/server/component/DisconnectedEvent.java b/juick-common/src/main/java/com/juick/server/component/DisconnectedEvent.java new file mode 100644 index 00000000..9da6d7a9 --- /dev/null +++ b/juick-common/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-common/src/main/java/com/juick/server/component/JuickServerComponent.java b/juick-common/src/main/java/com/juick/server/component/JuickServerComponent.java new file mode 100644 index 00000000..792f446f --- /dev/null +++ b/juick-common/src/main/java/com/juick/server/component/JuickServerComponent.java @@ -0,0 +1,71 @@ +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.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; + +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 new file mode 100644 index 00000000..a662e4fb --- /dev/null +++ b/juick-common/src/main/java/com/juick/server/component/JuickServerReconnectManager.java @@ -0,0 +1,22 @@ +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-common/src/main/java/com/juick/server/component/MessageEvent.java b/juick-common/src/main/java/com/juick/server/component/MessageEvent.java new file mode 100644 index 00000000..048de6a6 --- /dev/null +++ b/juick-common/src/main/java/com/juick/server/component/MessageEvent.java @@ -0,0 +1,22 @@ +package com.juick.server.component; + +import com.juick.Message; +import org.springframework.context.ApplicationEvent; + +public class MessageEvent extends ApplicationEvent { + private Message message; + /** + * Create a new ApplicationEvent. + * + * @param source the object on which the event initially occurred (never {@code null}) + * @param message app message + */ + public MessageEvent(Object source, Message message) { + super(source); + this.message = message; + } + + public Message getMessage() { + return message; + } +} diff --git a/juick-common/src/main/java/com/juick/server/component/UserUpdatedEvent.java b/juick-common/src/main/java/com/juick/server/component/UserUpdatedEvent.java new file mode 100644 index 00000000..059aeefd --- /dev/null +++ b/juick-common/src/main/java/com/juick/server/component/UserUpdatedEvent.java @@ -0,0 +1,23 @@ +package com.juick.server.component; + +import com.juick.User; +import org.springframework.context.ApplicationEvent; +import org.springframework.lang.NonNull; + +public class UserUpdatedEvent extends ApplicationEvent { + private User user; + /** + * Generated when user is updated (avatar changed, etc). + * + * @param source the object on which the event initially occurred (never {@code null}) + * @param user updated user + */ + public UserUpdatedEvent(@NonNull Object source, User user) { + super(source); + this.user = user; + } + + public User getUser() { + return user; + } +} -- cgit v1.2.3