简体   繁体   English

如何订阅内部Spring Boot / Simple Broker主题队列?

[英]How to subscribe an internal Spring Boot/Simple Broker topic queue?

I need to subscribe the internal spring boot topic /user/exchange/amq.direct/chat.message to creat a bot that will subscribe a topic queue and answer the messages. 我需要订阅内部spring引导主题/user/exchange/amq.direct/chat.message来创建一个机器人,它将订阅一个主题队列并回复消息。

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfiguration extends AbstractWebSocketMessageBrokerConfigurer {

@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {

    registry.addEndpoint("/ws").setAllowedOrigins("*").withSockJS();

}

@Override
public void configureMessageBroker(MessageBrokerRegistry config) {
    // use the /topic prefix for outgoing WebSocket communication
    config.enableSimpleBroker("/queue/", "/topic/", "/exchange/");

    // use the /app prefix for others
    config.setApplicationDestinationPrefixes("/app");
}

} }

My Controller: 我的控制器:

    @MessageMapping("/chat.message")
public ChatMessage filterMessage(@Payload ChatMessage message, Principal principal) {

    message.setUsername(principal.getName());

    return message;
}

@MessageMapping("/chat.private.{username}")
public void filterPrivateMessage(@Payload ChatMessage message, @DestinationVariable("username") String username, Principal principal) {

    message.setUsername(principal.getName());

    simpMessagingTemplate.convertAndSend("/user/" + username + "/exchange/amq.direct/chat.message", message);

}

How to subscribe a internal queue/topic that I put the message by SimpMessagingTemplate? 如何订阅我通过SimpMessagingTemplate放置消息的内部队列/主题?

You can use the @sendTo annotation and @DestinationVariable to distinguish which user you want to subscribe to. 您可以使用@sendTo批注和@DestinationVariable来区分您要订阅的用户。 I use this method to share my current subscription points. 我使用此方法来共享我当前的订阅点。 I'll give you one of my setup codes and a simple example. 我会给你一个设置代码和一个简单的例子。

It inherits from AbstractWebSocketMessageBrokerConfigurer and its configuration is final. 它继承自AbstractWebSocketMessageBrokerConfigurer,其配置是最终的。

@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
    registry.addEndpoint("/webSocket").setAllowedOrigins("*").withSockJS();
}

This code can only send a message to the user who subscribes to a specific path and the path that the client sends the message to. 此代码只能向订阅特定路径的用户以及客户端将消息发送到的路径发送消息。

@MessageMapping(value = "/question/detail/{questionId}/message")
@SendTo("/question/detail/{questionId}")
public MessageDto message(@DestinationVariable Long questionId, MessageDto messageDto) {
    return messageDto;
}

Is the answer you want? 你想要的答案是什么?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM