我正在使用Hazelcast开发Web套接字应用程序来分享在线用户的状态。一切正常,除了一个应用程序实例关闭或重新启动时,连接到该实例的所有用户都断开连接,afterConnectionClosed
MessagingHandler
扩展BinaryWebSocketHandler
。在afterConnectionClosed
中,将更新连接到当前节点的用户的状态,并且这些状态位于Hazelcast中。因此,当它尝试从Hazelcast中删除状态时,会出现以下错误:
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)
我正在使用以下属性禁用默认关闭:
config.setProperty(GroupProperty.SHUTDOWNHOOK_ENABLED.getName(), "false");
config.setProperty(GroupProperty.SHUTDOWNHOOK_POLICY.getName(), "GRACEFUL");
但是在用户断开连接之前它仍然处于关闭状态。有没有办法在应用程序结束时使用hazelcast关闭方式配置hazelcast?
答案 0 :(得分:4)
我一直在尝试不同的方法来处理spring websocket连接的正常关闭,这也修改了在线用户的hazelcast映射,但没有一个工作。我终于使用了SmartLifecycle
接口。可以找到此课程的文档here。
这是我用于正确关机的代码。是的,在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;
}
}
答案 1 :(得分:1)
@ nisheeth-shah,它更像是与Spring有关的问题,因为它的Spring决定了bean的关闭顺序。
您可以做什么,您可以使用MessageHandler
注释对@DependsOn
bean进行注释,并给出HazelcastInstance
bean的名称,如@DependsOn("hazelcastInstance")
。有关说明,请参阅此link。基本上,Spring会在创建HazelcastInstance
之前启动MessageHandler
,并且在HazelcastInstance
bean被销毁之前不会关闭MessageHandler
。
另外,不要禁用Hazelcast关机钩。
答案 2 :(得分:0)
您可以在hazelcastInstance.shutdown()
中使用@PreDestroy
即
@PreDestroy
public void shutDownHazelcast(){
hazelcastInstance.shutdown()
}
这会优雅地关闭hazelcast实例