专业财税服务推荐

精选优质财税服务,为企业提供专业、可靠的财税解决方案,助力企业健康发展

零报税代理记账
零申报代理记账
报税做账算帐财务报表老会计做账
代理记账
咨询微信:lhy_happyday
工商营业执照年度年报年检公示
全国个体、企业、公司、合作社工商年审年报服务!
个体/10元/次 企业/20元/次
咨询微信:lhy_happyday
财税咨询服务
一对一专业财税咨询,解决企业财税难题,提供定制方案
咨询微信:lhy_happyday
财务分析服务
小规模个体报税0申报税务年报工商年报月报季报报税代理记账
咨询微信:lhy_happyday
立即咨询专业财税顾问
微信号: lhy_happyday
会计从业9年,管理多家个体工商、小规模、一般纳税人等企业的财务、税务等相关工作!。
扫码或搜索添加微信,备注"财税咨询"获取专属优惠
知方号 知方号

STOMP Client :: Spring Framework spring web socket

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举报,一经查实,本站将立刻删除。