How to register stomp subscriptions in Spring

前提是你 提交于 2019-12-11 15:23:11

问题


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();
    }
}

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


回答1:


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));
    }
}

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.



来源:https://stackoverflow.com/questions/47517425/how-to-register-stomp-subscriptions-in-spring

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!