Search in sources :

Example 1 with SimpAnnotationMethodMessageHandler

use of org.springframework.messaging.simp.annotation.support.SimpAnnotationMethodMessageHandler in project spring-framework by spring-projects.

the class AbstractMessageBrokerConfiguration method simpAnnotationMethodMessageHandler.

@Bean
public SimpAnnotationMethodMessageHandler simpAnnotationMethodMessageHandler() {
    SimpAnnotationMethodMessageHandler handler = createAnnotationMethodMessageHandler();
    handler.setDestinationPrefixes(getBrokerRegistry().getApplicationDestinationPrefixes());
    handler.setMessageConverter(brokerMessageConverter());
    handler.setValidator(simpValidator());
    List<HandlerMethodArgumentResolver> argumentResolvers = new ArrayList<>();
    addArgumentResolvers(argumentResolvers);
    handler.setCustomArgumentResolvers(argumentResolvers);
    List<HandlerMethodReturnValueHandler> returnValueHandlers = new ArrayList<>();
    addReturnValueHandlers(returnValueHandlers);
    handler.setCustomReturnValueHandlers(returnValueHandlers);
    PathMatcher pathMatcher = getBrokerRegistry().getPathMatcher();
    if (pathMatcher != null) {
        handler.setPathMatcher(pathMatcher);
    }
    return handler;
}
Also used : HandlerMethodReturnValueHandler(org.springframework.messaging.handler.invocation.HandlerMethodReturnValueHandler) PathMatcher(org.springframework.util.PathMatcher) ArrayList(java.util.ArrayList) SimpAnnotationMethodMessageHandler(org.springframework.messaging.simp.annotation.support.SimpAnnotationMethodMessageHandler) HandlerMethodArgumentResolver(org.springframework.messaging.handler.invocation.HandlerMethodArgumentResolver) Bean(org.springframework.context.annotation.Bean)

Example 2 with SimpAnnotationMethodMessageHandler

use of org.springframework.messaging.simp.annotation.support.SimpAnnotationMethodMessageHandler 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();
    assertThat(validator).isNotNull();
    assertThat(validator).isSameAs(this.appContext.getBean("myValidator"));
    assertThat(validator).isInstanceOf(TestValidator.class);
    List<Class<? extends MessageHandler>> subscriberTypes = Arrays.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.asList(SimpleBrokerMessageHandler.class, UserDestinationMessageHandler.class);
    testChannel("brokerChannel", subscriberTypes, 1);
    testExecutor("brokerChannel", 102, 202, 602);
}
Also used : StompBrokerRelayMessageHandler(org.springframework.messaging.simp.stomp.StompBrokerRelayMessageHandler) MessageHandler(org.springframework.messaging.MessageHandler) SimpleBrokerMessageHandler(org.springframework.messaging.simp.broker.SimpleBrokerMessageHandler) UserRegistryMessageHandler(org.springframework.messaging.simp.user.UserRegistryMessageHandler) UserDestinationMessageHandler(org.springframework.messaging.simp.user.UserDestinationMessageHandler) SimpAnnotationMethodMessageHandler(org.springframework.messaging.simp.annotation.support.SimpAnnotationMethodMessageHandler) SubProtocolWebSocketHandler(org.springframework.web.socket.messaging.SubProtocolWebSocketHandler) SimpleBrokerMessageHandler(org.springframework.messaging.simp.broker.SimpleBrokerMessageHandler) SimpAnnotationMethodMessageHandler(org.springframework.messaging.simp.annotation.support.SimpAnnotationMethodMessageHandler) UserDestinationMessageHandler(org.springframework.messaging.simp.user.UserDestinationMessageHandler) Validator(org.springframework.validation.Validator) Test(org.junit.jupiter.api.Test)

Example 3 with SimpAnnotationMethodMessageHandler

use of org.springframework.messaging.simp.annotation.support.SimpAnnotationMethodMessageHandler in project spring-framework by spring-projects.

the class AbstractMessageBrokerConfiguration method simpAnnotationMethodMessageHandler.

@Bean
public SimpAnnotationMethodMessageHandler simpAnnotationMethodMessageHandler(AbstractSubscribableChannel clientInboundChannel, AbstractSubscribableChannel clientOutboundChannel, SimpMessagingTemplate brokerMessagingTemplate, CompositeMessageConverter brokerMessageConverter) {
    SimpAnnotationMethodMessageHandler handler = createAnnotationMethodMessageHandler(clientInboundChannel, clientOutboundChannel, brokerMessagingTemplate);
    MessageBrokerRegistry brokerRegistry = getBrokerRegistry(clientInboundChannel, clientOutboundChannel);
    handler.setDestinationPrefixes(brokerRegistry.getApplicationDestinationPrefixes());
    handler.setMessageConverter(brokerMessageConverter);
    handler.setValidator(simpValidator());
    List<HandlerMethodArgumentResolver> argumentResolvers = new ArrayList<>();
    addArgumentResolvers(argumentResolvers);
    handler.setCustomArgumentResolvers(argumentResolvers);
    List<HandlerMethodReturnValueHandler> returnValueHandlers = new ArrayList<>();
    addReturnValueHandlers(returnValueHandlers);
    handler.setCustomReturnValueHandlers(returnValueHandlers);
    PathMatcher pathMatcher = brokerRegistry.getPathMatcher();
    if (pathMatcher != null) {
        handler.setPathMatcher(pathMatcher);
    }
    return handler;
}
Also used : HandlerMethodReturnValueHandler(org.springframework.messaging.handler.invocation.HandlerMethodReturnValueHandler) PathMatcher(org.springframework.util.PathMatcher) ArrayList(java.util.ArrayList) SimpAnnotationMethodMessageHandler(org.springframework.messaging.simp.annotation.support.SimpAnnotationMethodMessageHandler) HandlerMethodArgumentResolver(org.springframework.messaging.handler.invocation.HandlerMethodArgumentResolver) Bean(org.springframework.context.annotation.Bean)

Example 4 with SimpAnnotationMethodMessageHandler

use of org.springframework.messaging.simp.annotation.support.SimpAnnotationMethodMessageHandler in project spring-framework by spring-projects.

the class MessageBrokerConfigurationTests method customArgumentAndReturnValueTypes.

@Test
public void customArgumentAndReturnValueTypes() {
    ApplicationContext context = loadConfig(CustomConfig.class);
    SimpAnnotationMethodMessageHandler handler = context.getBean(SimpAnnotationMethodMessageHandler.class);
    List<HandlerMethodArgumentResolver> customResolvers = handler.getCustomArgumentResolvers();
    assertThat(customResolvers.size()).isEqualTo(1);
    assertThat(handler.getArgumentResolvers().contains(customResolvers.get(0))).isTrue();
    List<HandlerMethodReturnValueHandler> customHandlers = handler.getCustomReturnValueHandlers();
    assertThat(customHandlers.size()).isEqualTo(1);
    assertThat(handler.getReturnValueHandlers().contains(customHandlers.get(0))).isTrue();
}
Also used : HandlerMethodReturnValueHandler(org.springframework.messaging.handler.invocation.HandlerMethodReturnValueHandler) AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) StaticApplicationContext(org.springframework.context.support.StaticApplicationContext) ApplicationContext(org.springframework.context.ApplicationContext) SimpAnnotationMethodMessageHandler(org.springframework.messaging.simp.annotation.support.SimpAnnotationMethodMessageHandler) HandlerMethodArgumentResolver(org.springframework.messaging.handler.invocation.HandlerMethodArgumentResolver) Test(org.junit.jupiter.api.Test)

Example 5 with SimpAnnotationMethodMessageHandler

use of org.springframework.messaging.simp.annotation.support.SimpAnnotationMethodMessageHandler in project spring-framework by spring-projects.

the class MessageBrokerConfigurationTests method clientOutboundChannelUsedByAnnotatedMethod.

@Test
public void clientOutboundChannelUsedByAnnotatedMethod() {
    ApplicationContext context = loadConfig(SimpleBrokerConfig.class);
    TestChannel channel = context.getBean("clientOutboundChannel", TestChannel.class);
    SimpAnnotationMethodMessageHandler messageHandler = context.getBean(SimpAnnotationMethodMessageHandler.class);
    StompHeaderAccessor headers = StompHeaderAccessor.create(StompCommand.SUBSCRIBE);
    headers.setSessionId("sess1");
    headers.setSessionAttributes(new ConcurrentHashMap<>());
    headers.setSubscriptionId("subs1");
    headers.setDestination("/foo");
    Message<?> message = MessageBuilder.withPayload(new byte[0]).setHeaders(headers).build();
    messageHandler.handleMessage(message);
    message = channel.messages.get(0);
    headers = StompHeaderAccessor.wrap(message);
    assertThat(headers.getMessageType()).isEqualTo(SimpMessageType.MESSAGE);
    assertThat(headers.getDestination()).isEqualTo("/foo");
    assertThat(new String((byte[]) message.getPayload())).isEqualTo("bar");
}
Also used : AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) StaticApplicationContext(org.springframework.context.support.StaticApplicationContext) ApplicationContext(org.springframework.context.ApplicationContext) SimpAnnotationMethodMessageHandler(org.springframework.messaging.simp.annotation.support.SimpAnnotationMethodMessageHandler) StompHeaderAccessor(org.springframework.messaging.simp.stomp.StompHeaderAccessor) Test(org.junit.jupiter.api.Test)

Aggregations

SimpAnnotationMethodMessageHandler (org.springframework.messaging.simp.annotation.support.SimpAnnotationMethodMessageHandler)9 Test (org.junit.jupiter.api.Test)7 ApplicationContext (org.springframework.context.ApplicationContext)4 AnnotationConfigApplicationContext (org.springframework.context.annotation.AnnotationConfigApplicationContext)4 StaticApplicationContext (org.springframework.context.support.StaticApplicationContext)4 HandlerMethodArgumentResolver (org.springframework.messaging.handler.invocation.HandlerMethodArgumentResolver)4 HandlerMethodReturnValueHandler (org.springframework.messaging.handler.invocation.HandlerMethodReturnValueHandler)4 ArrayList (java.util.ArrayList)2 Bean (org.springframework.context.annotation.Bean)2 StompHeaderAccessor (org.springframework.messaging.simp.stomp.StompHeaderAccessor)2 PathMatcher (org.springframework.util.PathMatcher)2 DirectFieldAccessor (org.springframework.beans.DirectFieldAccessor)1 MessageHandler (org.springframework.messaging.MessageHandler)1 ByteArrayMessageConverter (org.springframework.messaging.converter.ByteArrayMessageConverter)1 CompositeMessageConverter (org.springframework.messaging.converter.CompositeMessageConverter)1 ContentTypeResolver (org.springframework.messaging.converter.ContentTypeResolver)1 DefaultContentTypeResolver (org.springframework.messaging.converter.DefaultContentTypeResolver)1 MappingJackson2MessageConverter (org.springframework.messaging.converter.MappingJackson2MessageConverter)1 MessageConverter (org.springframework.messaging.converter.MessageConverter)1 StringMessageConverter (org.springframework.messaging.converter.StringMessageConverter)1