消息顺序

来自代理的消息发布到clientOutboundChannel,然后从那里写入 WebSocket 会话。由于该通道由ThreadPoolExecutor支持,因此消息在不同的线程中处理,客户端接收到的结果序列可能与发布的确切顺序不匹配。

要启用有序发布,请按如下方式设置setPreservePublishOrder标志

  • Java

  • Kotlin

  • Xml

@Configuration
@EnableWebSocketMessageBroker
public class PublishOrderWebSocketConfiguration implements WebSocketMessageBrokerConfigurer {

	@Override
	public void configureMessageBroker(MessageBrokerRegistry registry) {
		// ...
		registry.setPreservePublishOrder(true);
	}

}
@Configuration
@EnableWebSocketMessageBroker
class PublishOrderWebSocketConfiguration : WebSocketMessageBrokerConfigurer {

	override fun configureMessageBroker(registry: MessageBrokerRegistry) {
		// ...
		registry.setPreservePublishOrder(true)
	}
}
<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,从而保证发布顺序。请注意,这会带来少量性能开销,因此您应该仅在需要时启用它。

客户端发送到clientInboundChannel的消息也适用相同规则,然后根据其目标前缀进行处理。由于该通道由ThreadPoolExecutor支持,因此消息在不同的线程中处理,处理的结果序列可能与接收到的确切顺序不匹配。

要启用有序发布,请按如下方式设置setPreserveReceiveOrder标志

  • Java

  • Kotlin

@Configuration
@EnableWebSocketMessageBroker
public class ReceiveOrderWebSocketConfiguration implements WebSocketMessageBrokerConfigurer {

	@Override
	public void registerStompEndpoints(StompEndpointRegistry registry) {
		registry.setPreserveReceiveOrder(true);
	}
}
@Configuration
@EnableWebSocketMessageBroker
class ReceiveOrderWebSocketConfiguration : WebSocketMessageBrokerConfigurer {

	override fun registerStompEndpoints(registry: StompEndpointRegistry) {
		registry.setPreserveReceiveOrder(true)
	}
}