问题
I'm writing a test application using web sockets. Sometimes after a re-connection, Spring Boot v1.5.10.RELEASE does not find a session associated with the user. This problem not reproducible constantly.
Test:
@Test
public void receiveInCycle() throws Exception {
for (int i = 0; i < 9999; i++) {
receive();
}
}
public void receive() throws Exception {
Collection<Object> payloads = new ArrayList<>();
//create session for "kate" user
StompSession stompSession = createStompSession("kate");
StompSession.Subscription subscription = stompSession.subscribe("/user/queue/messages", new StompSessionHandlerAdapter() {
@Override
public void handleFrame(StompHeaders headers, Object payload) {
//save received payloads to collection
payloads.add(payload);
}
});
Thread.sleep(1000);
subscription.unsubscribe();
stompSession.disconnect();
//collection should not be empty since i send events every 100 millis
assertTrue(!payloads.isEmpty());
}
Сode that sends the messages:
@Scheduled(fixedDelay = 100)
public void send() {
messagingTemplate.convertAndSendToUser("kate",
"/queue/messages",
format("Private UUID - '%s'!", UUID.randomUUID().toString()));
}
Also in the logs, I always see the error
java.lang.IllegalStateException: Message will not be sent because the WebSocket session has been closed
but its frequency is not related to the number of non-working tests.
I also noticed that Spring can't find a session for a user, despite the user is connected and subscribed.
Logs - spring can't find session:
o.s.m.s.u.UserDestinationMessageHandler : No active sessions for user destination: /user/kate/queue/messages
Logs - user connected and subscribed:
2018-02-15 12:04:21.800 TRACE 15876 --- [-auto-1-exec-10] o.s.w.s.m.StompSubProtocolHandler : From client: CONNECT session=9
2018-02-15 12:04:21.801 DEBUG 15876 --- [nboundChannel-2] o.s.m.s.b.SimpleBrokerMessageHandler : Processing CONNECT user=kate session=9
2018-02-15 12:04:21.803 TRACE 15876 --- [-auto-1-exec-10] o.s.w.s.m.StompSubProtocolHandler : From client: SUBSCRIBE /user/queue/messages id=0 session=9
2018-02-15 12:04:21.803 DEBUG 15876 --- [nboundChannel-5] o.s.m.s.b.SimpleBrokerMessageHandler : Processing SUBSCRIBE /user/queue/messages id=0 session=9
After debugging, I found out that the error occurs because the user is not added to DefaultSimpUserRegistry (it stores the association between the session and the user), but I could not figure out why. I tried use Jetty instead of Tomcat and everything works fine.
Is there any explanation for this behavior?
Full log:
2018-02-15 12:04:21.797 TRACE 15876 --- [-auto-1-exec-10] o.s.w.s.s.s.DefaultHandshakeHandler : Processing request http://localhost:43755/test-server with headers={authorization=[kate], sec-websocket-key=[xZ67EfJb278L2ERbzCmUDw==], connection=[upgrade], sec-websocket-version=[13], host=[localhost:43755], upgrade=[websocket]}
2018-02-15 12:04:21.797 TRACE 15876 --- [-auto-1-exec-10] o.s.w.s.s.s.DefaultHandshakeHandler : Upgrading to WebSocket, subProtocol=null, extensions=[]
2018-02-15 12:04:21.799 TRACE 15876 --- [cTaskExecutor-1] o.s.w.s.c.s.StandardWebSocketClient : Handshake response headers: {date=[Thu, 15 Feb 2018 09:04:21 GMT], upgrade=[websocket], connection=[upgrade], sec-websocket-accept=[d7OI16/H5uovwY7a+QxLbuZKuaY=]}
2018-02-15 12:04:21.799 DEBUG 15876 --- [cTaskExecutor-1] o.s.m.simp.stomp.DefaultStompSession : Connection established in session id=bd60b291-eadd-c8f6-5b38-060d2318debd
2018-02-15 12:04:21.800 TRACE 15876 --- [-auto-1-exec-10] o.s.w.s.m.StompSubProtocolHandler : From client: CONNECT session=9
2018-02-15 12:04:21.801 DEBUG 15876 --- [nboundChannel-2] o.s.m.s.b.SimpleBrokerMessageHandler : Processing CONNECT user=kate session=9
2018-02-15 12:04:21.803 TRACE 15876 --- [-auto-1-exec-10] o.s.w.s.m.StompSubProtocolHandler : From client: SUBSCRIBE /user/queue/messages id=0 session=9
2018-02-15 12:04:21.803 DEBUG 15876 --- [nboundChannel-5] o.s.m.s.b.SimpleBrokerMessageHandler : Processing SUBSCRIBE /user/queue/messages id=0 session=9
2018-02-15 12:04:21.803 TRACE 15876 --- [nboundChannel-5] o.s.m.s.u.UserDestinationMessageHandler : Translated /user/queue/messages -> [/queue/messages-user9]
2018-02-15 12:04:21.803 DEBUG 15876 --- [nboundChannel-5] o.s.m.s.b.SimpleBrokerMessageHandler : Processing SUBSCRIBE destination=/queue/messages-user9 subscriptionId=0 session=9 user=kate payload=byte[0]
2018-02-15 12:04:21.826 DEBUG 15876 --- [MessageBroker-4] o.s.m.s.b.SimpleBrokerMessageHandler : Processing MESSAGE destination=/user/kate/queue/messages session=null payload=Private UUID - 'd890e685-9049-4773-94a1-f1334da176eb'!
2018-02-15 12:04:21.826 TRACE 15876 --- [MessageBroker-4] o.s.m.s.u.UserDestinationMessageHandler : Translated /user/kate/queue/messages -> [/queue/messages-user7]
2018-02-15 12:04:21.826 DEBUG 15876 --- [MessageBroker-4] o.s.m.s.b.SimpleBrokerMessageHandler : Processing MESSAGE destination=/queue/messages-user7 session=null payload=Private UUID - 'd890e685-9049-4773-94a1-f1334da176eb'!
2018-02-15 12:04:21.833 TRACE 15876 --- [o-auto-1-exec-1] o.s.w.s.m.StompSubProtocolHandler : From client: DISCONNECT session=7
2018-02-15 12:04:21.833 DEBUG 15876 --- [nboundChannel-3] o.s.m.s.b.SimpleBrokerMessageHandler : Processing DISCONNECT session=7
2018-02-15 12:04:21.834 DEBUG 15876 --- [o-auto-1-exec-1] o.s.w.s.m.SubProtocolWebSocketHandler : Clearing session 7
2018-02-15 12:04:21.834 DEBUG 15876 --- [nboundChannel-2] o.s.m.s.b.SimpleBrokerMessageHandler : Processing DISCONNECT session=7
2018-02-15 12:04:21.834 DEBUG 15876 --- [tboundChannel-6] o.s.w.s.m.SubProtocolWebSocketHandler : No session for GenericMessage [payload=byte[0], headers={simpMessageType=DISCONNECT_ACK, simpDisconnectMessage=GenericMessage [payload=byte[0], headers={simpMessageType=DISCONNECT, stompCommand=DISCONNECT, simpSessionAttributes={org.springframework.messaging.simp.SimpAttributes.COMPLETED=true}, simpUser=com.example.sockettesting.configuration.WebSocketConfiguration$1$$Lambda$12/293071828@319d0266, simpSessionId=7}], simpUser=com.example.sockettesting.configuration.WebSocketConfiguration$1$$Lambda$12/293071828@319d0266, simpSessionId=7}]
2018-02-15 12:04:21.834 DEBUG 15876 --- [tboundChannel-5] o.s.w.s.m.StompSubProtocolHandler : Failed to send WebSocket message to client in session 7
java.lang.IllegalStateException: Message will not be sent because the WebSocket session has been closed
at org.apache.tomcat.websocket.WsRemoteEndpointImplBase.writeMessagePart(WsRemoteEndpointImplBase.java:425) ~[tomcat-embed-websocket-8.5.27.jar:8.5.27]
at org.apache.tomcat.websocket.WsRemoteEndpointImplBase.sendMessageBlock(WsRemoteEndpointImplBase.java:309) ~[tomcat-embed-websocket-8.5.27.jar:8.5.27]
at org.apache.tomcat.websocket.WsRemoteEndpointImplBase.sendMessageBlock(WsRemoteEndpointImplBase.java:250) ~[tomcat-embed-websocket-8.5.27.jar:8.5.27]
at org.apache.tomcat.websocket.WsRemoteEndpointImplBase.sendPartialString(WsRemoteEndpointImplBase.java:223) ~[tomcat-embed-websocket-8.5.27.jar:8.5.27]
at org.apache.tomcat.websocket.WsRemoteEndpointBasic.sendText(WsRemoteEndpointBasic.java:49) ~[tomcat-embed-websocket-8.5.27.jar:8.5.27]
at org.springframework.web.socket.adapter.standard.StandardWebSocketSession.sendTextMessage(StandardWebSocketSession.java:203) ~[spring-websocket-4.3.14.RELEASE.jar:4.3.14.RELEASE]
at org.springframework.web.socket.adapter.AbstractWebSocketSession.sendMessage(AbstractWebSocketSession.java:101) ~[spring-websocket-4.3.14.RELEASE.jar:4.3.14.RELEASE]
at org.springframework.web.socket.handler.ConcurrentWebSocketSessionDecorator.tryFlushMessageBuffer(ConcurrentWebSocketSessionDecorator.java:153) ~[spring-websocket-4.3.14.RELEASE.jar:4.3.14.RELEASE]
at org.springframework.web.socket.handler.ConcurrentWebSocketSessionDecorator.sendMessage(ConcurrentWebSocketSessionDecorator.java:125) ~[spring-websocket-4.3.14.RELEASE.jar:4.3.14.RELEASE]
at org.springframework.web.socket.messaging.StompSubProtocolHandler.sendToClient(StompSubProtocolHandler.java:471) [spring-websocket-4.3.14.RELEASE.jar:4.3.14.RELEASE]
at org.springframework.web.socket.messaging.StompSubProtocolHandler.handleMessageToClient(StompSubProtocolHandler.java:458) [spring-websocket-4.3.14.RELEASE.jar:4.3.14.RELEASE]
at org.springframework.web.socket.messaging.SubProtocolWebSocketHandler.handleMessage(SubProtocolWebSocketHandler.java:337) [spring-websocket-4.3.14.RELEASE.jar:4.3.14.RELEASE]
at org.springframework.messaging.support.ExecutorSubscribableChannel$SendTask.run(ExecutorSubscribableChannel.java:135) [spring-messaging-4.3.14.RELEASE.jar:4.3.14.RELEASE]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [na:1.8.0_151]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [na:1.8.0_151]
at java.lang.Thread.run(Thread.java:748) [na:1.8.0_151]
2018-02-15 12:04:21.928 DEBUG 15876 --- [MessageBroker-4] o.s.m.s.b.SimpleBrokerMessageHandler : Processing MESSAGE destination=/user/kate/queue/messages session=null payload=Private UUID - '3a223606-3fec-4def-a194-0ac35e17b757'!
2018-02-15 12:04:21.928 TRACE 15876 --- [MessageBroker-4] o.s.m.s.u.UserDestinationMessageHandler : No active sessions for user destination: /user/kate/queue/messages
2018-02-15 12:04:22.028 DEBUG 15876 --- [MessageBroker-2] o.s.m.s.b.SimpleBrokerMessageHandler : Processing MESSAGE destination=/user/kate/queue/messages session=null payload=Private UUID - '9213c267-d2c8-44a7-bdc8-5f300220eb31'!
2018-02-15 12:04:22.028 TRACE 15876 --- [MessageBroker-2] o.s.m.s.u.UserDestinationMessageHandler : No active sessions for user destination: /user/kate/queue/messages
2018-02-15 12:04:22.129 DEBUG 15876 --- [MessageBroker-1] o.s.m.s.b.SimpleBrokerMessageHandler : Processing MESSAGE destination=/user/kate/queue/messages session=null payload=Private UUID - '196a612b-7689-40e7-b4b3-5819f88844ad'!
2018-02-15 12:04:22.130 TRACE 15876 --- [MessageBroker-1] o.s.m.s.u.UserDestinationMessageHandler : No active sessions for user destination: /user/kate/queue/messages
2018-02-15 12:04:22.230 DEBUG 15876 --- [MessageBroker-3] o.s.m.s.b.SimpleBrokerMessageHandler : Processing MESSAGE destination=/user/kate/queue/messages session=null payload=Private UUID - 'a4bfb498-aabc-4641-80bb-33a763fb165e'!
2018-02-15 12:04:22.231 TRACE 15876 --- [MessageBroker-3] o.s.m.s.u.UserDestinationMessageHandler : No active sessions for user destination: /user/kate/queue/messages
2018-02-15 12:04:22.332 DEBUG 15876 --- [MessageBroker-3] o.s.m.s.b.SimpleBrokerMessageHandler : Processing MESSAGE destination=/user/kate/queue/messages session=null payload=Private UUID - '37c6a440-2797-4453-9016-2ad1247eed14'!
2018-02-15 12:04:22.332 TRACE 15876 --- [MessageBroker-3] o.s.m.s.u.UserDestinationMessageHandler : No active sessions for user destination: /user/kate/queue/messages
2018-02-15 12:04:22.433 DEBUG 15876 --- [MessageBroker-3] o.s.m.s.b.SimpleBrokerMessageHandler : Processing MESSAGE destination=/user/kate/queue/messages session=null payload=Private UUID - 'f333d450-f04f-4e0f-84df-0e310a993938'!
2018-02-15 12:04:22.433 TRACE 15876 --- [MessageBroker-3] o.s.m.s.u.UserDestinationMessageHandler : No active sessions for user destination: /user/kate/queue/messages
2018-02-15 12:04:22.533 DEBUG 15876 --- [MessageBroker-3] o.s.m.s.b.SimpleBrokerMessageHandler : Processing MESSAGE destination=/user/kate/queue/messages session=null payload=Private UUID - '95291a0a-d608-48ba-8f8e-012d64e825ef'!
2018-02-15 12:04:22.534 TRACE 15876 --- [MessageBroker-3] o.s.m.s.u.UserDestinationMessageHandler : No active sessions for user destination: /user/kate/queue/messages
2018-02-15 12:04:22.634 DEBUG 15876 --- [MessageBroker-3] o.s.m.s.b.SimpleBrokerMessageHandler : Processing MESSAGE destination=/user/kate/queue/messages session=null payload=Private UUID - 'da26c02e-d841-4215-aa00-b42999c30a1b'!
2018-02-15 12:04:22.635 TRACE 15876 --- [MessageBroker-3] o.s.m.s.u.UserDestinationMessageHandler : No active sessions for user destination: /user/kate/queue/messages
2018-02-15 12:04:22.736 DEBUG 15876 --- [MessageBroker-3] o.s.m.s.b.SimpleBrokerMessageHandler : Processing MESSAGE destination=/user/kate/queue/messages session=null payload=Private UUID - '6c6732a2-9598-46cf-9797-113347965165'!
2018-02-15 12:04:22.736 TRACE 15876 --- [MessageBroker-3] o.s.m.s.u.UserDestinationMessageHandler : No active sessions for user destination: /user/kate/queue/messages
2018-02-15 12:04:22.803 DEBUG 15876 --- [ main] o.s.m.simp.stomp.DefaultStompSession : Connection closed in session id=bd60b291-eadd-c8f6-5b38-060d2318debd
2018-02-15 12:04:22.804 TRACE 15876 --- [o-auto-1-exec-2] o.s.w.s.m.StompSubProtocolHandler : From client: UNSUBSCRIBE id=0 session=9
2018-02-15 12:04:22.804 DEBUG 15876 --- [nboundChannel-4] o.s.m.s.b.SimpleBrokerMessageHandler : Processing UNSUBSCRIBE id=0 session=9
2018-02-15 12:04:22.804 TRACE 15876 --- [o-auto-1-exec-2] o.s.w.s.m.StompSubProtocolHandler : From client: DISCONNECT session=9
2018-02-15 12:04:22.804 DEBUG 15876 --- [nboundChannel-1] o.s.m.s.b.SimpleBrokerMessageHandler : Processing DISCONNECT session=9
2018-02-15 12:04:22.805 DEBUG 15876 --- [o-auto-1-exec-2] o.s.w.s.m.SubProtocolWebSocketHandler : Clearing session 9
2018-02-15 12:04:22.805 DEBUG 15876 --- [nboundChannel-5] o.s.m.s.b.SimpleBrokerMessageHandler : Processing DISCONNECT session=9
2018-02-15 12:04:22.806 DEBUG 15876 --- [tboundChannel-8] o.s.w.s.m.SubProtocolWebSocketHandler : No session for GenericMessage [payload=byte[0], headers={simpMessageType=DISCONNECT_ACK, simpDisconnectMessage=GenericMessage [payload=byte[0], headers={simpMessageType=DISCONNECT, stompCommand=DISCONNECT, simpSessionAttributes={org.springframework.messaging.simp.SimpAttributes.COMPLETED=true}, simpHeartbeat=[J@47a1a4bc, simpUser=com.example.sockettesting.configuration.WebSocketConfiguration$1$$Lambda$12/293071828@319d0266, simpSessionId=9}], simpUser=com.example.sockettesting.configuration.WebSocketConfiguration$1$$Lambda$12/293071828@319d0266, simpSessionId=9}]
2018-02-15 12:04:22.807 DEBUG 15876 --- [tboundChannel-8] o.s.w.s.m.SubProtocolWebSocketHandler : No session for GenericMessage [payload=byte[0], headers={simpMessageType=DISCONNECT_ACK, simpDisconnectMessage=GenericMessage [payload=byte[0], headers={simpMessageType=DISCONNECT, stompCommand=DISCONNECT, simpSessionAttributes={org.springframework.messaging.simp.SimpAttributes.COMPLETED=true}, simpUser=com.example.sockettesting.configuration.WebSocketConfiguration$1$$Lambda$12/293071828@319d0266, simpSessionId=9}], simpUser=com.example.sockettesting.configuration.WebSocketConfiguration$1$$Lambda$12/293071828@319d0266, simpSessionId=9}]
来源:https://stackoverflow.com/questions/48804780/spring-boot-no-active-sessions-for-user-destination-after-reconnect