@@ -1884,41 +1884,40 @@ to access information about the message.
1884
1884
[[websocket-stomp-client]]
1885
1885
=== STOMP Client
1886
1886
1887
- Spring provides STOMP/WebSocket client and STOMP/TCP client support with the
1888
- following built-in choices (other libraries can be adapted):
1887
+ Spring provides a STOMP over WebSocket client and a STOMP over TCP client.
1889
1888
1890
- * `WebSocketStompClient` built on the Spring WebSocket API with
1891
- support for standard JSR-356 WebSocket, Jetty 9, as well as SockJS
1892
- for HTTP-based WebSocket emulation (see <<websocket-fallback-sockjs-client>>).
1893
- * `Reactor11TcpStompClient` built on `NettyTcpClient` from the reactor-net project.
1894
-
1895
- To begin, create and configure the client:
1889
+ To begin create and configure `WebSocketStompClient`:
1896
1890
1897
1891
[source,java,indent=0]
1898
1892
[subs="verbatim,quotes"]
1899
1893
----
1900
- WebSocketClient transport = new StandardWebSocketClient();
1901
- WebSocketStompClient stompClient = new WebSocketStompClient(transport );
1894
+ WebSocketClient webSocketClient = new StandardWebSocketClient();
1895
+ WebSocketStompClient stompClient = new WebSocketStompClient(webSocketClient );
1902
1896
stompClient.setMessageConverter(new StringMessageConverter());
1903
- stompClient.setTaskScheduler(taskScheduler); // for heartbeats, receipts
1897
+ stompClient.setTaskScheduler(taskScheduler); // for heartbeats
1904
1898
----
1905
1899
1906
- Then connect to the WebSocket and provide a handler for the STOMP session:
1900
+ In the above example `StandardWebSocketClient` could be replaced with `SockJsClient`
1901
+ since that is also an implementation of `WebSocketClient`. The `SockJsClient` can
1902
+ use WebSocket or HTTP-based transport as a fallback. For more details see
1903
+ <<websocket-fallback-sockjs-client>>.
1904
+
1905
+ Next establish a connection and provide a handler for the STOMP session:
1907
1906
1908
1907
[source,java,indent=0]
1909
1908
[subs="verbatim,quotes"]
1910
1909
----
1911
1910
String url = "ws://127.0.0.1:8080/endpoint";
1912
- StompSessionHandler handler = ... ;
1913
- stompClient.connect(url, handler );
1911
+ StompSessionHandler sessionHandler = new MyStompSessionHandler() ;
1912
+ stompClient.connect(url, sessionHandler );
1914
1913
----
1915
1914
1916
- When the session is ready for use, the handler is notified:
1915
+ When the session is ready for use the handler is notified:
1917
1916
1918
1917
[source,java,indent=0]
1919
1918
[subs="verbatim,quotes"]
1920
1919
----
1921
- public class MySessionHandler extends StompSessionHandlerAdapter {
1920
+ public class MyStompSessionHandler extends StompSessionHandlerAdapter {
1922
1921
1923
1922
@Override
1924
1923
public void afterConnected(StompSession session, StompHeaders connectedHeaders) {
@@ -1927,18 +1926,19 @@ public class MySessionHandler extends StompSessionHandlerAdapter {
1927
1926
}
1928
1927
----
1929
1928
1930
- Send any Object as the payload and it will be serialized with a `MessageConverter`:
1929
+ Once the session is established any payload can be sent and that will be
1930
+ serialized with the configured `MessageConverter`:
1931
1931
1932
1932
[source,java,indent=0]
1933
1933
[subs="verbatim,quotes"]
1934
1934
----
1935
1935
session.send("/topic/foo", "payload");
1936
1936
----
1937
1937
1938
- The subscribe methods take a `StompFrameHandler` for messages on the subscription
1939
- and return a `Subscription` handle for unsubscribing. For each received message,
1940
- the handler must help to select the target type and then handle the
1941
- deserialized payload:
1938
+ You can also subscribe to destinations. The `subscribe` methods require a handler
1939
+ for messages on the subscription and return a `Subscription` handle that can be
1940
+ used to unsubscribe. For each received message the handler can specify the target
1941
+ Object type the payload should be deserialized to :
1942
1942
1943
1943
[source,java,indent=0]
1944
1944
[subs="verbatim,quotes"]
@@ -1958,11 +1958,19 @@ session.subscribe("/topic/foo", new StompFrameHandler() {
1958
1958
});
1959
1959
----
1960
1960
1961
- STOMP supports heartbeats. To use this feature simply configure the
1962
- `WebSocketStompClient` with a `TaskScheduler` and if desired customize the
1963
- default heartbeat intervals (10, 10 seconds respectively by default) for
1964
- write inactivity which causes a heartbeat to be sent and for read
1965
- inactivity which closes the connection.
1961
+ To enable STOMP heartbeat configure `WebSocketStompClient` with a `TaskScheduler`
1962
+ and optionally customize the heartbeat intervals, 10 seconds for write inactivity
1963
+ which causes a heartbeat to be sent and 10 seconds for read inactivity which
1964
+ closes the connection.
1965
+
1966
+ [NOTE]
1967
+ ====
1968
+ When using `WebSocketStompClient` for performance tests to simulate thousands
1969
+ of clients from the same machine consider turning off heartbeats since each
1970
+ connection schedules its own heartbeat tasks and that's not optimized for a
1971
+ a large number of clients running on the same machine.
1972
+ ====
1973
+
1966
1974
1967
1975
The STOMP protocol also supports receipts where the client must add a "receipt"
1968
1976
header to which the server responds with a RECEIPT frame after the send or
0 commit comments