User Destinations

应用程序可以发送以特定用户为目标的消息,Spring 的 STOMP 支持识别带有前缀 /user/ 的目标,以用于此目的。例如,客户端可能会订阅 /user/queue/position-updates 目标。UserDestinationMessageHandler 处理此目标,并将其转换为用户会话的唯一目标(例如 /queue/position-updates-user123)。这提供了订阅通用命名目标的便利性,同时确保不会与订阅相同目标的其他用户发生冲突,这样每个用户都可以收到唯一的股票头寸更新。

An application can send messages that target a specific user, and Spring’s STOMP support recognizes destinations prefixed with /user/ for this purpose. For example, a client might subscribe to the /user/queue/position-updates destination. UserDestinationMessageHandler handles this destination and transforms it into a destination unique to the user session (such as /queue/position-updates-user123). This provides the convenience of subscribing to a generically named destination while, at the same time, ensuring no collisions with other users who subscribe to the same destination so that each user can receive unique stock position updates.

在使用用户目标时,重要的是像 Enable STOMP 中所示的那样配置代理和应用程序目标前缀,否则 代理会处理不应该由 UserDestinationMessageHandler 处理的带有“/user”前缀的消息。

When working with user destinations, it is important to configure broker and application destination prefixes as shown in Enable STOMP, or otherwise the broker would handle "/user" prefixed messages that should only be handled by UserDestinationMessageHandler.

在发送端,消息可以发送到例如 /user/{username}/queue/position-updates 的目标,而 UserDestinationMessageHandler 又会将其翻译为一个或多个目标,每个与用户关联的会话一个。这允许应用程序内的任何组件发送以特定用户为目标的消息,而无需知道除其名称和通用目标之外的任何信息。这也通过注释和消息传递模板得到支持。

On the sending side, messages can be sent to a destination such as /user/{username}/queue/position-updates, which in turn is translated by the UserDestinationMessageHandler into one or more destinations, one for each session associated with the user. This lets any component within the application send messages that target a specific user without necessarily knowing anything more than their name and the generic destination. This is also supported through an annotation and a messaging template.

消息处理方法可以通过 @SendToUser 注释将消息发送给与正在处理的消息关联的用户(也在类级别支持以共享一个共同的目标),如下面的示例所示:

A message-handling method can send messages to the user associated with the message being handled through the @SendToUser annotation (also supported on the class-level to share a common destination), as the following example shows:

@Controller
public class PortfolioController {

	@MessageMapping("/trade")
	@SendToUser("/queue/position-updates")
	public TradeResult executeTrade(Trade trade, Principal principal) {
		// ...
		return tradeResult;
	}
}

如果用户有多个会话,默认情况下,将针对已订阅给定目标的所有会话。但是,有时可能仅需要针对发送正在处理的消息的会话。您可以通过将 broadcast 属性设置为 false 来这样做,如下面的示例所示:

If the user has more than one session, by default, all of the sessions subscribed to the given destination are targeted. However, sometimes, it may be necessary to target only the session that sent the message being handled. You can do so by setting the broadcast attribute to false, as the following example shows:

@Controller
public class MyController {

	@MessageMapping("/action")
	public void handleAction() throws Exception{
		// raise MyBusinessException here
	}

	@MessageExceptionHandler
	@SendToUser(destinations="/queue/errors", broadcast=false)
	public ApplicationError handleException(MyBusinessException exception) {
		// ...
		return appError;
	}
}

虽然用户目标通常意味着经过身份验证的用户,但这并不是严格必需的。一个未与通过身份验证的用户关联的 WebSocket 会话可以订阅一个用户目标。在这种情况下,@SendToUser 注释的行为与 broadcast=false 完全相同(即仅针对发送正在处理的消息的会话)。

While user destinations generally imply an authenticated user, it is not strictly required. A WebSocket session that is not associated with an authenticated user can subscribe to a user destination. In such cases, the @SendToUser annotation behaves exactly the same as with broadcast=false (that is, targeting only the session that sent the message being handled).

例如,通过注入由 Java 配置或 XML 命名空间创建的 SimpMessagingTemplate,您可以从任何应用程序组件向用户目的地发送一条消息。(如果需要使用 @Qualifier 进行限定,则 Bean 名称就是 brokerMessagingTemplate。)以下示例展示了如何进行操作:

You can send a message to user destinations from any application component by, for example, injecting the SimpMessagingTemplate created by the Java configuration or the XML namespace. (The bean name is brokerMessagingTemplate if required for qualification with @Qualifier.) The following example shows how to do so:

@Service
public class TradeServiceImpl implements TradeService {

	private final SimpMessagingTemplate messagingTemplate;

	@Autowired
	public TradeServiceImpl(SimpMessagingTemplate messagingTemplate) {
		this.messagingTemplate = messagingTemplate;
	}

	// ...

	public void afterTradeExecuted(Trade trade) {
		this.messagingTemplate.convertAndSendToUser(
				trade.getUserName(), "/queue/position-updates", trade.getResult());
	}
}

当您在外部消息代理中使用用户目标时,您应该查看代理文档以了解如何管理非活动队列,以便在用户会话结束后删除所有唯一用户队列。例如,当您使用诸如 /exchange/amq.direct/position-updates 之类的目标时,RabbitMQ 会创建自动删除队列。因此,在该情况下,客户端可以订阅 /user/exchange/amq.direct/position-updates。类似地,ActiveMQ 具有 [清除非活动目标]({activemq-purge})。

When you use user destinations with an external message broker, you should check the broker documentation on how to manage inactive queues, so that, when the user session is over, all unique user queues are removed. For example, RabbitMQ creates auto-delete queues when you use destinations such as /exchange/amq.direct/position-updates. So, in that case, the client could subscribe to /user/exchange/amq.direct/position-updates. Similarly, ActiveMQ has configuration options for purging inactive destinations.

在多应用程序服务器场景中,用户目的地可能仍然无法解析,因为用户已连接到另一个服务器。在这些情况下,您可以配置一个目的地以广播无法解析的消息,以便其他服务器有机会尝试。这可以通过 Java 配置中 MessageBrokerRegistryuserDestinationBroadcast 属性和 XML 中 message-broker 元素的 user-destination-broadcast 属性来完成。

In a multi-application server scenario, a user destination may remain unresolved because the user is connected to a different server. In such cases, you can configure a destination to broadcast unresolved messages so that other servers have a chance to try. This can be done through the userDestinationBroadcast property of the MessageBrokerRegistry in Java configuration and the user-destination-broadcast attribute of the message-broker element in XML.