How do you quickly close a nonresponsive websocket in Java Spring Tomcat?

ServletServerContainerFactoryBean simply configures the underlying JSR-356 WebSocketContainer through Spring configuration on startup. If you peek inside you'll see it's trivial.

From what I can see in Tomcat code about the handling of maxSessionIdleTimeout, the WsWebSocketContainer#backgroundProcess() method runs every 10 seconds by default to see if there are expired sessions.

I suspect also the pings you're sending from the server are making the session appear active, hence not helping with regards to the idle session timeout configuration.

As to why Tomcat doesn't realize the client is disconnected sooner, I can't really say. In my experience if a client closes a WebSocket connection or if I kill the browser it's detected immediately. In any case that's more to do with Tomcat not Spring.


Application Events may help you.

PS: Annotation driven events

PS2: I made an example project for you