对于最新的稳定版本,请使用 Spring Framework 6.2.0! |
消息顺序
来自代理的消息将发布到clientOutboundChannel
,从他们所在的位置
写入 WebSocket 会话。由于通道由ThreadPoolExecutor
消息
在不同的线程中处理,并且客户端接收到的结果序列可能会
与发布的确切顺序不匹配。
如果这是一个问题,请启用setPreservePublishOrder
标志,如下例所示:
@Configuration
@EnableWebSocketMessageBroker
public class MyConfig implements WebSocketMessageBrokerConfigurer {
@Override
protected void configureMessageBroker(MessageBrokerRegistry registry) {
// ...
registry.setPreservePublishOrder(true);
}
}
以下示例显示了与上述示例等效的 XML 配置:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:websocket="http://www.springframework.org/schema/websocket"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/websocket
https://www.springframework.org/schema/websocket/spring-websocket.xsd">
<websocket:message-broker preserve-publish-order="true">
<!-- ... -->
</websocket:message-broker>
</beans>
设置该标志后,同一客户端会话中的消息将发布到clientOutboundChannel
一次一个,以便保证发布顺序。
请注意,这会产生较小的性能开销,因此应仅在需要时启用它。