我正在使用网络套接字编写测试应用程序。 有时在重新连接后,Spring Boot v1.5.10.RELEASE找不到与用户关联的会话。这个问题不能经常重现。
测试:
e.Handled = true
发送消息的Сode:
@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());
}
同样在日志中,我总是看到错误
@Scheduled(fixedDelay = 100)
public void send() {
messagingTemplate.convertAndSendToUser("kate",
"/queue/messages",
format("Private UUID - '%s'!", UUID.randomUUID().toString()));
}
但其频率与非工作测试的数量无关。
我还注意到,尽管用户已连接并订阅,但Spring无法为用户找到会话。
日志 - 春天无法找到会话:
java.lang.IllegalStateException: Message will not be sent because the WebSocket session has been closed
日志 - 用户连接和订阅:
o.s.m.s.u.UserDestinationMessageHandler : No active sessions for user destination: /user/kate/queue/messages
调试后,我发现错误的发生是因为没有将用户添加到DefaultSimpUserRegistry(它存储会话和用户之间的关联),但我无法弄清楚原因。 我尝试使用Jetty而不是Tomcat,一切正常。
这种行为有什么解释吗?
完整日志:
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