diff options
author | Alexander Alexeev | 2017-11-02 20:17:56 +0700 |
---|---|---|
committer | Vitaly Takmazov | 2017-11-03 09:51:06 +0300 |
commit | 400f4d6d26cac27e24e3c783a8f84b84811e5e47 (patch) | |
tree | c521877a45cfad38ee180e17bec3f9777f86702f | |
parent | 8a8683bc35583fed27b6562907a66bd171f8e778 (diff) |
close() implemented for correct componen shutdown
-rw-r--r-- | juick-server-web/src/main/java/com/juick/server/component/JuickServerComponent.java | 17 |
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 96b8c398a..71e81f2d7 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); + } + } |