如何在Spring中注册stomp订阅 [英] How to register stomp subscriptions in Spring

查看:232
本文介绍了如何在Spring中注册stomp订阅的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用spring-messaging v.4.2控制Spring应用程序中的stomp订阅. 这是我的stomp的Spring应用程序配置:

I want to control stomp subscription in my Spring application with spring-messaging v.4.2. This is my Spring app configuration for stomp:

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.myapp")
@EnableWebSocketMessageBroker
@EnableAsync
@EnableScheduling
public class Config extends AbstractWebSocketMessageBrokerConfigurer  {

    @Override
    public void configureMessageBroker(MessageBrokerRegistry config) {
        config.enableSimpleBroker("/queue", "/topic");
        config.setApplicationDestinationPrefixes("/app");
        config.setUserDestinationPrefix("/user");
    }

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

    @Bean
    public Executor taskExecutor() {
        return new SimpleAsyncTaskExecutor();
    }   

    @Bean 
    public MultipartResolver multipartResolver(){
        return new CommonsMultipartResolver();
    }
}

如何编写以路径和stompId为参数的每个订阅调用的方法? 谢谢

How can I write a method called for each subscription with path and stompId as parameters ? Thanks

推荐答案

在必要时,StompSubProtocolHandler会引发相应的ApplicationEvent:

The StompSubProtocolHandler raises appropriate ApplicationEvents when it is necessary:

if (this.eventPublisher != null) {
    Principal user = getUser(session);
    if (isConnect) {
        publishEvent(this.eventPublisher, new SessionConnectEvent(this, message, user));
    }
    else if (StompCommand.SUBSCRIBE.equals(command)) {
        publishEvent(this.eventPublisher, new SessionSubscribeEvent(this, message, user));
    }
    else if (StompCommand.UNSUBSCRIBE.equals(command)) {
        publishEvent(this.eventPublisher, new SessionUnsubscribeEvent(this, message, user));
    }
}

message中提供了所有必要的信息.请参阅StompHeaderAccessor API如何从此事件的消息头中获取所需的STOMP信息.

All the necessary information is present in the message. See StompHeaderAccessor API how to obtain the required STOMP information from the message headers on the matter.

这篇关于如何在Spring中注册stomp订阅的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆