Search in sources :

Example 11 with MessageHandler

use of org.springframework.messaging.MessageHandler in project spring-framework by spring-projects.

the class MessageBrokerConfigurationTests method userBroadcastsDisabledWithSimpleBroker.

@Test
public void userBroadcastsDisabledWithSimpleBroker() throws Exception {
    SimpUserRegistry registry = this.simpleBrokerContext.getBean(SimpUserRegistry.class);
    assertNotNull(registry);
    assertNotEquals(MultiServerUserRegistry.class, registry.getClass());
    UserDestinationMessageHandler handler = this.simpleBrokerContext.getBean(UserDestinationMessageHandler.class);
    assertNull(handler.getBroadcastDestination());
    String name = "userRegistryMessageHandler";
    MessageHandler messageHandler = this.simpleBrokerContext.getBean(name, MessageHandler.class);
    assertNotEquals(UserRegistryMessageHandler.class, messageHandler.getClass());
}
Also used : 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) UserDestinationMessageHandler(org.springframework.messaging.simp.user.UserDestinationMessageHandler) SimpUserRegistry(org.springframework.messaging.simp.user.SimpUserRegistry) Test(org.junit.Test)

Example 12 with MessageHandler

use of org.springframework.messaging.MessageHandler in project spring-framework by spring-projects.

the class TestValidator method simpleBroker.

@Test
@SuppressWarnings("unchecked")
public void simpleBroker() throws Exception {
    loadBeanDefinitions("websocket-config-broker-simple.xml");
    HandlerMapping hm = this.appContext.getBean(HandlerMapping.class);
    assertThat(hm, Matchers.instanceOf(SimpleUrlHandlerMapping.class));
    SimpleUrlHandlerMapping suhm = (SimpleUrlHandlerMapping) hm;
    assertThat(suhm.getUrlMap().keySet(), Matchers.hasSize(4));
    assertThat(suhm.getUrlMap().values(), Matchers.hasSize(4));
    HttpRequestHandler httpRequestHandler = (HttpRequestHandler) suhm.getUrlMap().get("/foo");
    assertNotNull(httpRequestHandler);
    assertThat(httpRequestHandler, Matchers.instanceOf(WebSocketHttpRequestHandler.class));
    WebSocketHttpRequestHandler wsHttpRequestHandler = (WebSocketHttpRequestHandler) httpRequestHandler;
    HandshakeHandler handshakeHandler = wsHttpRequestHandler.getHandshakeHandler();
    assertNotNull(handshakeHandler);
    assertTrue(handshakeHandler instanceof TestHandshakeHandler);
    List<HandshakeInterceptor> interceptors = wsHttpRequestHandler.getHandshakeInterceptors();
    assertThat(interceptors, contains(instanceOf(FooTestInterceptor.class), instanceOf(BarTestInterceptor.class), instanceOf(OriginHandshakeInterceptor.class)));
    WebSocketSession session = new TestWebSocketSession("id");
    wsHttpRequestHandler.getWebSocketHandler().afterConnectionEstablished(session);
    assertEquals(true, session.getAttributes().get("decorated"));
    WebSocketHandler wsHandler = wsHttpRequestHandler.getWebSocketHandler();
    assertThat(wsHandler, Matchers.instanceOf(ExceptionWebSocketHandlerDecorator.class));
    wsHandler = ((ExceptionWebSocketHandlerDecorator) wsHandler).getDelegate();
    assertThat(wsHandler, Matchers.instanceOf(LoggingWebSocketHandlerDecorator.class));
    wsHandler = ((LoggingWebSocketHandlerDecorator) wsHandler).getDelegate();
    assertThat(wsHandler, Matchers.instanceOf(TestWebSocketHandlerDecorator.class));
    wsHandler = ((TestWebSocketHandlerDecorator) wsHandler).getDelegate();
    assertThat(wsHandler, Matchers.instanceOf(SubProtocolWebSocketHandler.class));
    assertSame(wsHandler, this.appContext.getBean(MessageBrokerBeanDefinitionParser.WEB_SOCKET_HANDLER_BEAN_NAME));
    SubProtocolWebSocketHandler subProtocolWsHandler = (SubProtocolWebSocketHandler) wsHandler;
    assertEquals(Arrays.asList("v10.stomp", "v11.stomp", "v12.stomp"), subProtocolWsHandler.getSubProtocols());
    assertEquals(25 * 1000, subProtocolWsHandler.getSendTimeLimit());
    assertEquals(1024 * 1024, subProtocolWsHandler.getSendBufferSizeLimit());
    Map<String, SubProtocolHandler> handlerMap = subProtocolWsHandler.getProtocolHandlerMap();
    StompSubProtocolHandler stompHandler = (StompSubProtocolHandler) handlerMap.get("v12.stomp");
    assertNotNull(stompHandler);
    assertEquals(128 * 1024, stompHandler.getMessageSizeLimit());
    assertNotNull(stompHandler.getErrorHandler());
    assertEquals(TestStompErrorHandler.class, stompHandler.getErrorHandler().getClass());
    assertNotNull(new DirectFieldAccessor(stompHandler).getPropertyValue("eventPublisher"));
    httpRequestHandler = (HttpRequestHandler) suhm.getUrlMap().get("/test/**");
    assertNotNull(httpRequestHandler);
    assertThat(httpRequestHandler, Matchers.instanceOf(SockJsHttpRequestHandler.class));
    SockJsHttpRequestHandler sockJsHttpRequestHandler = (SockJsHttpRequestHandler) httpRequestHandler;
    wsHandler = unwrapWebSocketHandler(sockJsHttpRequestHandler.getWebSocketHandler());
    assertNotNull(wsHandler);
    assertThat(wsHandler, Matchers.instanceOf(SubProtocolWebSocketHandler.class));
    assertNotNull(sockJsHttpRequestHandler.getSockJsService());
    assertThat(sockJsHttpRequestHandler.getSockJsService(), Matchers.instanceOf(DefaultSockJsService.class));
    DefaultSockJsService defaultSockJsService = (DefaultSockJsService) sockJsHttpRequestHandler.getSockJsService();
    WebSocketTransportHandler wsTransportHandler = (WebSocketTransportHandler) defaultSockJsService.getTransportHandlers().get(TransportType.WEBSOCKET);
    assertNotNull(wsTransportHandler.getHandshakeHandler());
    assertThat(wsTransportHandler.getHandshakeHandler(), Matchers.instanceOf(TestHandshakeHandler.class));
    assertFalse(defaultSockJsService.shouldSuppressCors());
    ThreadPoolTaskScheduler scheduler = (ThreadPoolTaskScheduler) defaultSockJsService.getTaskScheduler();
    assertEquals(Runtime.getRuntime().availableProcessors(), scheduler.getScheduledThreadPoolExecutor().getCorePoolSize());
    assertTrue(scheduler.getScheduledThreadPoolExecutor().getRemoveOnCancelPolicy());
    interceptors = defaultSockJsService.getHandshakeInterceptors();
    assertThat(interceptors, contains(instanceOf(FooTestInterceptor.class), instanceOf(BarTestInterceptor.class), instanceOf(OriginHandshakeInterceptor.class)));
    assertTrue(defaultSockJsService.getAllowedOrigins().contains("http://mydomain3.com"));
    assertTrue(defaultSockJsService.getAllowedOrigins().contains("http://mydomain4.com"));
    SimpUserRegistry userRegistry = this.appContext.getBean(SimpUserRegistry.class);
    assertNotNull(userRegistry);
    assertEquals(DefaultSimpUserRegistry.class, userRegistry.getClass());
    UserDestinationResolver userDestResolver = this.appContext.getBean(UserDestinationResolver.class);
    assertNotNull(userDestResolver);
    assertThat(userDestResolver, Matchers.instanceOf(DefaultUserDestinationResolver.class));
    DefaultUserDestinationResolver defaultUserDestResolver = (DefaultUserDestinationResolver) userDestResolver;
    assertEquals("/personal/", defaultUserDestResolver.getDestinationPrefix());
    UserDestinationMessageHandler userDestHandler = this.appContext.getBean(UserDestinationMessageHandler.class);
    assertNotNull(userDestHandler);
    SimpleBrokerMessageHandler brokerMessageHandler = this.appContext.getBean(SimpleBrokerMessageHandler.class);
    assertNotNull(brokerMessageHandler);
    Collection<String> prefixes = brokerMessageHandler.getDestinationPrefixes();
    assertEquals(Arrays.asList("/topic", "/queue"), new ArrayList<>(prefixes));
    assertNotNull(brokerMessageHandler.getTaskScheduler());
    assertArrayEquals(new long[] { 15000, 15000 }, brokerMessageHandler.getHeartbeatValue());
    List<Class<? extends MessageHandler>> subscriberTypes = Arrays.<Class<? extends MessageHandler>>asList(SimpAnnotationMethodMessageHandler.class, UserDestinationMessageHandler.class, SimpleBrokerMessageHandler.class);
    testChannel("clientInboundChannel", subscriberTypes, 2);
    testExecutor("clientInboundChannel", Runtime.getRuntime().availableProcessors() * 2, Integer.MAX_VALUE, 60);
    subscriberTypes = Collections.singletonList(SubProtocolWebSocketHandler.class);
    testChannel("clientOutboundChannel", subscriberTypes, 1);
    testExecutor("clientOutboundChannel", Runtime.getRuntime().availableProcessors() * 2, Integer.MAX_VALUE, 60);
    subscriberTypes = Arrays.<Class<? extends MessageHandler>>asList(SimpleBrokerMessageHandler.class, UserDestinationMessageHandler.class);
    testChannel("brokerChannel", subscriberTypes, 1);
    try {
        this.appContext.getBean("brokerChannelExecutor", ThreadPoolTaskExecutor.class);
        fail("expected exception");
    } catch (NoSuchBeanDefinitionException ex) {
    // expected
    }
    assertNotNull(this.appContext.getBean("webSocketScopeConfigurer", CustomScopeConfigurer.class));
    DirectFieldAccessor subscriptionRegistryAccessor = new DirectFieldAccessor(brokerMessageHandler.getSubscriptionRegistry());
    String pathSeparator = (String) new DirectFieldAccessor(subscriptionRegistryAccessor.getPropertyValue("pathMatcher")).getPropertyValue("pathSeparator");
    assertEquals(".", pathSeparator);
}
Also used : ExceptionWebSocketHandlerDecorator(org.springframework.web.socket.handler.ExceptionWebSocketHandlerDecorator) LoggingWebSocketHandlerDecorator(org.springframework.web.socket.handler.LoggingWebSocketHandlerDecorator) 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) WebSocketHttpRequestHandler(org.springframework.web.socket.server.support.WebSocketHttpRequestHandler) SockJsHttpRequestHandler(org.springframework.web.socket.sockjs.support.SockJsHttpRequestHandler) HttpRequestHandler(org.springframework.web.HttpRequestHandler) SimpleBrokerMessageHandler(org.springframework.messaging.simp.broker.SimpleBrokerMessageHandler) UserDestinationMessageHandler(org.springframework.messaging.simp.user.UserDestinationMessageHandler) SimpUserRegistry(org.springframework.messaging.simp.user.SimpUserRegistry) DefaultSimpUserRegistry(org.springframework.web.socket.messaging.DefaultSimpUserRegistry) HandshakeInterceptor(org.springframework.web.socket.server.HandshakeInterceptor) OriginHandshakeInterceptor(org.springframework.web.socket.server.support.OriginHandshakeInterceptor) SimpleUrlHandlerMapping(org.springframework.web.servlet.handler.SimpleUrlHandlerMapping) WebSocketHttpRequestHandler(org.springframework.web.socket.server.support.WebSocketHttpRequestHandler) TestWebSocketSession(org.springframework.web.socket.handler.TestWebSocketSession) WebSocketSession(org.springframework.web.socket.WebSocketSession) ThreadPoolTaskScheduler(org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler) HandshakeHandler(org.springframework.web.socket.server.HandshakeHandler) SockJsHttpRequestHandler(org.springframework.web.socket.sockjs.support.SockJsHttpRequestHandler) SimpleUrlHandlerMapping(org.springframework.web.servlet.handler.SimpleUrlHandlerMapping) HandlerMapping(org.springframework.web.servlet.HandlerMapping) TestWebSocketSession(org.springframework.web.socket.handler.TestWebSocketSession) StompSubProtocolHandler(org.springframework.web.socket.messaging.StompSubProtocolHandler) DefaultSockJsService(org.springframework.web.socket.sockjs.transport.handler.DefaultSockJsService) UserDestinationResolver(org.springframework.messaging.simp.user.UserDestinationResolver) DefaultUserDestinationResolver(org.springframework.messaging.simp.user.DefaultUserDestinationResolver) CustomScopeConfigurer(org.springframework.beans.factory.config.CustomScopeConfigurer) WebSocketTransportHandler(org.springframework.web.socket.sockjs.transport.handler.WebSocketTransportHandler) SubProtocolWebSocketHandler(org.springframework.web.socket.messaging.SubProtocolWebSocketHandler) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) WebSocketHandler(org.springframework.web.socket.WebSocketHandler) SubProtocolWebSocketHandler(org.springframework.web.socket.messaging.SubProtocolWebSocketHandler) SubProtocolHandler(org.springframework.web.socket.messaging.SubProtocolHandler) StompSubProtocolHandler(org.springframework.web.socket.messaging.StompSubProtocolHandler) NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException) DefaultUserDestinationResolver(org.springframework.messaging.simp.user.DefaultUserDestinationResolver) Test(org.junit.Test)

Example 13 with MessageHandler

use of org.springframework.messaging.MessageHandler in project spring-framework by spring-projects.

the class WebSocketMessageBrokerConfigurationSupportTests method brokerChannel.

@Test
public void brokerChannel() {
    ApplicationContext config = createConfig(TestChannelConfig.class, TestConfigurer.class);
    TestChannel channel = config.getBean("brokerChannel", TestChannel.class);
    Set<MessageHandler> handlers = channel.getSubscribers();
    List<ChannelInterceptor> interceptors = channel.getInterceptors();
    assertEquals(ImmutableMessageChannelInterceptor.class, interceptors.get(interceptors.size() - 1).getClass());
    assertEquals(2, handlers.size());
    assertTrue(handlers.contains(config.getBean(SimpleBrokerMessageHandler.class)));
    assertTrue(handlers.contains(config.getBean(UserDestinationMessageHandler.class)));
}
Also used : ApplicationContext(org.springframework.context.ApplicationContext) AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) SimpleBrokerMessageHandler(org.springframework.messaging.simp.broker.SimpleBrokerMessageHandler) UserDestinationMessageHandler(org.springframework.messaging.simp.user.UserDestinationMessageHandler) MessageHandler(org.springframework.messaging.MessageHandler) ChannelInterceptor(org.springframework.messaging.support.ChannelInterceptor) ImmutableMessageChannelInterceptor(org.springframework.messaging.support.ImmutableMessageChannelInterceptor) Test(org.junit.Test)

Example 14 with MessageHandler

use of org.springframework.messaging.MessageHandler in project camel by apache.

the class SpringIntegrationProducer method process.

public void process(final Exchange exchange) throws Exception {
    if (exchange.getPattern().isOutCapable()) {
        // we want to do in-out so the inputChannel is mandatory (used to receive reply from spring integration)
        if (inputChannel == null) {
            throw new IllegalArgumentException("InputChannel has not been configured on " + getEndpoint());
        }
        exchange.getIn().getHeaders().put(MessageHeaders.REPLY_CHANNEL, inputChannel);
        // subscribe so we can receive the reply from spring integration
        inputChannel.subscribe(new MessageHandler() {

            public void handleMessage(Message<?> message) {
                log.debug("Received {} from InputChannel: {}", message, inputChannel);
                SpringIntegrationBinding.storeToCamelMessage(message, exchange.getOut());
            }
        });
    }
    org.springframework.messaging.Message<?> siOutmessage = SpringIntegrationBinding.createSpringIntegrationMessage(exchange);
    // send the message to spring integration
    log.debug("Sending {} to OutputChannel: {}", siOutmessage, outputChannel);
    outputChannel.send(siOutmessage);
}
Also used : MessageHandler(org.springframework.messaging.MessageHandler)

Example 15 with MessageHandler

use of org.springframework.messaging.MessageHandler in project camel by apache.

the class SpringIntegrationTwoWayConsumerTest method testSendingTwoWayMessage.

@Test
public void testSendingTwoWayMessage() throws Exception {
    final CountDownLatch latch = new CountDownLatch(1);
    MessageChannel requestChannel = getMandatoryBean(MessageChannel.class, "requestChannel");
    Map<String, Object> maps = new HashMap<String, Object>();
    maps.put(MessageHeaders.REPLY_CHANNEL, "responseChannel");
    Message<String> message = new GenericMessage<String>(MESSAGE_BODY, maps);
    DirectChannel responseChannel = getMandatoryBean(DirectChannel.class, "responseChannel");
    responseChannel.subscribe(new MessageHandler() {

        public void handleMessage(Message<?> message) {
            latch.countDown();
            assertEquals("Get the wrong result", MESSAGE_BODY + " is processed", message.getPayload());
            assertEquals("Done", message.getHeaders().get("Status"));
        }
    });
    requestChannel.send(message);
    assertTrue(latch.await(1, TimeUnit.SECONDS));
}
Also used : GenericMessage(org.springframework.messaging.support.GenericMessage) MessageChannel(org.springframework.messaging.MessageChannel) MessageHandler(org.springframework.messaging.MessageHandler) HashMap(java.util.HashMap) DirectChannel(org.springframework.integration.channel.DirectChannel) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Aggregations

MessageHandler (org.springframework.messaging.MessageHandler)17 Test (org.junit.Test)14 SimpleBrokerMessageHandler (org.springframework.messaging.simp.broker.SimpleBrokerMessageHandler)8 UserDestinationMessageHandler (org.springframework.messaging.simp.user.UserDestinationMessageHandler)8 SimpAnnotationMethodMessageHandler (org.springframework.messaging.simp.annotation.support.SimpAnnotationMethodMessageHandler)6 StompBrokerRelayMessageHandler (org.springframework.messaging.simp.stomp.StompBrokerRelayMessageHandler)6 UserRegistryMessageHandler (org.springframework.messaging.simp.user.UserRegistryMessageHandler)6 CountDownLatch (java.util.concurrent.CountDownLatch)5 MessageChannel (org.springframework.messaging.MessageChannel)5 GenericMessage (org.springframework.messaging.support.GenericMessage)5 DirectChannel (org.springframework.integration.channel.DirectChannel)4 HashMap (java.util.HashMap)3 SimpUserRegistry (org.springframework.messaging.simp.user.SimpUserRegistry)3 ChannelInterceptor (org.springframework.messaging.support.ChannelInterceptor)3 ImmutableMessageChannelInterceptor (org.springframework.messaging.support.ImmutableMessageChannelInterceptor)3 SubProtocolWebSocketHandler (org.springframework.web.socket.messaging.SubProtocolWebSocketHandler)3 NoSuchBeanDefinitionException (org.springframework.beans.factory.NoSuchBeanDefinitionException)2 ApplicationContext (org.springframework.context.ApplicationContext)2 AnnotationConfigApplicationContext (org.springframework.context.annotation.AnnotationConfigApplicationContext)2 MessageDeliveryException (org.springframework.messaging.MessageDeliveryException)2