use of org.springframework.messaging.MessageHandler in project spring-framework by spring-projects.
the class TestValidator method testChannel.
private void testChannel(String channelName, List<Class<? extends MessageHandler>> subscriberTypes, int interceptorCount) {
AbstractSubscribableChannel channel = this.appContext.getBean(channelName, AbstractSubscribableChannel.class);
for (Class<? extends MessageHandler> subscriberType : subscriberTypes) {
MessageHandler subscriber = this.appContext.getBean(subscriberType);
assertNotNull("No subsription for " + subscriberType, subscriber);
assertTrue(channel.hasSubscription(subscriber));
}
List<ChannelInterceptor> interceptors = channel.getInterceptors();
assertEquals(interceptorCount, interceptors.size());
assertEquals(ImmutableMessageChannelInterceptor.class, interceptors.get(interceptors.size() - 1).getClass());
}
use of org.springframework.messaging.MessageHandler in project spring-framework by spring-projects.
the class TestValidator method stompBrokerRelay.
@Test
public void stompBrokerRelay() {
loadBeanDefinitions("websocket-config-broker-relay.xml");
HandlerMapping hm = this.appContext.getBean(HandlerMapping.class);
assertNotNull(hm);
assertThat(hm, Matchers.instanceOf(SimpleUrlHandlerMapping.class));
SimpleUrlHandlerMapping suhm = (SimpleUrlHandlerMapping) hm;
assertThat(suhm.getUrlMap().keySet(), Matchers.hasSize(1));
assertThat(suhm.getUrlMap().values(), Matchers.hasSize(1));
assertEquals(2, suhm.getOrder());
HttpRequestHandler httpRequestHandler = (HttpRequestHandler) suhm.getUrlMap().get("/foo/**");
assertNotNull(httpRequestHandler);
assertThat(httpRequestHandler, Matchers.instanceOf(SockJsHttpRequestHandler.class));
SockJsHttpRequestHandler sockJsHttpRequestHandler = (SockJsHttpRequestHandler) httpRequestHandler;
WebSocketHandler wsHandler = unwrapWebSocketHandler(sockJsHttpRequestHandler.getWebSocketHandler());
assertNotNull(wsHandler);
assertThat(wsHandler, Matchers.instanceOf(SubProtocolWebSocketHandler.class));
assertNotNull(sockJsHttpRequestHandler.getSockJsService());
UserDestinationResolver userDestResolver = this.appContext.getBean(UserDestinationResolver.class);
assertNotNull(userDestResolver);
assertThat(userDestResolver, Matchers.instanceOf(DefaultUserDestinationResolver.class));
DefaultUserDestinationResolver defaultUserDestResolver = (DefaultUserDestinationResolver) userDestResolver;
assertEquals("/user/", defaultUserDestResolver.getDestinationPrefix());
StompBrokerRelayMessageHandler messageBroker = this.appContext.getBean(StompBrokerRelayMessageHandler.class);
assertNotNull(messageBroker);
assertEquals("clientlogin", messageBroker.getClientLogin());
assertEquals("clientpass", messageBroker.getClientPasscode());
assertEquals("syslogin", messageBroker.getSystemLogin());
assertEquals("syspass", messageBroker.getSystemPasscode());
assertEquals("relayhost", messageBroker.getRelayHost());
assertEquals(1234, messageBroker.getRelayPort());
assertEquals("spring.io", messageBroker.getVirtualHost());
assertEquals(5000, messageBroker.getSystemHeartbeatReceiveInterval());
assertEquals(5000, messageBroker.getSystemHeartbeatSendInterval());
assertThat(messageBroker.getDestinationPrefixes(), Matchers.containsInAnyOrder("/topic", "/queue"));
List<Class<? extends MessageHandler>> subscriberTypes = Arrays.<Class<? extends MessageHandler>>asList(SimpAnnotationMethodMessageHandler.class, UserDestinationMessageHandler.class, StompBrokerRelayMessageHandler.class);
testChannel("clientInboundChannel", subscriberTypes, 2);
testExecutor("clientInboundChannel", Runtime.getRuntime().availableProcessors() * 2, Integer.MAX_VALUE, 60);
subscriberTypes = Collections.singletonList(SubProtocolWebSocketHandler.class);
testChannel("clientOutboundChannel", subscriberTypes, 1);
testExecutor("clientOutboundChannel", Runtime.getRuntime().availableProcessors() * 2, Integer.MAX_VALUE, 60);
subscriberTypes = Arrays.<Class<? extends MessageHandler>>asList(StompBrokerRelayMessageHandler.class, UserDestinationMessageHandler.class);
testChannel("brokerChannel", subscriberTypes, 1);
try {
this.appContext.getBean("brokerChannelExecutor", ThreadPoolTaskExecutor.class);
fail("expected exception");
} catch (NoSuchBeanDefinitionException ex) {
// expected
}
String destination = "/topic/unresolved-user-destination";
UserDestinationMessageHandler userDestHandler = this.appContext.getBean(UserDestinationMessageHandler.class);
assertEquals(destination, userDestHandler.getBroadcastDestination());
assertNotNull(messageBroker.getSystemSubscriptions());
assertSame(userDestHandler, messageBroker.getSystemSubscriptions().get(destination));
destination = "/topic/simp-user-registry";
UserRegistryMessageHandler userRegistryHandler = this.appContext.getBean(UserRegistryMessageHandler.class);
assertEquals(destination, userRegistryHandler.getBroadcastDestination());
assertNotNull(messageBroker.getSystemSubscriptions());
assertSame(userRegistryHandler, messageBroker.getSystemSubscriptions().get(destination));
SimpUserRegistry userRegistry = this.appContext.getBean(SimpUserRegistry.class);
assertEquals(MultiServerUserRegistry.class, userRegistry.getClass());
String name = "webSocketMessageBrokerStats";
WebSocketMessageBrokerStats stats = this.appContext.getBean(name, WebSocketMessageBrokerStats.class);
String actual = stats.toString();
String expected = "WebSocketSession\\[0 current WS\\(0\\)-HttpStream\\(0\\)-HttpPoll\\(0\\), " + "0 total, 0 closed abnormally \\(0 connect failure, 0 send limit, 0 transport error\\)\\], " + "stompSubProtocol\\[processed CONNECT\\(0\\)-CONNECTED\\(0\\)-DISCONNECT\\(0\\)\\], " + "stompBrokerRelay\\[0 sessions, relayhost:1234 \\(not available\\), processed CONNECT\\(0\\)-CONNECTED\\(0\\)-DISCONNECT\\(0\\)\\], " + "inboundChannel\\[pool size = \\d, active threads = \\d, queued tasks = \\d, completed tasks = \\d\\], " + "outboundChannelpool size = \\d, active threads = \\d, queued tasks = \\d, completed tasks = \\d\\], " + "sockJsScheduler\\[pool size = \\d, active threads = \\d, queued tasks = \\d, completed tasks = \\d\\]";
assertTrue("\nExpected: " + expected.replace("\\", "") + "\n Actual: " + actual, actual.matches(expected));
}
use of org.springframework.messaging.MessageHandler in project spring-framework by spring-projects.
the class TestValidator method customChannels.
@Test
public void customChannels() {
loadBeanDefinitions("websocket-config-broker-customchannels.xml");
SimpAnnotationMethodMessageHandler annotationMethodMessageHandler = this.appContext.getBean(SimpAnnotationMethodMessageHandler.class);
Validator validator = annotationMethodMessageHandler.getValidator();
assertNotNull(validator);
assertSame(this.appContext.getBean("myValidator"), validator);
assertThat(validator, Matchers.instanceOf(TestValidator.class));
List<Class<? extends MessageHandler>> subscriberTypes = Arrays.<Class<? extends MessageHandler>>asList(SimpAnnotationMethodMessageHandler.class, UserDestinationMessageHandler.class, SimpleBrokerMessageHandler.class);
testChannel("clientInboundChannel", subscriberTypes, 3);
testExecutor("clientInboundChannel", 100, 200, 600);
subscriberTypes = Collections.singletonList(SubProtocolWebSocketHandler.class);
testChannel("clientOutboundChannel", subscriberTypes, 3);
testExecutor("clientOutboundChannel", 101, 201, 601);
subscriberTypes = Arrays.<Class<? extends MessageHandler>>asList(SimpleBrokerMessageHandler.class, UserDestinationMessageHandler.class);
testChannel("brokerChannel", subscriberTypes, 1);
testExecutor("brokerChannel", 102, 202, 602);
}
use of org.springframework.messaging.MessageHandler in project camel by apache.
the class CamelSourceAdapterTest method testSendingOneWayMessage.
@Test
public void testSendingOneWayMessage() throws Exception {
final CountDownLatch latch = new CountDownLatch(1);
DirectChannel channelA = getMandatoryBean(DirectChannel.class, "channelA");
channelA.subscribe(new MessageHandler() {
public void handleMessage(Message<?> message) {
latch.countDown();
assertEquals("We should get the message from channelA", message.getPayload(), "Willem");
}
});
template.sendBody("direct:OneWay", "Willem");
assertTrue(latch.await(1, TimeUnit.SECONDS));
}
use of org.springframework.messaging.MessageHandler in project camel by apache.
the class SpringIntegrationProducer method process.
public void process(final Exchange exchange) throws Exception {
if (exchange.getPattern().isOutCapable()) {
// we want to do in-out so the inputChannel is mandatory (used to receive reply from spring integration)
if (inputChannel == null) {
throw new IllegalArgumentException("InputChannel has not been configured on " + getEndpoint());
}
exchange.getIn().getHeaders().put(MessageHeaders.REPLY_CHANNEL, inputChannel);
// subscribe so we can receive the reply from spring integration
inputChannel.subscribe(new MessageHandler() {
public void handleMessage(Message<?> message) {
log.debug("Received {} from InputChannel: {}", message, inputChannel);
SpringIntegrationBinding.storeToCamelMessage(message, exchange.getOut());
}
});
}
org.springframework.messaging.Message<?> siOutmessage = SpringIntegrationBinding.createSpringIntegrationMessage(exchange);
// send the message to spring integration
log.debug("Sending {} to OutputChannel: {}", siOutmessage, outputChannel);
outputChannel.send(siOutmessage);
}
Aggregations