Вопрос

So this is a strange one. I have a basic Spring 4 websockets application running on Glassfish 4 using RabbitMQ for the message broker, nothing fancy. I was testing the durability of the websocket clients (one in java and one in javascript using stomp.js and socks.js) and noticed that when I undeployed the application from glassfish both clients would think the websocket was still up. For fun I added a recurring ping request from each client to the server to mimic a heartbeat. When the application is up the ping request works great and I get pong responses from the server, but when I undeploy the app from glassfish (to simulate a disconnect) I still get successful ping and pong messages from the server. It seems to me that when the application is undeployed it should send out disconnect messages to all connected clients which would invoke their reconnect logic to hit another server in the cluster. Has anyone seen similar behavior??? Thanks for any help!

Это было полезно?

Решение

I think I have this one figured out. I failed to set the heartbeat configuration on the STOMP connection. Once I set these values I began seeing heartbeats sent to the client by the server, and when I pulled the plug on the web socket application the heartbeats stopped, as they should. After that point is was very easy to implement some reconnect logic based on the last time I received a heartbeat and if it was too old or not. Here is some sample code for configuring the STOMP client, most of this I pulled from the spring stock-portfolio stomp client example.

Inside the StompWebSocketHandler class you simply add this block of code. You would obviously set the heartbeatInterval variable to whatever value you desire.

public void afterConnectionEstablished(WebSocketSession session) throws IOException {
     StompHeaderAccessor headers = StompHeaderAccessor.create(StompCommand.CONNECT);
     headers.setAcceptVersion("1.1,1.2");
     headers.setHeartbeat(heartbeatInterval, heartbeatInterval);
     Message<byte[]> message = MessageBuilder.withPayload(new byte[0]).setHeaders(headers).build();

     TextMessage textMessage = new TextMessage(new String(encoder.encode(message), DEFAULT_CHARSET));
     session.sendMessage(textMessage);
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top