如何保证Java / Spring应用程序上的WebSocket消息顺序

时间:2016-06-10 08:52:51

标签: java spring websocket rabbitmq

我们目前正在使用Spring的SimpMessagingTemplate将WebSocket消息从服务器发送到客户端。我们注意到有时收到的邮件无序。

示例:

(Autowired) SimpMessagingTemplate broker
broker.convertAndSend(channelId, "Test1");
broker.convertAndSend(channelId, "Test2");

Client receives:
"Test2"
"Test1"

由于WebSocket是基于TCP / IP构建的,guarantees order,我不明白为什么Spring / SimpMessagingTemplate会破坏这个功能。

我无法使用SimpMessageTemplate找到有关消息顺序的任何文档,但有很多关于“全功能消息代理”的评论,例如RabbitMQ和ActiveMQ。 RabbitMQ在某些情况下保证消息顺序(不确定它是否适用于Spring应用程序上的WebSocket消息)。我尝试根据this指南对其进行设置,但Connection Factory正在给我一个"Could not autowire. No beans of Connection Factory type found."

如果我做错了什么,我试着下载并运行教程项目 - 这也不起作用。它只是通过一堆看起来像这样的调试打印显示“Build successfulful”:DEBUG org.springframework.core.env.PropertySourcesPropertyResolver - Could not find key 'spring.liveBeansView.mbeanDomain' in any property source. Returning [null]

如何在不破坏协议属性的情况下从Spring服务器发送WebSocket消息?

1 个答案:

答案 0 :(得分:1)

如果有人使用弹簧默认的SockJS + STOMP,这是一个春天的问题。有一个解决方案SockJS receive stomp messages from spring websocket out of order