aboutsummaryrefslogtreecommitdiff
path: root/juick-server-web/src/main/java/com/juick/server/component
diff options
context:
space:
mode:
authorGravatar Alexander Alexeev2017-11-02 20:17:56 +0700
committerGravatar Vitaly Takmazov2017-11-03 09:51:06 +0300
commit400f4d6d26cac27e24e3c783a8f84b84811e5e47 (patch)
treec521877a45cfad38ee180e17bec3f9777f86702f /juick-server-web/src/main/java/com/juick/server/component
parent8a8683bc35583fed27b6562907a66bd171f8e778 (diff)
close() implemented for correct componen shutdown
Diffstat (limited to 'juick-server-web/src/main/java/com/juick/server/component')
-rw-r--r--juick-server-web/src/main/java/com/juick/server/component/JuickServerComponent.java17
1 files changed, 16 insertions, 1 deletions
diff --git a/juick-server-web/src/main/java/com/juick/server/component/JuickServerComponent.java b/juick-server-web/src/main/java/com/juick/server/component/JuickServerComponent.java
index 96b8c398..71e81f2d 100644
--- a/juick-server-web/src/main/java/com/juick/server/component/JuickServerComponent.java
+++ b/juick-server-web/src/main/java/com/juick/server/component/JuickServerComponent.java
@@ -11,8 +11,11 @@ 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);
@@ -22,7 +25,12 @@ public class JuickServerComponent extends TextWebSocketHandler {
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");
@@ -41,7 +49,9 @@ public class JuickServerComponent extends TextWebSocketHandler {
if (logger.isInfoEnabled()) // prevent writeValueAsString execution if logger disabled
logger.info("got jmsg: {}", jsonMapper.writeValueAsString(jmsg));
- applicationEventPublisher.publishEvent(new MessageEvent(this, jmsg));
+ if (!closeFlag.get()) {
+ applicationEventPublisher.publishEvent(new MessageEvent(this, jmsg));
+ }
}
@Scheduled(fixedRate = 30000, initialDelay = 30000)
@@ -53,4 +63,9 @@ public class JuickServerComponent extends TextWebSocketHandler {
applicationEventPublisher.publishEvent(new DisconnectedEvent(this));
}
}
+ @PreDestroy
+ public void close() {
+ closeFlag.set(true);
+ }
+
}