Search in sources :

Example 26 with ChannelInterceptor

use of org.springframework.messaging.support.ChannelInterceptor in project spring-integration by spring-projects.

the class JmsChannelParserTests method topicChannelWithInterceptors.

@Test
@SuppressWarnings("unchecked")
public void topicChannelWithInterceptors() {
    assertEquals(SubscribableJmsChannel.class, topicChannelWithInterceptors.getClass());
    SubscribableJmsChannel channel = (SubscribableJmsChannel) topicChannelWithInterceptors;
    DirectFieldAccessor accessor = new DirectFieldAccessor(channel);
    List<ChannelInterceptor> interceptors = (List<ChannelInterceptor>) new DirectFieldAccessor(accessor.getPropertyValue("interceptors")).getPropertyValue("interceptors");
    assertEquals(2, interceptors.size());
    assertEquals(TestInterceptor.class, interceptors.get(0).getClass());
    assertEquals(TestInterceptor.class, interceptors.get(1).getClass());
}
Also used : SubscribableJmsChannel(org.springframework.integration.jms.SubscribableJmsChannel) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) ChannelInterceptor(org.springframework.messaging.support.ChannelInterceptor) List(java.util.List) Test(org.junit.Test)

Example 27 with ChannelInterceptor

use of org.springframework.messaging.support.ChannelInterceptor in project spring-integration by spring-projects.

the class PollableJmsChannelTests method queueNameWithTruePreReceiveInterceptors.

@Test
public void queueNameWithTruePreReceiveInterceptors() throws Exception {
    JmsChannelFactoryBean factoryBean = new JmsChannelFactoryBean(false);
    CachingConnectionFactory ccf = new CachingConnectionFactory(this.connectionFactory);
    ccf.setCacheConsumers(false);
    factoryBean.setConnectionFactory(ccf);
    factoryBean.setDestinationName("someDynamicQueue");
    factoryBean.setPubSubDomain(false);
    List<ChannelInterceptor> interceptorList = new ArrayList<ChannelInterceptor>();
    ChannelInterceptor interceptor = spy(new SampleInterceptor(true));
    interceptorList.add(interceptor);
    factoryBean.setInterceptors(interceptorList);
    factoryBean.setBeanFactory(mock(BeanFactory.class));
    factoryBean.afterPropertiesSet();
    PollableJmsChannel channel = (PollableJmsChannel) factoryBean.getObject();
    boolean sent1 = channel.send(new GenericMessage<String>("foo"));
    assertTrue(sent1);
    Message<?> result1 = channel.receive(10000);
    assertNotNull(result1);
    verify(interceptor, times(1)).preReceive(Mockito.any(MessageChannel.class));
    verify(interceptor, times(1)).postReceive(Mockito.any(Message.class), Mockito.any(MessageChannel.class));
}
Also used : Message(org.springframework.messaging.Message) TextMessage(javax.jms.TextMessage) GenericMessage(org.springframework.messaging.support.GenericMessage) ChannelInterceptor(org.springframework.messaging.support.ChannelInterceptor) ArrayList(java.util.ArrayList) JmsChannelFactoryBean(org.springframework.integration.jms.config.JmsChannelFactoryBean) MessageChannel(org.springframework.messaging.MessageChannel) CachingConnectionFactory(org.springframework.jms.connection.CachingConnectionFactory) BeanFactory(org.springframework.beans.factory.BeanFactory) Test(org.junit.Test)

Example 28 with ChannelInterceptor

use of org.springframework.messaging.support.ChannelInterceptor in project spring-framework by spring-projects.

the class StompSubProtocolHandlerTests method handleMessageFromClientWithImmutableMessageInterceptor.

@Test
public void handleMessageFromClientWithImmutableMessageInterceptor() {
    AtomicReference<Boolean> mutable = new AtomicReference<>();
    ExecutorSubscribableChannel channel = new ExecutorSubscribableChannel();
    channel.addInterceptor(new ChannelInterceptor() {

        @Override
        public Message<?> preSend(Message<?> message, MessageChannel channel) {
            mutable.set(MessageHeaderAccessor.getAccessor(message, MessageHeaderAccessor.class).isMutable());
            return message;
        }
    });
    channel.addInterceptor(new ImmutableMessageChannelInterceptor());
    StompSubProtocolHandler handler = new StompSubProtocolHandler();
    handler.afterSessionStarted(this.session, channel);
    TextMessage message = StompTextMessageBuilder.create(StompCommand.CONNECT).build();
    handler.handleMessageFromClient(this.session, message, channel);
    assertThat(mutable.get()).isNotNull();
    assertThat(mutable.get()).isTrue();
}
Also used : ExecutorSubscribableChannel(org.springframework.messaging.support.ExecutorSubscribableChannel) TextMessage(org.springframework.web.socket.TextMessage) Message(org.springframework.messaging.Message) WebSocketMessage(org.springframework.web.socket.WebSocketMessage) BinaryMessage(org.springframework.web.socket.BinaryMessage) ImmutableMessageChannelInterceptor(org.springframework.messaging.support.ImmutableMessageChannelInterceptor) ChannelInterceptor(org.springframework.messaging.support.ChannelInterceptor) ImmutableMessageChannelInterceptor(org.springframework.messaging.support.ImmutableMessageChannelInterceptor) AtomicReference(java.util.concurrent.atomic.AtomicReference) MessageChannel(org.springframework.messaging.MessageChannel) SimpMessageHeaderAccessor(org.springframework.messaging.simp.SimpMessageHeaderAccessor) MessageHeaderAccessor(org.springframework.messaging.support.MessageHeaderAccessor) TextMessage(org.springframework.web.socket.TextMessage) Test(org.junit.jupiter.api.Test)

Example 29 with ChannelInterceptor

use of org.springframework.messaging.support.ChannelInterceptor in project spring-framework by spring-projects.

the class StompSubProtocolHandlerTests method handleMessageFromClientWithoutImmutableMessageInterceptor.

@Test
public void handleMessageFromClientWithoutImmutableMessageInterceptor() {
    AtomicReference<Boolean> mutable = new AtomicReference<>();
    ExecutorSubscribableChannel channel = new ExecutorSubscribableChannel();
    channel.addInterceptor(new ChannelInterceptor() {

        @Override
        public Message<?> preSend(Message<?> message, MessageChannel channel) {
            mutable.set(MessageHeaderAccessor.getAccessor(message, MessageHeaderAccessor.class).isMutable());
            return message;
        }
    });
    StompSubProtocolHandler handler = new StompSubProtocolHandler();
    handler.afterSessionStarted(this.session, channel);
    TextMessage message = StompTextMessageBuilder.create(StompCommand.CONNECT).build();
    handler.handleMessageFromClient(this.session, message, channel);
    assertThat(mutable.get()).isNotNull();
    assertThat(mutable.get()).isFalse();
}
Also used : ExecutorSubscribableChannel(org.springframework.messaging.support.ExecutorSubscribableChannel) MessageChannel(org.springframework.messaging.MessageChannel) TextMessage(org.springframework.web.socket.TextMessage) Message(org.springframework.messaging.Message) WebSocketMessage(org.springframework.web.socket.WebSocketMessage) BinaryMessage(org.springframework.web.socket.BinaryMessage) ChannelInterceptor(org.springframework.messaging.support.ChannelInterceptor) ImmutableMessageChannelInterceptor(org.springframework.messaging.support.ImmutableMessageChannelInterceptor) AtomicReference(java.util.concurrent.atomic.AtomicReference) SimpMessageHeaderAccessor(org.springframework.messaging.simp.SimpMessageHeaderAccessor) MessageHeaderAccessor(org.springframework.messaging.support.MessageHeaderAccessor) TextMessage(org.springframework.web.socket.TextMessage) Test(org.junit.jupiter.api.Test)

Example 30 with ChannelInterceptor

use of org.springframework.messaging.support.ChannelInterceptor in project spring-framework by spring-projects.

the class WebSocketMessageBrokerConfigurationSupportTests method brokerChannel.

@Test
void brokerChannel() {
    ApplicationContext context = createContext(TestChannelConfig.class, TestConfigurer.class);
    TestChannel channel = context.getBean("brokerChannel", TestChannel.class);
    Set<MessageHandler> handlers = channel.getSubscribers();
    List<ChannelInterceptor> interceptors = channel.getInterceptors();
    assertThat(interceptors.get(interceptors.size() - 1).getClass()).isEqualTo(ImmutableMessageChannelInterceptor.class);
    assertThat(handlers).hasSize(2);
    assertThat(handlers).contains(context.getBean(SimpleBrokerMessageHandler.class));
    assertThat(handlers).contains(context.getBean(UserDestinationMessageHandler.class));
}
Also used : ApplicationContext(org.springframework.context.ApplicationContext) AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) SimpleBrokerMessageHandler(org.springframework.messaging.simp.broker.SimpleBrokerMessageHandler) UserDestinationMessageHandler(org.springframework.messaging.simp.user.UserDestinationMessageHandler) MessageHandler(org.springframework.messaging.MessageHandler) ChannelInterceptor(org.springframework.messaging.support.ChannelInterceptor) ImmutableMessageChannelInterceptor(org.springframework.messaging.support.ImmutableMessageChannelInterceptor) SimpleBrokerMessageHandler(org.springframework.messaging.simp.broker.SimpleBrokerMessageHandler) UserDestinationMessageHandler(org.springframework.messaging.simp.user.UserDestinationMessageHandler) Test(org.junit.jupiter.api.Test)

Aggregations

ChannelInterceptor (org.springframework.messaging.support.ChannelInterceptor)30 Test (org.junit.Test)18 ChannelInterceptorAware (org.springframework.integration.channel.ChannelInterceptorAware)8 ApplicationContext (org.springframework.context.ApplicationContext)7 ArrayList (java.util.ArrayList)6 ImmutableMessageChannelInterceptor (org.springframework.messaging.support.ImmutableMessageChannelInterceptor)6 Test (org.junit.jupiter.api.Test)5 AnnotationConfigApplicationContext (org.springframework.context.annotation.AnnotationConfigApplicationContext)5 DirectChannel (org.springframework.integration.channel.DirectChannel)5 GlobalChannelInterceptorWrapper (org.springframework.integration.channel.interceptor.GlobalChannelInterceptorWrapper)5 Message (org.springframework.messaging.Message)5 Field (java.lang.reflect.Field)4 HashMap (java.util.HashMap)4 PartitionHandler (org.springframework.cloud.stream.binder.PartitionHandler)4 PartitionKeyExtractorStrategy (org.springframework.cloud.stream.binder.PartitionKeyExtractorStrategy)4 PartitionSelectorStrategy (org.springframework.cloud.stream.binder.PartitionSelectorStrategy)4 Source (org.springframework.cloud.stream.messaging.Source)4 CustomPartitionKeyExtractorClass (org.springframework.cloud.stream.partitioning.CustomPartitionKeyExtractorClass)4 CustomPartitionSelectorClass (org.springframework.cloud.stream.partitioning.CustomPartitionSelectorClass)4 PropertySource (org.springframework.context.annotation.PropertySource)4