Search in sources :

Example 1 with SubscribableChannel

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

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

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

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

the class WebSocketAnnotationMethodMessageHandlerTests method setUp.

@Before
public void setUp() throws Exception {
    this.applicationContext = new StaticApplicationContext();
    this.applicationContext.registerSingleton("controller", TestController.class);
    this.applicationContext.registerSingleton("controllerAdvice", TestControllerAdvice.class);
    this.applicationContext.refresh();
    SubscribableChannel channel = Mockito.mock(SubscribableChannel.class);
    SimpMessageSendingOperations brokerTemplate = new SimpMessagingTemplate(channel);
    this.messageHandler = new TestWebSocketAnnotationMethodMessageHandler(brokerTemplate, channel, channel);
    this.messageHandler.setApplicationContext(this.applicationContext);
    this.messageHandler.afterPropertiesSet();
}
Also used : SimpMessagingTemplate(org.springframework.messaging.simp.SimpMessagingTemplate) StaticApplicationContext(org.springframework.context.support.StaticApplicationContext) SimpMessageSendingOperations(org.springframework.messaging.simp.SimpMessageSendingOperations) SubscribableChannel(org.springframework.messaging.SubscribableChannel) Before(org.junit.Before)

Example 5 with SubscribableChannel

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

the class SockJsWebSocketHandlerTests method getSubProtocols.

@Test
public void getSubProtocols() throws Exception {
    SubscribableChannel channel = mock(SubscribableChannel.class);
    SubProtocolWebSocketHandler handler = new SubProtocolWebSocketHandler(channel, channel);
    StompSubProtocolHandler stompHandler = new StompSubProtocolHandler();
    handler.addProtocolHandler(stompHandler);
    TaskScheduler scheduler = mock(TaskScheduler.class);
    DefaultSockJsService service = new DefaultSockJsService(scheduler);
    WebSocketServerSockJsSession session = new WebSocketServerSockJsSession("1", service, handler, null);
    SockJsWebSocketHandler sockJsHandler = new SockJsWebSocketHandler(service, handler, session);
    assertEquals(stompHandler.getSupportedProtocols(), sockJsHandler.getSubProtocols());
}
Also used : WebSocketServerSockJsSession(org.springframework.web.socket.sockjs.transport.session.WebSocketServerSockJsSession) SubProtocolWebSocketHandler(org.springframework.web.socket.messaging.SubProtocolWebSocketHandler) StompSubProtocolHandler(org.springframework.web.socket.messaging.StompSubProtocolHandler) TaskScheduler(org.springframework.scheduling.TaskScheduler) SubscribableChannel(org.springframework.messaging.SubscribableChannel) Test(org.junit.Test)

Aggregations

SubscribableChannel (org.springframework.messaging.SubscribableChannel)6 Test (org.junit.Test)4 MessageChannel (org.springframework.messaging.MessageChannel)3 StubMessageChannel (org.springframework.messaging.StubMessageChannel)3 Before (org.junit.Before)2 MessageHandler (org.springframework.messaging.MessageHandler)2 MessagingException (org.springframework.messaging.MessagingException)2 ExecutorSubscribableChannel (org.springframework.messaging.support.ExecutorSubscribableChannel)2 GenericMessage (org.springframework.messaging.support.GenericMessage)2 TaskScheduler (org.springframework.scheduling.TaskScheduler)2 SubProtocolWebSocketHandler (org.springframework.web.socket.messaging.SubProtocolWebSocketHandler)2 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 StaticApplicationContext (org.springframework.context.support.StaticApplicationContext)1 MessageDeliveryException (org.springframework.messaging.MessageDeliveryException)1 SimpMessageSendingOperations (org.springframework.messaging.simp.SimpMessageSendingOperations)1 SimpMessagingTemplate (org.springframework.messaging.simp.SimpMessagingTemplate)1 StompBrokerRelayMessageHandler (org.springframework.messaging.simp.stomp.StompBrokerRelayMessageHandler)1 StompSubProtocolHandler (org.springframework.web.socket.messaging.StompSubProtocolHandler)1 WebSocketServerSockJsSession (org.springframework.web.socket.sockjs.transport.session.WebSocketServerSockJsSession)1