知方号

知方号

STOMP Client :: Spring Framework

STOMP Client

Spring provides a STOMP over WebSocket client and a STOMP over TCP client.

To begin, you can create and configure WebSocketStompClient, as the following example shows:

WebSocketClient webSocketClient = new StandardWebSocketClient();WebSocketStompClient stompClient = new WebSocketStompClient(webSocketClient);stompClient.setMessageConverter(new StringMessageConverter());stompClient.setTaskScheduler(taskScheduler); // for heartbeats

In the preceding example, you could replace StandardWebSocketClient with SockJsClient,since that is also an implementation of WebSocketClient. The SockJsClient canuse WebSocket or HTTP-based transport as a fallback. For more details, seeSockJsClient.

Next, you can establish a connection and provide a handler for the STOMP session,as the following example shows:

String url = "ws://127.0.0.1:8080/endpoint";StompSessionHandler sessionHandler = new MyStompSessionHandler();stompClient.connect(url, sessionHandler);

When the session is ready for use, the handler is notified, as the following example shows:

public class MyStompSessionHandler extends StompSessionHandlerAdapter {@Overridepublic void afterConnected(StompSession session, StompHeaders connectedHeaders) {// ...}}

Once the session is established, any payload can be sent and isserialized with the configured MessageConverter, as the following example shows:

session.send("/topic/something", "payload");

You can also subscribe to destinations. The subscribe methods require a handlerfor messages on the subscription and returns a Subscription handle that you canuse to unsubscribe. For each received message, the handler can specify the targetObject type to which the payload should be deserialized, as the following example shows:

session.subscribe("/topic/something", new StompFrameHandler() {@Overridepublic Type getPayloadType(StompHeaders headers) {return String.class;}@Overridepublic void handleFrame(StompHeaders headers, Object payload) {// ...}});

To enable STOMP heartbeat, you can configure WebSocketStompClient with a TaskSchedulerand optionally customize the heartbeat intervals (10 seconds for write inactivity,which causes a heartbeat to be sent, and 10 seconds for read inactivity, whichcloses the connection).

WebSocketStompClient sends a heartbeat only in case of inactivity, i.e. when noother messages are sent. This can present a challenge when using an external brokersince messages with a non-broker destination represent activity but aren’t actuallyforwarded to the broker. In that case you can configure a TaskSchedulerwhen initializing the External Broker which ensures aheartbeat is forwarded to the broker also when only messages with a non-brokerdestination are sent.

When you use WebSocketStompClient for performance tests to simulate thousandsof clients from the same machine, consider turning off heartbeats, since eachconnection schedules its own heartbeat tasks and that is not optimized fora large number of clients running on the same machine.

The STOMP protocol also supports receipts, where the client must add a receiptheader to which the server responds with a RECEIPT frame after the send orsubscribe are processed. To support this, the StompSession offerssetAutoReceipt(boolean) that causes a receipt header to beadded on every subsequent send or subscribe event.Alternatively, you can also manually add a receipt header to the StompHeaders.Both send and subscribe return an instance of Receiptablethat you can use to register for receipt success and failure callbacks.For this feature, you must configure the client with a TaskSchedulerand the amount of time before a receipt expires (15 seconds by default).

Note that StompSessionHandler itself is a StompFrameHandler, which letsit handle ERROR frames in addition to the handleException callback forexceptions from the handling of messages and handleTransportError fortransport-level errors including ConnectionLostException.

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至lizi9903@foxmail.com举报,一经查实,本站将立刻删除。