Events
我们 发布 了几个`ApplicationContext` 事件,并且可以通过实现 Spring 的 ApplicationListener
接口来接收它们:
Several ApplicationContext
events are published and can be
received by implementing Spring’s ApplicationListener
interface:
-
BrokerAvailabilityEvent
:指示何时代理变得可用或不可用。虽然 “simple” 代理在启动后立即变为可用,并且在应用程序运行时保持可用,但 STOMP “broker relay” 可能会丢失与完整功能代理的连接(例如,如果该代理已重新启动)。代理中继具有重连逻辑,并重新建立 “system” 与代理的连接(如果代理返回)。因此,每当状态从连接更改为断开连接,反之亦然时,就会发布此事件。使用SimpMessagingTemplate
的组件应该订阅此事件,并在代理不可用的情况下避免发送消息。在任何情况下,它们都应该准备好处理发送消息时的MessageDeliveryException
。 -
BrokerAvailabilityEvent
: Indicates when the broker becomes available or unavailable. While the “simple” broker becomes available immediately on startup and remains so while the application is running, the STOMP “broker relay” can lose its connection to the full featured broker (for example, if the broker is restarted). The broker relay has reconnect logic and re-establishes the “system” connection to the broker when it comes back. As a result, this event is published whenever the state changes from connected to disconnected and vice-versa. Components that use theSimpMessagingTemplate
should subscribe to this event and avoid sending messages at times when the broker is not available. In any case, they should be prepared to handleMessageDeliveryException
when sending a message. -
SessionConnectEvent
:在收到新的 STOMP CONNECT 时发布,以指示新客户端会话的开始。该事件包含表示连接的消息,包括会话 ID、用户信息(如果存在)和客户端发送的任何自定义标头。这对于跟踪客户端会话很有用。订阅此事件的组件可以使用SimpMessageHeaderAccessor
或StompMessageHeaderAccessor
将包含的消息进行包装。 -
SessionConnectEvent
: Published when a new STOMP CONNECT is received to indicate the start of a new client session. The event contains the message that represents the connect, including the session ID, user information (if any), and any custom headers the client sent. This is useful for tracking client sessions. Components subscribed to this event can wrap the contained message withSimpMessageHeaderAccessor
orStompMessageHeaderAccessor
. -
SessionConnectedEvent
:紧随SessionConnectEvent
发布,当代理发送 STOMP CONNECTED 帧以响应 CONNECT 时。此时,可以认为 STOMP 会话已完全建立。 -
SessionConnectedEvent
: Published shortly after aSessionConnectEvent
when the broker has sent a STOMP CONNECTED frame in response to the CONNECT. At this point, the STOMP session can be considered fully established. -
SessionSubscribeEvent
:在收到新的 STOMP SUBSCRIBE 时发布。 -
SessionSubscribeEvent
: Published when a new STOMP SUBSCRIBE is received. -
SessionUnsubscribeEvent
:在收到新的 STOMP UNSUBSCRIBE 时发布。 -
SessionUnsubscribeEvent
: Published when a new STOMP UNSUBSCRIBE is received. -
SessionDisconnectEvent
:在 STOMP 会话结束时发布。可能会从客户端发送 DISCONNECT,也可能在 WebSocket 会话关闭时自动生成 DISCONNECT。在某些情况下,此事件会每个会话发布多次。组件应针对多个断开连接事件具有幂等性。 -
SessionDisconnectEvent
: Published when a STOMP session ends. The DISCONNECT may have been sent from the client or it may be automatically generated when the WebSocket session is closed. In some cases, this event is published more than once per session. Components should be idempotent with regard to multiple disconnect events.
当您使用全功能中介时,STOMP “broker relay” 会在代理暂时不可用时自动重新连接 “system” 连接。但是,客户端连接不会自动重新连接。假设启用心跳,则客户端通常会注意到代理 10 秒内未响应。客户端需要实现自己的重新连接逻辑。 |
When you use a full-featured broker, the STOMP “broker relay” automatically reconnects the “system” connection if broker becomes temporarily unavailable. Client connections, however, are not automatically reconnected. Assuming heartbeats are enabled, the client typically notices the broker is not responding within 10 seconds. Clients need to implement their own reconnecting logic. |