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