From 1f19bb231105b5434ca7a8df7874a00fd9a2a0e7 Mon Sep 17 00:00:00 2001 From: rstoyanchev Date: Wed, 22 Nov 2023 15:32:49 +0000 Subject: [PATCH] Update STOMP WebSocket transport reference docs Closes gh-31616 --- .../ROOT/pages/web/websocket/server.adoc | 48 ++++++++----------- .../web/websocket/stomp/server-config.adoc | 28 +++++++++-- 2 files changed, 45 insertions(+), 31 deletions(-) diff --git a/framework-docs/modules/ROOT/pages/web/websocket/server.adoc b/framework-docs/modules/ROOT/pages/web/websocket/server.adoc index 653f944dfc08..0e506eaff089 100644 --- a/framework-docs/modules/ROOT/pages/web/websocket/server.adoc +++ b/framework-docs/modules/ROOT/pages/web/websocket/server.adoc @@ -229,34 +229,27 @@ Java initialization API. The following example shows how to do so: [[websocket-server-runtime-configuration]] -== Server Configuration +== Configuring the Server [.small]#xref:web/webflux-websocket.adoc#webflux-websocket-server-config[See equivalent in the Reactive stack]# -Each underlying WebSocket engine exposes configuration properties that control -runtime characteristics, such as the size of message buffer sizes, idle timeout, -and others. +You can configure of the underlying WebSocket server such as input message buffer size, +idle timeout, and more. -For Tomcat, WildFly, and GlassFish, you can add a `ServletServerContainerFactoryBean` to your -WebSocket Java config, as the following example shows: +For Jakarta WebSocket servers, you can add a `ServletServerContainerFactoryBean` to your +Java configuration. For example: [source,java,indent=0,subs="verbatim,quotes"] ---- - @Configuration - @EnableWebSocket - public class WebSocketConfig implements WebSocketConfigurer { - - @Bean - public ServletServerContainerFactoryBean createWebSocketContainer() { - ServletServerContainerFactoryBean container = new ServletServerContainerFactoryBean(); - container.setMaxTextMessageBufferSize(8192); - container.setMaxBinaryMessageBufferSize(8192); - return container; - } - - } + @Bean + public ServletServerContainerFactoryBean createWebSocketContainer() { + ServletServerContainerFactoryBean container = new ServletServerContainerFactoryBean(); + container.setMaxTextMessageBufferSize(8192); + container.setMaxBinaryMessageBufferSize(8192); + return container; + } ---- -The following example shows the XML configuration equivalent of the preceding example: +Or to your XML configuration: [source,xml,indent=0,subs="verbatim,quotes,attributes"] ---- @@ -277,12 +270,11 @@ The following example shows the XML configuration equivalent of the preceding ex ---- -NOTE: For client-side WebSocket configuration, you should use `WebSocketContainerFactoryBean` -(XML) or `ContainerProvider.getWebSocketContainer()` (Java configuration). +NOTE: For client Jakarta WebSocket configuration, use +ContainerProvider.getWebSocketContainer() in Java configuration, or +`WebSocketContainerFactoryBean` in XML. -For Jetty, you need to supply a pre-configured Jetty `WebSocketServerFactory` and plug -that into Spring's `DefaultHandshakeHandler` through your WebSocket Java config. -The following example shows how to do so: +For Jetty, you can supply a `Consumer` callback to configure the WebSocket server: [source,java,indent=0,subs="verbatim,quotes"] ---- @@ -298,11 +290,9 @@ The following example shows how to do so: @Bean public DefaultHandshakeHandler handshakeHandler() { - WebSocketPolicy policy = new WebSocketPolicy(WebSocketBehavior.SERVER); policy.setInputBufferSize(8192); policy.setIdleTimeout(600000); - return new DefaultHandshakeHandler( new JettyRequestUpgradeStrategy(new WebSocketServerFactory(policy))); } @@ -349,6 +339,10 @@ The following example shows the XML configuration equivalent of the preceding ex ---- +TIP: When using STOMP over WebSocket, you will also need to configure +xref:web/websocket/stomp/server-config.adoc[STOMP WebSocket transport] +properties. + [[websocket-server-allowed-origins]] diff --git a/framework-docs/modules/ROOT/pages/web/websocket/stomp/server-config.adoc b/framework-docs/modules/ROOT/pages/web/websocket/stomp/server-config.adoc index 92538177177f..b5ecf349ae06 100644 --- a/framework-docs/modules/ROOT/pages/web/websocket/stomp/server-config.adoc +++ b/framework-docs/modules/ROOT/pages/web/websocket/stomp/server-config.adoc @@ -1,9 +1,14 @@ [[websocket-stomp-server-config]] -= WebSocket Server += WebSocket Transport -To configure the underlying WebSocket server, the information in -xref:web/websocket/server.adoc#websocket-server-runtime-configuration[Server Configuration] applies. For Jetty, however you need to set -the `HandshakeHandler` and `WebSocketPolicy` through the `StompEndpointRegistry`: +This section explains how to configure the underlying WebSocket server transport. + +For Jakarta WebSocket servers, add a `ServletServerContainerFactoryBean` to your +configuration. For examples, see +xref:web/websocket/server.adoc#websocket-server-runtime-configuration[Configuring the Server] +under the WebSocket section. + +For Jetty WebSocket servers, customize the `JettyRequestUpgradeStrategy` as follows: [source,java,indent=0,subs="verbatim,quotes"] ---- @@ -29,5 +34,20 @@ the `HandshakeHandler` and `WebSocketPolicy` through the `StompEndpointRegistry` } ---- +In addition to WebSocket server properties, there are also STOMP WebSocket transport properties +to customize as follows: + +[source,java,indent=0,subs="verbatim,quotes"] +---- + @Configuration + @EnableWebSocketMessageBroker + public class WebSocketConfig implements WebSocketMessageBrokerConfigurer { + @Override + public void configureWebSocketTransport(WebSocketTransportRegistration registry) { + registry.setMessageSizeLimit(4 * 8192); + registry.setTimeToFirstMessage(30000); + } + } +----