Search in sources :

Example 1 with StompBrokerRelayMessageHandler

use of org.springframework.messaging.simp.stomp.StompBrokerRelayMessageHandler in project spring-framework by spring-projects.

the class AbstractMessageBrokerConfiguration method stompBrokerRelayMessageHandler.

@Bean
public AbstractBrokerMessageHandler stompBrokerRelayMessageHandler() {
    StompBrokerRelayMessageHandler handler = getBrokerRegistry().getStompBrokerRelay(brokerChannel());
    if (handler == null) {
        return new NoOpBrokerMessageHandler();
    }
    Map<String, MessageHandler> subscriptions = new HashMap<>(1);
    String destination = getBrokerRegistry().getUserDestinationBroadcast();
    if (destination != null) {
        subscriptions.put(destination, userDestinationMessageHandler());
    }
    destination = getBrokerRegistry().getUserRegistryBroadcast();
    if (destination != null) {
        subscriptions.put(destination, userRegistryMessageHandler());
    }
    handler.setSystemSubscriptions(subscriptions);
    return handler;
}
Also used : SimpleBrokerMessageHandler(org.springframework.messaging.simp.broker.SimpleBrokerMessageHandler) StompBrokerRelayMessageHandler(org.springframework.messaging.simp.stomp.StompBrokerRelayMessageHandler) AbstractBrokerMessageHandler(org.springframework.messaging.simp.broker.AbstractBrokerMessageHandler) UserRegistryMessageHandler(org.springframework.messaging.simp.user.UserRegistryMessageHandler) UserDestinationMessageHandler(org.springframework.messaging.simp.user.UserDestinationMessageHandler) MessageHandler(org.springframework.messaging.MessageHandler) SimpAnnotationMethodMessageHandler(org.springframework.messaging.simp.annotation.support.SimpAnnotationMethodMessageHandler) HashMap(java.util.HashMap) StompBrokerRelayMessageHandler(org.springframework.messaging.simp.stomp.StompBrokerRelayMessageHandler) Bean(org.springframework.context.annotation.Bean)

Example 2 with StompBrokerRelayMessageHandler

use of org.springframework.messaging.simp.stomp.StompBrokerRelayMessageHandler in project spring-framework by spring-projects.

the class WebSocketMessageBrokerConfigurationSupport method webSocketMessageBrokerStats.

@Bean
public WebSocketMessageBrokerStats webSocketMessageBrokerStats() {
    AbstractBrokerMessageHandler relayBean = stompBrokerRelayMessageHandler();
    StompBrokerRelayMessageHandler brokerRelay = (relayBean instanceof StompBrokerRelayMessageHandler ? (StompBrokerRelayMessageHandler) relayBean : null);
    // Ensure STOMP endpoints are registered
    stompWebSocketHandlerMapping();
    WebSocketMessageBrokerStats stats = new WebSocketMessageBrokerStats();
    stats.setSubProtocolWebSocketHandler((SubProtocolWebSocketHandler) subProtocolWebSocketHandler());
    stats.setStompBrokerRelay(brokerRelay);
    stats.setInboundChannelExecutor(clientInboundChannelExecutor());
    stats.setOutboundChannelExecutor(clientOutboundChannelExecutor());
    stats.setSockJsTaskScheduler(messageBrokerTaskScheduler());
    return stats;
}
Also used : StompBrokerRelayMessageHandler(org.springframework.messaging.simp.stomp.StompBrokerRelayMessageHandler) WebSocketMessageBrokerStats(org.springframework.web.socket.config.WebSocketMessageBrokerStats) AbstractBrokerMessageHandler(org.springframework.messaging.simp.broker.AbstractBrokerMessageHandler) Bean(org.springframework.context.annotation.Bean)

Example 3 with StompBrokerRelayMessageHandler

use of org.springframework.messaging.simp.stomp.StompBrokerRelayMessageHandler in project spring-integration by spring-projects.

the class WebSocketInboundChannelAdapter method onInit.

@Override
protected void onInit() {
    super.onInit();
    this.webSocketContainer.setMessageListener(this);
    if (!CollectionUtils.isEmpty(this.messageConverters)) {
        List<MessageConverter> converters = this.messageConverter.getConverters();
        if (this.mergeWithDefaultConverters) {
            ListIterator<MessageConverter> iterator = this.messageConverters.listIterator(this.messageConverters.size());
            while (iterator.hasPrevious()) {
                MessageConverter converter = iterator.previous();
                converters.add(0, converter);
            }
        } else {
            converters.clear();
            converters.addAll(this.messageConverters);
        }
    }
    if (this.server && this.useBroker) {
        Map<String, AbstractBrokerMessageHandler> brokers = getApplicationContext().getBeansOfType(AbstractBrokerMessageHandler.class);
        for (AbstractBrokerMessageHandler broker : brokers.values()) {
            if (broker instanceof SimpleBrokerMessageHandler || broker instanceof StompBrokerRelayMessageHandler) {
                this.brokerHandler = broker;
                break;
            }
        }
        Assert.state(this.brokerHandler != null, "WebSocket Broker Relay isn't present in the application context; " + "it is required when 'useBroker = true'.");
    }
}
Also used : SimpleBrokerMessageHandler(org.springframework.messaging.simp.broker.SimpleBrokerMessageHandler) StompBrokerRelayMessageHandler(org.springframework.messaging.simp.stomp.StompBrokerRelayMessageHandler) AbstractBrokerMessageHandler(org.springframework.messaging.simp.broker.AbstractBrokerMessageHandler) CompositeMessageConverter(org.springframework.messaging.converter.CompositeMessageConverter) MessageConverter(org.springframework.messaging.converter.MessageConverter) ByteArrayMessageConverter(org.springframework.messaging.converter.ByteArrayMessageConverter) MappingJackson2MessageConverter(org.springframework.messaging.converter.MappingJackson2MessageConverter) StringMessageConverter(org.springframework.messaging.converter.StringMessageConverter)

Example 4 with StompBrokerRelayMessageHandler

use of org.springframework.messaging.simp.stomp.StompBrokerRelayMessageHandler in project spring-framework by spring-projects.

the class WebSocketMessageBrokerConfigurationSupport method webSocketMessageBrokerStats.

@Bean
public WebSocketMessageBrokerStats webSocketMessageBrokerStats(@Nullable AbstractBrokerMessageHandler stompBrokerRelayMessageHandler, WebSocketHandler subProtocolWebSocketHandler, TaskExecutor clientInboundChannelExecutor, TaskExecutor clientOutboundChannelExecutor, TaskScheduler messageBrokerTaskScheduler) {
    WebSocketMessageBrokerStats stats = new WebSocketMessageBrokerStats();
    stats.setSubProtocolWebSocketHandler((SubProtocolWebSocketHandler) subProtocolWebSocketHandler);
    if (stompBrokerRelayMessageHandler instanceof StompBrokerRelayMessageHandler) {
        stats.setStompBrokerRelay((StompBrokerRelayMessageHandler) stompBrokerRelayMessageHandler);
    }
    stats.setInboundChannelExecutor(clientInboundChannelExecutor);
    stats.setOutboundChannelExecutor(clientOutboundChannelExecutor);
    stats.setSockJsTaskScheduler(messageBrokerTaskScheduler);
    return stats;
}
Also used : StompBrokerRelayMessageHandler(org.springframework.messaging.simp.stomp.StompBrokerRelayMessageHandler) WebSocketMessageBrokerStats(org.springframework.web.socket.config.WebSocketMessageBrokerStats) Bean(org.springframework.context.annotation.Bean)

Example 5 with StompBrokerRelayMessageHandler

use of org.springframework.messaging.simp.stomp.StompBrokerRelayMessageHandler in project spring-framework by spring-projects.

the class TestValidator method stompBrokerRelay.

@Test
public void stompBrokerRelay() {
    loadBeanDefinitions("websocket-config-broker-relay.xml");
    HandlerMapping hm = this.appContext.getBean(HandlerMapping.class);
    assertThat(hm).isNotNull();
    assertThat(hm).isInstanceOf(SimpleUrlHandlerMapping.class);
    SimpleUrlHandlerMapping suhm = (SimpleUrlHandlerMapping) hm;
    assertThat(suhm.getUrlMap()).hasSize(1);
    assertThat(suhm.getOrder()).isEqualTo(2);
    HttpRequestHandler httpRequestHandler = (HttpRequestHandler) suhm.getUrlMap().get("/foo/**");
    assertThat(httpRequestHandler).isNotNull();
    assertThat(httpRequestHandler).isInstanceOf(SockJsHttpRequestHandler.class);
    SockJsHttpRequestHandler sockJsHttpRequestHandler = (SockJsHttpRequestHandler) httpRequestHandler;
    WebSocketHandler wsHandler = unwrapWebSocketHandler(sockJsHttpRequestHandler.getWebSocketHandler());
    assertThat(wsHandler).isNotNull();
    assertThat(wsHandler).isInstanceOf(SubProtocolWebSocketHandler.class);
    assertThat(sockJsHttpRequestHandler.getSockJsService()).isNotNull();
    UserDestinationResolver userDestResolver = this.appContext.getBean(UserDestinationResolver.class);
    assertThat(userDestResolver).isNotNull();
    assertThat(userDestResolver).isInstanceOf(DefaultUserDestinationResolver.class);
    DefaultUserDestinationResolver defaultUserDestResolver = (DefaultUserDestinationResolver) userDestResolver;
    assertThat(defaultUserDestResolver.getDestinationPrefix()).isEqualTo("/user/");
    StompBrokerRelayMessageHandler messageBroker = this.appContext.getBean(StompBrokerRelayMessageHandler.class);
    assertThat(messageBroker).isNotNull();
    assertThat(messageBroker.getClientLogin()).isEqualTo("clientlogin");
    assertThat(messageBroker.getClientPasscode()).isEqualTo("clientpass");
    assertThat(messageBroker.getSystemLogin()).isEqualTo("syslogin");
    assertThat(messageBroker.getSystemPasscode()).isEqualTo("syspass");
    assertThat(messageBroker.getRelayHost()).isEqualTo("relayhost");
    assertThat(messageBroker.getRelayPort()).isEqualTo(1234);
    assertThat(messageBroker.getVirtualHost()).isEqualTo("spring.io");
    assertThat(messageBroker.getSystemHeartbeatReceiveInterval()).isEqualTo(5000);
    assertThat(messageBroker.getSystemHeartbeatSendInterval()).isEqualTo(5000);
    assertThat(messageBroker.getDestinationPrefixes()).containsExactlyInAnyOrder("/topic", "/queue");
    assertThat(messageBroker.isPreservePublishOrder()).isTrue();
    List<Class<? extends MessageHandler>> subscriberTypes = Arrays.asList(SimpAnnotationMethodMessageHandler.class, UserDestinationMessageHandler.class, StompBrokerRelayMessageHandler.class);
    testChannel("clientInboundChannel", subscriberTypes, 2);
    testExecutor("clientInboundChannel", Runtime.getRuntime().availableProcessors() * 2, Integer.MAX_VALUE, 60);
    subscriberTypes = Collections.singletonList(SubProtocolWebSocketHandler.class);
    testChannel("clientOutboundChannel", subscriberTypes, 2);
    testExecutor("clientOutboundChannel", Runtime.getRuntime().availableProcessors() * 2, Integer.MAX_VALUE, 60);
    subscriberTypes = Arrays.asList(StompBrokerRelayMessageHandler.class, UserDestinationMessageHandler.class);
    testChannel("brokerChannel", subscriberTypes, 1);
    assertThatExceptionOfType(NoSuchBeanDefinitionException.class).isThrownBy(() -> this.appContext.getBean("brokerChannelExecutor", ThreadPoolTaskExecutor.class));
    String destination = "/topic/unresolved-user-destination";
    UserDestinationMessageHandler userDestHandler = this.appContext.getBean(UserDestinationMessageHandler.class);
    assertThat(userDestHandler.getBroadcastDestination()).isEqualTo(destination);
    assertThat(messageBroker.getSystemSubscriptions()).isNotNull();
    assertThat(messageBroker.getSystemSubscriptions().get(destination)).isSameAs(userDestHandler);
    destination = "/topic/simp-user-registry";
    UserRegistryMessageHandler userRegistryHandler = this.appContext.getBean(UserRegistryMessageHandler.class);
    assertThat(userRegistryHandler.getBroadcastDestination()).isEqualTo(destination);
    assertThat(messageBroker.getSystemSubscriptions()).isNotNull();
    assertThat(messageBroker.getSystemSubscriptions().get(destination)).isSameAs(userRegistryHandler);
    SimpUserRegistry userRegistry = this.appContext.getBean(SimpUserRegistry.class);
    assertThat(userRegistry.getClass()).isEqualTo(MultiServerUserRegistry.class);
    String name = "webSocketMessageBrokerStats";
    WebSocketMessageBrokerStats stats = this.appContext.getBean(name, WebSocketMessageBrokerStats.class);
    String actual = stats.toString();
    String expected = "WebSocketSession\\[0 current WS\\(0\\)-HttpStream\\(0\\)-HttpPoll\\(0\\), " + "0 total, 0 closed abnormally \\(0 connect failure, 0 send limit, 0 transport error\\)], " + "stompSubProtocol\\[processed CONNECT\\(0\\)-CONNECTED\\(0\\)-DISCONNECT\\(0\\)], " + "stompBrokerRelay\\[0 sessions, relayhost:1234 \\(not available\\), " + "processed CONNECT\\(0\\)-CONNECTED\\(0\\)-DISCONNECT\\(0\\)], " + "inboundChannel\\[pool size = \\d, active threads = \\d, queued tasks = \\d, " + "completed tasks = \\d], " + "outboundChannel\\[pool size = \\d, active threads = \\d, queued tasks = \\d, " + "completed tasks = \\d], " + "sockJsScheduler\\[pool size = \\d, active threads = \\d, queued tasks = \\d, " + "completed tasks = \\d]";
    assertThat(actual.matches(expected)).as("\nExpected: " + expected.replace("\\", "") + "\n  Actual: " + actual).isTrue();
}
Also used : UserDestinationResolver(org.springframework.messaging.simp.user.UserDestinationResolver) DefaultUserDestinationResolver(org.springframework.messaging.simp.user.DefaultUserDestinationResolver) StompBrokerRelayMessageHandler(org.springframework.messaging.simp.stomp.StompBrokerRelayMessageHandler) MessageHandler(org.springframework.messaging.MessageHandler) SimpleBrokerMessageHandler(org.springframework.messaging.simp.broker.SimpleBrokerMessageHandler) UserRegistryMessageHandler(org.springframework.messaging.simp.user.UserRegistryMessageHandler) UserDestinationMessageHandler(org.springframework.messaging.simp.user.UserDestinationMessageHandler) SimpAnnotationMethodMessageHandler(org.springframework.messaging.simp.annotation.support.SimpAnnotationMethodMessageHandler) UserRegistryMessageHandler(org.springframework.messaging.simp.user.UserRegistryMessageHandler) WebSocketHttpRequestHandler(org.springframework.web.socket.server.support.WebSocketHttpRequestHandler) SockJsHttpRequestHandler(org.springframework.web.socket.sockjs.support.SockJsHttpRequestHandler) HttpRequestHandler(org.springframework.web.HttpRequestHandler) StompBrokerRelayMessageHandler(org.springframework.messaging.simp.stomp.StompBrokerRelayMessageHandler) UserDestinationMessageHandler(org.springframework.messaging.simp.user.UserDestinationMessageHandler) SimpUserRegistry(org.springframework.messaging.simp.user.SimpUserRegistry) DefaultSimpUserRegistry(org.springframework.web.socket.messaging.DefaultSimpUserRegistry) SimpleUrlHandlerMapping(org.springframework.web.servlet.handler.SimpleUrlHandlerMapping) SockJsHttpRequestHandler(org.springframework.web.socket.sockjs.support.SockJsHttpRequestHandler) SimpleUrlHandlerMapping(org.springframework.web.servlet.handler.SimpleUrlHandlerMapping) HandlerMapping(org.springframework.web.servlet.HandlerMapping) SubProtocolWebSocketHandler(org.springframework.web.socket.messaging.SubProtocolWebSocketHandler) WebSocketHandler(org.springframework.web.socket.WebSocketHandler) SubProtocolWebSocketHandler(org.springframework.web.socket.messaging.SubProtocolWebSocketHandler) ThreadPoolTaskExecutor(org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor) NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException) DefaultUserDestinationResolver(org.springframework.messaging.simp.user.DefaultUserDestinationResolver) Test(org.junit.jupiter.api.Test)

Aggregations

StompBrokerRelayMessageHandler (org.springframework.messaging.simp.stomp.StompBrokerRelayMessageHandler)10 Bean (org.springframework.context.annotation.Bean)4 AbstractBrokerMessageHandler (org.springframework.messaging.simp.broker.AbstractBrokerMessageHandler)4 SimpleBrokerMessageHandler (org.springframework.messaging.simp.broker.SimpleBrokerMessageHandler)4 UserDestinationMessageHandler (org.springframework.messaging.simp.user.UserDestinationMessageHandler)4 UserRegistryMessageHandler (org.springframework.messaging.simp.user.UserRegistryMessageHandler)4 Test (org.junit.jupiter.api.Test)3 MessageHandler (org.springframework.messaging.MessageHandler)3 SimpAnnotationMethodMessageHandler (org.springframework.messaging.simp.annotation.support.SimpAnnotationMethodMessageHandler)3 HashMap (java.util.HashMap)2 Nullable (org.springframework.lang.Nullable)2 SimpUserRegistry (org.springframework.messaging.simp.user.SimpUserRegistry)2 WebSocketMessageBrokerStats (org.springframework.web.socket.config.WebSocketMessageBrokerStats)2 NoSuchBeanDefinitionException (org.springframework.beans.factory.NoSuchBeanDefinitionException)1 ApplicationContext (org.springframework.context.ApplicationContext)1 AnnotationConfigApplicationContext (org.springframework.context.annotation.AnnotationConfigApplicationContext)1 StaticApplicationContext (org.springframework.context.support.StaticApplicationContext)1 MessageChannel (org.springframework.messaging.MessageChannel)1 StubMessageChannel (org.springframework.messaging.StubMessageChannel)1 SubscribableChannel (org.springframework.messaging.SubscribableChannel)1