How to disable default Hazelcast download on spring boot

I am developing a web socket application using Hazelcast to exchange online user status. Everything works fine, except that when one of the application instances is restarted or restarted, all users connected to this instance are disconnected and afterConnectionClosed from the MessagingHandler , which extends BinaryWebSocketHandler . afterConnectionClosed will update the status of users connected to the current node, and these statuses are in Hazelcast. Therefore, when he tries to remove the status from Hazelcast, he gives the following error:

 com.hazelcast.core.HazelcastInstanceNotActiveException: State: SHUT_DOWN Operation: class com.hazelcast.map.impl.operation.RemoveOperation at com.hazelcast.spi.impl.operationservice.impl.Invocation.engineActive(Invocation.java:490) at com.hazelcast.spi.impl.operationservice.impl.Invocation.doInvoke(Invocation.java:523) at com.hazelcast.spi.impl.operationservice.impl.Invocation.invoke0(Invocation.java:513) at com.hazelcast.spi.impl.operationservice.impl.Invocation.invoke(Invocation.java:207) at com.hazelcast.spi.impl.operationservice.impl.InvocationBuilderImpl.invoke(InvocationBuilderImpl.java:60) at com.hazelcast.map.impl.proxy.MapProxySupport.invokeOperation(MapProxySupport.java:423) at com.hazelcast.map.impl.proxy.MapProxySupport.removeInternal(MapProxySupport.java:563) at com.hazelcast.map.impl.proxy.MapProxyImpl.remove(MapProxyImpl.java:207) at com.nisheeth.spring.MessageHandler.afterConnectionClosed(MessageHandler.java:57) at org.springframework.web.socket.handler.WebSocketHandlerDecorator.afterConnectionClosed(WebSocketHandlerDecorator.java:85) at org.springframework.web.socket.handler.LoggingWebSocketHandlerDecorator.afterConnectionClosed(LoggingWebSocketHandlerDecorator.java:72) at org.springframework.web.socket.handler.ExceptionWebSocketHandlerDecorator.afterConnectionClosed(ExceptionWebSocketHandlerDecorator.java:78) at org.springframework.web.socket.adapter.standard.StandardWebSocketHandlerAdapter.onClose(StandardWebSocketHandlerAdapter.java:141) at org.apache.tomcat.websocket.WsSession.fireEndpointOnClose(WsSession.java:535) at org.apache.tomcat.websocket.WsSession.doClose(WsSession.java:481) at org.apache.tomcat.websocket.WsSession.close(WsSession.java:445) at org.apache.tomcat.websocket.WsWebSocketContainer.destroy(WsWebSocketContainer.java:960) at org.apache.tomcat.websocket.server.WsContextListener.contextDestroyed(WsContextListener.java:48) at org.apache.catalina.core.StandardContext.listenerStop(StandardContext.java:4792) at org.apache.catalina.core.StandardContext.stopInternal(StandardContext.java:5429) at org.apache.catalina.util.LifecycleBase.stop(LifecycleBase.java:226) at org.apache.catalina.core.ContainerBase$StopChild.call(ContainerBase.java:1435) at org.apache.catalina.core.ContainerBase$StopChild.call(ContainerBase.java:1424) at java.util.concurrent.FutureTask.run(Unknown Source) at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) at java.lang.Thread.run(Unknown Source) at ------ submitted from ------.(Unknown Source) at com.hazelcast.spi.impl.operationservice.impl.InvocationFuture.resolve(InvocationFuture.java:127) at com.hazelcast.spi.impl.operationservice.impl.InvocationFuture.resolveAndThrowIfException(InvocationFuture.java:79) at com.hazelcast.spi.impl.AbstractInvocationFuture.get(AbstractInvocationFuture.java:147) at com.hazelcast.map.impl.proxy.MapProxySupport.invokeOperation(MapProxySupport.java:424) at com.hazelcast.map.impl.proxy.MapProxySupport.removeInternal(MapProxySupport.java:563) at com.hazelcast.map.impl.proxy.MapProxyImpl.remove(MapProxyImpl.java:207) at com.nisheeth.spring.MessageHandler.afterConnectionClosed(MessageHandler.java:57) at org.springframework.web.socket.handler.WebSocketHandlerDecorator.afterConnectionClosed(WebSocketHandlerDecorator.java:85) at org.springframework.web.socket.handler.LoggingWebSocketHandlerDecorator.afterConnectionClosed(LoggingWebSocketHandlerDecorator.java:72) at org.springframework.web.socket.handler.ExceptionWebSocketHandlerDecorator.afterConnectionClosed(ExceptionWebSocketHandlerDecorator.java:78) at org.springframework.web.socket.adapter.standard.StandardWebSocketHandlerAdapter.onClose(StandardWebSocketHandlerAdapter.java:141) at org.apache.tomcat.websocket.WsSession.fireEndpointOnClose(WsSession.java:535) at org.apache.tomcat.websocket.WsSession.doClose(WsSession.java:481) at org.apache.tomcat.websocket.WsSession.close(WsSession.java:445) at org.apache.tomcat.websocket.WsWebSocketContainer.destroy(WsWebSocketContainer.java:960) at org.apache.tomcat.websocket.server.WsContextListener.contextDestroyed(WsContextListener.java:48) at org.apache.catalina.core.StandardContext.listenerStop(StandardContext.java:4792) at org.apache.catalina.core.StandardContext.stopInternal(StandardContext.java:5429) at org.apache.catalina.util.LifecycleBase.stop(LifecycleBase.java:226) at org.apache.catalina.core.ContainerBase$StopChild.call(ContainerBase.java:1435) at org.apache.catalina.core.ContainerBase$StopChild.call(ContainerBase.java:1424) at java.util.concurrent.FutureTask.run(Unknown Source) at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) at java.lang.Thread.run(Unknown Source) 

I will disable the default disconnect using the following properties:

 config.setProperty(GroupProperty.SHUTDOWNHOOK_ENABLED.getName(), "false"); config.setProperty(GroupProperty.SHUTDOWNHOOK_POLICY.getName(), "GRACEFUL"); 

But still, it still disconnects before users disconnect. Is there a way to configure LET so that the hazel is turned off at the end of the application?

+5
source share
3 answers

I am trying to use different methods to handle the graceful closing of a spring connection to a web socket, which also changes the map of online users with an illustration map, but no one worked. Finally, I used the SmartLifecycle interface. The documentation for this class can be found here .

Here is the code I used to shutdown properly. And yes, it is called before disabling Hazelcast:

 @Component public class AppLifecycle implements SmartLifecycle { private Logger log = LoggerFactory.getLogger(AppLifecycle.class); @Autowired private SampleService sampleService; @Override public boolean isAutoStartup() { log.debug("=========================auto startup========================="); return true; } @Override public void stop(Runnable runnable) { sampleService.getSessionMap().forEach((key, session) -> { try { session.close(CloseStatus.SERVICE_RESTARTED); log.debug("disconnecting : {}", key); } catch (IOException e) { log.error(e.getMessage(), e); } }); log.debug("=========================stop runnable========================="); new Thread(runnable).start(); } @Override public void start() { log.debug("=========================start========================="); } @Override public void stop() { log.debug("=========================stop========================="); } @Override public boolean isRunning() { return true; } @Override public int getPhase() { return Integer.MAX_VALUE; } } 
+4
source

@ nisheeth-shah, This is more like a problem with Spring, as it is Spring, which decides how to turn off beans.

What you can do, you can annotate your MessageHandler bean with @DependsOn annotation and specify the name HazelcastInstance bean, like this @DependsOn("hazelcastInstance") . See the Link for an explanation. Basically, Spring will start the HazelcastInstance before creating the MessageHandler and won't disable the HazelcastInstance before the MessageHandler bean.

Also, do not disable the Hazelcast shutdown hook.

+1
source

You can use hazelcastInstance.shutdown() in @PreDestroy i.e.

 @PreDestroy public void shutDownHazelcast(){ hazelcastInstance.shutdown() } 

This will allow you to gracefully close the carousel instance.

0
source

Source: https://habr.com/ru/post/1275990/


All Articles