javaspring-boottomcatwebsocket

How can you set org.apache.tomcat.websocket.BLOCKING_SEND_TIMEOUT?


What's the expected way to set org.apache.tomcat.websocket.BLOCKING_SEND_TIMEOUT to increase a tomcat websocket timeout? Tomcat documentation states the following:

This may be changed by setting the property org.apache.tomcat.websocket.BLOCKING_SEND_TIMEOUT in the user properties collection attached to the WebSocket session.

The WebSocketSession I see available in the TextWebSocketHandler's afterConnectionEstablished method doesn't apear to have user properties. So, I assume that's not what the documentation means. In looking at TomcatRequestUpgradeStrategy, it appears to me that it never looks at an endpoint user properties. It also doesn't appear to me that you can overwrite TomcatRequestUpgradeStrategy since AbstractHandshakeHandler has a hardcoded class name for TomcatRequestUpgradeStrategy.

Please help.


Solution

  • org.apache.tomcat.websocket.BLOCKING_SEND_TIMEOUT is an user property you need to set on WebSocket API's Session, not the Spring abstractions of this interface.

    You can configure it in the afterConnectionEstablished method by casting the Spring WebSocketSession to NativeWebSocketSession and retrieving the underlying WebSocket API session:

    public void afterConnectionEstablished(WebSocketSession session) throws Exception {
        if (session instanceof NativeWebSocketSession) {
            final Session nativeSession = ((NativeWebSocketSession) session).getNativeSession(Session.class);
            if (nativeSession != null) {
                nativeSession.getUserProperties()
                             .put("org.apache.tomcat.websocket.BLOCKING_SEND_TIMEOUT", 60_000L);
            }
        }
    }