use of org.springframework.messaging.MessageHandler in project spring-framework by spring-projects.
the class AbstractMessageBrokerConfiguration method stompBrokerRelayMessageHandler.
@Bean
public AbstractBrokerMessageHandler stompBrokerRelayMessageHandler() {
StompBrokerRelayMessageHandler handler = getBrokerRegistry().getStompBrokerRelay(brokerChannel());
if (handler == null) {
return new NoOpBrokerMessageHandler();
}
Map<String, MessageHandler> subscriptions = new HashMap<>(1);
String destination = getBrokerRegistry().getUserDestinationBroadcast();
if (destination != null) {
subscriptions.put(destination, userDestinationMessageHandler());
}
destination = getBrokerRegistry().getUserRegistryBroadcast();
if (destination != null) {
subscriptions.put(destination, userRegistryMessageHandler());
}
handler.setSystemSubscriptions(subscriptions);
return handler;
}
use of org.springframework.messaging.MessageHandler 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.MessageHandler 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.MessageHandler in project spring-framework by spring-projects.
the class StompBrokerRelayMessageHandlerTests method systemSubscription.
@Test
public void systemSubscription() throws Exception {
MessageHandler handler = mock(MessageHandler.class);
this.brokerRelay.setSystemSubscriptions(Collections.singletonMap("/topic/foo", handler));
this.brokerRelay.start();
StompHeaderAccessor accessor = StompHeaderAccessor.create(StompCommand.CONNECTED);
accessor.setLeaveMutable(true);
MessageHeaders headers = accessor.getMessageHeaders();
this.tcpClient.handleMessage(MessageBuilder.createMessage(new byte[0], headers));
assertEquals(2, this.tcpClient.getSentMessages().size());
assertEquals(StompCommand.CONNECT, this.tcpClient.getSentHeaders(0).getCommand());
assertEquals(StompCommand.SUBSCRIBE, this.tcpClient.getSentHeaders(1).getCommand());
assertEquals("/topic/foo", this.tcpClient.getSentHeaders(1).getDestination());
Message<byte[]> message = message(StompCommand.MESSAGE, null, null, "/topic/foo");
this.tcpClient.handleMessage(message);
ArgumentCaptor<Message> captor = ArgumentCaptor.forClass(Message.class);
verify(handler).handleMessage(captor.capture());
assertSame(message, captor.getValue());
}
use of org.springframework.messaging.MessageHandler in project spring-framework by spring-projects.
the class WebSocketMessageBrokerConfigurationSupportTests method clientOutboundChannel.
@Test
public void clientOutboundChannel() {
ApplicationContext config = createConfig(TestChannelConfig.class, TestConfigurer.class);
TestChannel channel = config.getBean("clientOutboundChannel", TestChannel.class);
Set<MessageHandler> handlers = channel.getSubscribers();
List<ChannelInterceptor> interceptors = channel.getInterceptors();
assertEquals(ImmutableMessageChannelInterceptor.class, interceptors.get(interceptors.size() - 1).getClass());
assertEquals(1, handlers.size());
assertTrue(handlers.contains(config.getBean(SubProtocolWebSocketHandler.class)));
}
Aggregations