Search in sources :

Example 1 with MessageChannel

use of org.springframework.messaging.MessageChannel 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 2 with MessageChannel

use of org.springframework.messaging.MessageChannel 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 3 with MessageChannel

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

the class StompBrokerRelayRegistrationTests method test.

@Test
public void test() {
    SubscribableChannel inChannel = new StubMessageChannel();
    MessageChannel outChannel = new StubMessageChannel();
    String[] prefixes = new String[] { "/foo", "/bar" };
    StompBrokerRelayRegistration registration = new StompBrokerRelayRegistration(inChannel, outChannel, prefixes);
    registration.setClientLogin("clientlogin");
    registration.setClientPasscode("clientpasscode");
    registration.setSystemLogin("syslogin");
    registration.setSystemPasscode("syspasscode");
    registration.setSystemHeartbeatReceiveInterval(123);
    registration.setSystemHeartbeatSendInterval(456);
    registration.setVirtualHost("example.org");
    StompBrokerRelayMessageHandler handler = registration.getMessageHandler(new StubMessageChannel());
    assertArrayEquals(prefixes, handler.getDestinationPrefixes().toArray(new String[2]));
    assertEquals("clientlogin", handler.getClientLogin());
    assertEquals("clientpasscode", handler.getClientPasscode());
    assertEquals("syslogin", handler.getSystemLogin());
    assertEquals("syspasscode", handler.getSystemPasscode());
    assertEquals(123, handler.getSystemHeartbeatReceiveInterval());
    assertEquals(456, handler.getSystemHeartbeatSendInterval());
    assertEquals("example.org", handler.getVirtualHost());
}
Also used : MessageChannel(org.springframework.messaging.MessageChannel) StubMessageChannel(org.springframework.messaging.StubMessageChannel) StompBrokerRelayMessageHandler(org.springframework.messaging.simp.stomp.StompBrokerRelayMessageHandler) StubMessageChannel(org.springframework.messaging.StubMessageChannel) SubscribableChannel(org.springframework.messaging.SubscribableChannel) Test(org.junit.Test)

Example 4 with MessageChannel

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

the class ChannelInterceptorTests method postSendInterceptorMessageWasSent.

@Test
public void postSendInterceptorMessageWasSent() {
    final AtomicBoolean preSendInvoked = new AtomicBoolean(false);
    final AtomicBoolean completionInvoked = new AtomicBoolean(false);
    this.channel.addInterceptor(new ChannelInterceptorAdapter() {

        @Override
        public void postSend(Message<?> message, MessageChannel channel, boolean sent) {
            assertInput(message, channel, sent);
            preSendInvoked.set(true);
        }

        @Override
        public void afterSendCompletion(Message<?> message, MessageChannel channel, boolean sent, Exception ex) {
            assertInput(message, channel, sent);
            completionInvoked.set(true);
        }

        private void assertInput(Message<?> message, MessageChannel channel, boolean sent) {
            assertNotNull(message);
            assertNotNull(channel);
            assertSame(ChannelInterceptorTests.this.channel, channel);
            assertTrue(sent);
        }
    });
    this.channel.send(MessageBuilder.withPayload("test").build());
    assertTrue(preSendInvoked.get());
    assertTrue(completionInvoked.get());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MessageChannel(org.springframework.messaging.MessageChannel) MessagingException(org.springframework.messaging.MessagingException) Test(org.junit.Test)

Example 5 with MessageChannel

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

the class StompSubProtocolHandlerTests method webSocketScope.

@Test
public void webSocketScope() {
    Runnable runnable = Mockito.mock(Runnable.class);
    SimpAttributes simpAttributes = new SimpAttributes(this.session.getId(), this.session.getAttributes());
    simpAttributes.setAttribute("name", "value");
    simpAttributes.registerDestructionCallback("name", runnable);
    MessageChannel testChannel = new MessageChannel() {

        @Override
        public boolean send(Message<?> message) {
            SimpAttributes simpAttributes = SimpAttributesContextHolder.currentAttributes();
            assertThat(simpAttributes.getAttribute("name"), is("value"));
            return true;
        }

        @Override
        public boolean send(Message<?> message, long timeout) {
            return false;
        }
    };
    this.protocolHandler.afterSessionStarted(this.session, this.channel);
    StompHeaderAccessor headers = StompHeaderAccessor.create(StompCommand.CONNECT);
    Message<byte[]> message = MessageBuilder.createMessage(EMPTY_PAYLOAD, headers.getMessageHeaders());
    TextMessage textMessage = new TextMessage(new StompEncoder().encode(message));
    this.protocolHandler.handleMessageFromClient(this.session, textMessage, testChannel);
    assertEquals(Collections.<WebSocketMessage<?>>emptyList(), session.getSentMessages());
    this.protocolHandler.afterSessionEnded(this.session, CloseStatus.BAD_DATA, testChannel);
    assertEquals(Collections.<WebSocketMessage<?>>emptyList(), this.session.getSentMessages());
    verify(runnable, times(1)).run();
}
Also used : MessageChannel(org.springframework.messaging.MessageChannel) TextMessage(org.springframework.web.socket.TextMessage) Message(org.springframework.messaging.Message) WebSocketMessage(org.springframework.web.socket.WebSocketMessage) BinaryMessage(org.springframework.web.socket.BinaryMessage) SimpAttributes(org.springframework.messaging.simp.SimpAttributes) StompEncoder(org.springframework.messaging.simp.stomp.StompEncoder) StompHeaderAccessor(org.springframework.messaging.simp.stomp.StompHeaderAccessor) TextMessage(org.springframework.web.socket.TextMessage) Test(org.junit.Test)

Aggregations

MessageChannel (org.springframework.messaging.MessageChannel)20 Test (org.junit.Test)18 MessageHandler (org.springframework.messaging.MessageHandler)5 SimpMessageHeaderAccessor (org.springframework.messaging.simp.SimpMessageHeaderAccessor)5 GenericMessage (org.springframework.messaging.support.GenericMessage)5 CountDownLatch (java.util.concurrent.CountDownLatch)4 Message (org.springframework.messaging.Message)4 MessageDeliveryException (org.springframework.messaging.MessageDeliveryException)4 MessagingException (org.springframework.messaging.MessagingException)4 ExecutorSubscribableChannel (org.springframework.messaging.support.ExecutorSubscribableChannel)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)3 DirectChannel (org.springframework.integration.channel.DirectChannel)3 StubMessageChannel (org.springframework.messaging.StubMessageChannel)3 SubscribableChannel (org.springframework.messaging.SubscribableChannel)3 BinaryMessage (org.springframework.web.socket.BinaryMessage)3 TextMessage (org.springframework.web.socket.TextMessage)3 WebSocketMessage (org.springframework.web.socket.WebSocketMessage)3 HashMap (java.util.HashMap)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 Exchange (org.apache.camel.Exchange)2