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);
}
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);
}
}
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());
}
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();
}
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());
}
Aggregations