问题
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 ApplicationEvent
s 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