Search in sources :

Example 1 with MessageHandler

use of org.springframework.messaging.MessageHandler 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 MessageHandler

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

the class GenericMessagingTemplateTests method sendAndReceive.

@Test
public void sendAndReceive() {
    SubscribableChannel channel = new ExecutorSubscribableChannel(this.executor);
    channel.subscribe(new MessageHandler() {

        @Override
        public void handleMessage(Message<?> message) throws MessagingException {
            MessageChannel replyChannel = (MessageChannel) message.getHeaders().getReplyChannel();
            replyChannel.send(new GenericMessage<>("response"));
        }
    });
    String actual = this.template.convertSendAndReceive(channel, "request", String.class);
    assertEquals("response", actual);
}
Also used : ExecutorSubscribableChannel(org.springframework.messaging.support.ExecutorSubscribableChannel) GenericMessage(org.springframework.messaging.support.GenericMessage) MessageHandler(org.springframework.messaging.MessageHandler) MessageChannel(org.springframework.messaging.MessageChannel) StubMessageChannel(org.springframework.messaging.StubMessageChannel) MessagingException(org.springframework.messaging.MessagingException) SubscribableChannel(org.springframework.messaging.SubscribableChannel) ExecutorSubscribableChannel(org.springframework.messaging.support.ExecutorSubscribableChannel) Test(org.junit.Test)

Example 3 with MessageHandler

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

the class GenericMessagingTemplateTests method sendAndReceiveTimeout.

@Test
public void sendAndReceiveTimeout() throws InterruptedException {
    final AtomicReference<Throwable> failure = new AtomicReference<Throwable>();
    final CountDownLatch latch = new CountDownLatch(1);
    this.template.setReceiveTimeout(1);
    this.template.setThrowExceptionOnLateReply(true);
    SubscribableChannel channel = new ExecutorSubscribableChannel(this.executor);
    channel.subscribe(new MessageHandler() {

        @Override
        public void handleMessage(Message<?> message) throws MessagingException {
            try {
                Thread.sleep(500);
                MessageChannel replyChannel = (MessageChannel) message.getHeaders().getReplyChannel();
                replyChannel.send(new GenericMessage<>("response"));
                failure.set(new IllegalStateException("Expected exception"));
            } catch (InterruptedException e) {
                failure.set(e);
            } catch (MessageDeliveryException ex) {
                String expected = "Reply message received but the receiving thread has exited due to a timeout";
                String actual = ex.getMessage();
                if (!expected.equals(actual)) {
                    failure.set(new IllegalStateException("Unexpected error: '" + actual + "'"));
                }
            } finally {
                latch.countDown();
            }
        }
    });
    assertNull(this.template.convertSendAndReceive(channel, "request", String.class));
    assertTrue(latch.await(1000, TimeUnit.MILLISECONDS));
    Throwable ex = failure.get();
    if (ex != null) {
        throw new AssertionError(ex);
    }
}
Also used : ExecutorSubscribableChannel(org.springframework.messaging.support.ExecutorSubscribableChannel) MessageHandler(org.springframework.messaging.MessageHandler) MessagingException(org.springframework.messaging.MessagingException) AtomicReference(java.util.concurrent.atomic.AtomicReference) MessageDeliveryException(org.springframework.messaging.MessageDeliveryException) CountDownLatch(java.util.concurrent.CountDownLatch) GenericMessage(org.springframework.messaging.support.GenericMessage) MessageChannel(org.springframework.messaging.MessageChannel) StubMessageChannel(org.springframework.messaging.StubMessageChannel) SubscribableChannel(org.springframework.messaging.SubscribableChannel) ExecutorSubscribableChannel(org.springframework.messaging.support.ExecutorSubscribableChannel) Test(org.junit.Test)

Example 4 with MessageHandler

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

the class StompBrokerRelayMessageHandlerTests method systemSubscription.

@Test
public void systemSubscription() throws Exception {
    MessageHandler handler = mock(MessageHandler.class);
    this.brokerRelay.setSystemSubscriptions(Collections.singletonMap("/topic/foo", handler));
    this.brokerRelay.start();
    StompHeaderAccessor accessor = StompHeaderAccessor.create(StompCommand.CONNECTED);
    accessor.setLeaveMutable(true);
    MessageHeaders headers = accessor.getMessageHeaders();
    this.tcpClient.handleMessage(MessageBuilder.createMessage(new byte[0], headers));
    assertEquals(2, this.tcpClient.getSentMessages().size());
    assertEquals(StompCommand.CONNECT, this.tcpClient.getSentHeaders(0).getCommand());
    assertEquals(StompCommand.SUBSCRIBE, this.tcpClient.getSentHeaders(1).getCommand());
    assertEquals("/topic/foo", this.tcpClient.getSentHeaders(1).getDestination());
    Message<byte[]> message = message(StompCommand.MESSAGE, null, null, "/topic/foo");
    this.tcpClient.handleMessage(message);
    ArgumentCaptor<Message> captor = ArgumentCaptor.forClass(Message.class);
    verify(handler).handleMessage(captor.capture());
    assertSame(message, captor.getValue());
}
Also used : MessageHandler(org.springframework.messaging.MessageHandler) Message(org.springframework.messaging.Message) MessageHeaders(org.springframework.messaging.MessageHeaders) Test(org.junit.Test)

Example 5 with MessageHandler

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

the class WebSocketMessageBrokerConfigurationSupportTests method clientOutboundChannel.

@Test
public void clientOutboundChannel() {
    ApplicationContext config = createConfig(TestChannelConfig.class, TestConfigurer.class);
    TestChannel channel = config.getBean("clientOutboundChannel", TestChannel.class);
    Set<MessageHandler> handlers = channel.getSubscribers();
    List<ChannelInterceptor> interceptors = channel.getInterceptors();
    assertEquals(ImmutableMessageChannelInterceptor.class, interceptors.get(interceptors.size() - 1).getClass());
    assertEquals(1, handlers.size());
    assertTrue(handlers.contains(config.getBean(SubProtocolWebSocketHandler.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)

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