Spring Websocket + SockJS 中 SimpleMessageBroker 的 /topic 和 /queue 之间的区别

Difference between /topic, /queue for SimpleMessageBroker in Spring Websocket + SockJS

如果我使用 "simple broker",Spring Websocket + SockJS 的 /topic、/queue 等之间有什么区别?例如。这里Sending message to specific user on Spring Websocket说:当您的客户端订阅以/user/开头的频道时,例如:/user/queue/reply,您的服务器实例将订阅一个名为queue/reply-user[会话 ID]

我想以某种清晰的方式理解此类转换背后的逻辑。

你应该看看 this part of the reference documentation。 简而言之,"/topic""/queue" 都是配置到同一目的地的前缀。

在文档中,"/app" 是已配置的 "application destination prefix" - 这意味着所有通过 "clientInboundChannel" 流入并匹配这些前缀的消息都将映射到您的应用程序,例如 @MessageMapping 注释.

此外,"/topic""/queue" 都是配置为 STOMP 目的地的前缀 - 这意味着所有通过 "clientInboundChannel" 流入并匹配这些前缀的消息都将转发到 STOMP 代理.在您的情况下,这就是简单的代理实施。

因此,从 Spring Websocket 的角度来看,"/queue""/topic" 的处理方式相同,并且是 "typical" STOMP 目的地 - 所有与它们匹配的消息都被转发到消息代理。现在,如果您使用的是完整的消息代理实现,那么这些目的地可能具有不同的含义,并且消息代理的行为可能会有所不同。以下是 Apache Apollo and RabbitMQ.

的一些示例

请注意,如果需要,您可以更改这些前缀。但我建议您将这些设置为默认值,除非您真的知道自己在做什么。

我认为该主题的最佳答案是 Spring 文档

中的以下内容

The meaning of a destination is intentionally left opaque in the STOMP spec. It can be any string, and it’s entirely up to STOMP servers to define the semantics and the syntax of the destinations that they support. It is very common, however, for destinations to be path-like strings where "/topic/.." implies publish-subscribe (one-to-many) and "/queue/" implies point-to-point (one-to-one) message exchanges.

上面的答案中没有提到一个更大更重要的区别。

主题是自动删除的,而队列是持久的。这意味着当 websocket 连接关闭时,主题及其数据将被删除。在队列中,服务器仍然可以发送消息,当客户端通过 websocket 连接时,它会收到服务器发送的旧消息。

顺便说一句,内存代理没有区别。使用专用代理时会发生这种情况。