Search in sources :

Example 1 with ExecutorSubscribableChannel

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

the class AbstractMessageBrokerConfiguration method brokerChannel.

@Bean
public AbstractSubscribableChannel brokerChannel() {
    ChannelRegistration reg = getBrokerRegistry().getBrokerChannelRegistration();
    ExecutorSubscribableChannel channel = reg.hasTaskExecutor() ? new ExecutorSubscribableChannel(brokerChannelExecutor()) : new ExecutorSubscribableChannel();
    reg.setInterceptors(new ImmutableMessageChannelInterceptor());
    channel.setInterceptors(reg.getInterceptors());
    return channel;
}
Also used : ExecutorSubscribableChannel(org.springframework.messaging.support.ExecutorSubscribableChannel) ImmutableMessageChannelInterceptor(org.springframework.messaging.support.ImmutableMessageChannelInterceptor) Bean(org.springframework.context.annotation.Bean)

Example 2 with ExecutorSubscribableChannel

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

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

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

the class StompBrokerRelayMessageHandlerIntegrationTests method setUp.

@Before
public void setUp() throws Exception {
    logger.debug("Setting up before '" + this.testName.getMethodName() + "'");
    this.port = SocketUtils.findAvailableTcpPort(61613);
    this.responseChannel = new ExecutorSubscribableChannel();
    this.responseHandler = new TestMessageHandler();
    this.responseChannel.subscribe(this.responseHandler);
    this.eventPublisher = new TestEventPublisher();
    startActiveMqBroker();
    createAndStartRelay();
}
Also used : ExecutorSubscribableChannel(org.springframework.messaging.support.ExecutorSubscribableChannel) Before(org.junit.Before)

Example 5 with ExecutorSubscribableChannel

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

the class StompSubProtocolHandlerTests method handleMessageFromClientWithTokenAuthentication.

// SPR-14690
@Test
public void handleMessageFromClientWithTokenAuthentication() {
    ExecutorSubscribableChannel channel = new ExecutorSubscribableChannel();
    channel.addInterceptor(new AuthenticationInterceptor("__pete__@gmail.com"));
    channel.addInterceptor(new ImmutableMessageChannelInterceptor());
    TestMessageHandler messageHandler = new TestMessageHandler();
    channel.subscribe(messageHandler);
    StompSubProtocolHandler handler = new StompSubProtocolHandler();
    handler.afterSessionStarted(this.session, channel);
    TextMessage wsMessage = StompTextMessageBuilder.create(StompCommand.CONNECT).build();
    handler.handleMessageFromClient(this.session, wsMessage, channel);
    assertEquals(1, messageHandler.getMessages().size());
    Message<?> message = messageHandler.getMessages().get(0);
    Principal user = SimpMessageHeaderAccessor.getUser(message.getHeaders());
    assertNotNull(user);
    assertEquals("__pete__@gmail.com", user.getName());
}
Also used : ExecutorSubscribableChannel(org.springframework.messaging.support.ExecutorSubscribableChannel) ImmutableMessageChannelInterceptor(org.springframework.messaging.support.ImmutableMessageChannelInterceptor) TextMessage(org.springframework.web.socket.TextMessage) Principal(java.security.Principal) TestPrincipal(org.springframework.messaging.simp.TestPrincipal) Test(org.junit.Test)

Aggregations

ExecutorSubscribableChannel (org.springframework.messaging.support.ExecutorSubscribableChannel)11 Test (org.junit.Test)5 MessageChannel (org.springframework.messaging.MessageChannel)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)3 Bean (org.springframework.context.annotation.Bean)3 ImmutableMessageChannelInterceptor (org.springframework.messaging.support.ImmutableMessageChannelInterceptor)3 TextMessage (org.springframework.web.socket.TextMessage)3 Before (org.junit.Before)2 Message (org.springframework.messaging.Message)2 MessageHandler (org.springframework.messaging.MessageHandler)2 MessagingException (org.springframework.messaging.MessagingException)2 StubMessageChannel (org.springframework.messaging.StubMessageChannel)2 SubscribableChannel (org.springframework.messaging.SubscribableChannel)2 SimpMessageHeaderAccessor (org.springframework.messaging.simp.SimpMessageHeaderAccessor)2 ChannelInterceptorAdapter (org.springframework.messaging.support.ChannelInterceptorAdapter)2 GenericMessage (org.springframework.messaging.support.GenericMessage)2 MessageHeaderAccessor (org.springframework.messaging.support.MessageHeaderAccessor)2 BinaryMessage (org.springframework.web.socket.BinaryMessage)2 WebSocketMessage (org.springframework.web.socket.WebSocketMessage)2 Principal (java.security.Principal)1