Search in sources :

Example 6 with ChannelInterceptor

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

the class GlobalChannelInterceptorProcessorTests method testProcessorWithInterceptorNotMatchingPattern.

@Test
public void testProcessorWithInterceptorNotMatchingPattern() {
    Map<String, GlobalChannelInterceptorWrapper> interceptors = new HashMap<>();
    Map<String, ChannelInterceptorAware> channels = new HashMap<>();
    ChannelInterceptor channelInterceptor = Mockito.mock(ChannelInterceptor.class);
    GlobalChannelInterceptorWrapper globalChannelInterceptorWrapper = new GlobalChannelInterceptorWrapper(channelInterceptor);
    ChannelInterceptorAware channel = Mockito.mock(ChannelInterceptorAware.class);
    globalChannelInterceptorWrapper.setPatterns(new String[] { "te*" });
    interceptors.put("Test-1", globalChannelInterceptorWrapper);
    channels.put("Test-1", channel);
    when(this.beanFactory.getBeansOfType(GlobalChannelInterceptorWrapper.class)).thenReturn(interceptors);
    when(this.beanFactory.getBeansOfType(ChannelInterceptorAware.class)).thenReturn(channels);
    this.globalChannelInterceptorProcessor.afterSingletonsInstantiated();
    verify(channel, Mockito.never()).addInterceptor(channelInterceptor);
}
Also used : HashMap(java.util.HashMap) ChannelInterceptor(org.springframework.messaging.support.ChannelInterceptor) GlobalChannelInterceptorWrapper(org.springframework.integration.channel.interceptor.GlobalChannelInterceptorWrapper) ChannelInterceptorAware(org.springframework.integration.channel.ChannelInterceptorAware) Test(org.junit.Test)

Example 7 with ChannelInterceptor

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

the class GlobalChannelInterceptorProcessorTests method testProcessorWithInterceptorDefaultPattern.

@Test
public void testProcessorWithInterceptorDefaultPattern() {
    Map<String, GlobalChannelInterceptorWrapper> interceptors = new HashMap<>();
    Map<String, ChannelInterceptorAware> channels = new HashMap<>();
    ChannelInterceptor channelInterceptor = Mockito.mock(ChannelInterceptor.class);
    GlobalChannelInterceptorWrapper globalChannelInterceptorWrapper = new GlobalChannelInterceptorWrapper(channelInterceptor);
    ChannelInterceptorAware channel = Mockito.mock(ChannelInterceptorAware.class);
    interceptors.put("Test-1", globalChannelInterceptorWrapper);
    channels.put("Test-1", channel);
    when(this.beanFactory.getBeansOfType(GlobalChannelInterceptorWrapper.class)).thenReturn(interceptors);
    when(this.beanFactory.getBeansOfType(ChannelInterceptorAware.class)).thenReturn(channels);
    this.globalChannelInterceptorProcessor.afterSingletonsInstantiated();
    verify(channel).addInterceptor(channelInterceptor);
}
Also used : HashMap(java.util.HashMap) ChannelInterceptor(org.springframework.messaging.support.ChannelInterceptor) GlobalChannelInterceptorWrapper(org.springframework.integration.channel.interceptor.GlobalChannelInterceptorWrapper) ChannelInterceptorAware(org.springframework.integration.channel.ChannelInterceptorAware) Test(org.junit.Test)

Example 8 with ChannelInterceptor

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

the class PollableJmsChannelTests method queueNameWithFalsePreReceiveInterceptors.

@Test
public void queueNameWithFalsePreReceiveInterceptors() 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(false));
    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);
    assertNull(result1);
    verify(interceptor, times(1)).preReceive(Mockito.any(MessageChannel.class));
    verify(interceptor, times(0)).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 9 with ChannelInterceptor

use of org.springframework.messaging.support.ChannelInterceptor in project spring-cloud-stream by spring-cloud.

the class CustomPartitionedProducerTest method testCustomPartitionedProducer.

@Test
public void testCustomPartitionedProducer() {
    ApplicationContext context = SpringApplication.run(CustomPartitionedProducerTest.TestSource.class, "--spring.jmx.enabled=false", "--spring.main.web-application-type=none", "--spring.cloud.stream.bindings.output.producer.partitionKeyExtractorClass=org.springframework.cloud.stream.partitioning.CustomPartitionKeyExtractorClass", "--spring.cloud.stream.bindings.output.producer.partitionSelectorClass=org.springframework.cloud.stream.partitioning.CustomPartitionSelectorClass");
    Source testSource = context.getBean(Source.class);
    DirectChannel messageChannel = (DirectChannel) testSource.output();
    for (ChannelInterceptor channelInterceptor : messageChannel.getChannelInterceptors()) {
        if (channelInterceptor instanceof MessageConverterConfigurer.PartitioningInterceptor) {
            Field partitionHandlerField = ReflectionUtils.findField(MessageConverterConfigurer.PartitioningInterceptor.class, "partitionHandler");
            ReflectionUtils.makeAccessible(partitionHandlerField);
            PartitionHandler partitionHandler = (PartitionHandler) ReflectionUtils.getField(partitionHandlerField, channelInterceptor);
            Field partitonKeyExtractorField = ReflectionUtils.findField(PartitionHandler.class, "partitionKeyExtractorStrategy");
            ReflectionUtils.makeAccessible(partitonKeyExtractorField);
            Field partitonSelectorField = ReflectionUtils.findField(PartitionHandler.class, "partitionSelectorStrategy");
            ReflectionUtils.makeAccessible(partitonSelectorField);
            Assert.assertTrue(((PartitionKeyExtractorStrategy) ReflectionUtils.getField(partitonKeyExtractorField, partitionHandler)).getClass().equals(CustomPartitionKeyExtractorClass.class));
            Assert.assertTrue(((PartitionSelectorStrategy) ReflectionUtils.getField(partitonSelectorField, partitionHandler)).getClass().equals(CustomPartitionSelectorClass.class));
        }
    }
}
Also used : PartitionSelectorStrategy(org.springframework.cloud.stream.binder.PartitionSelectorStrategy) DirectChannel(org.springframework.integration.channel.DirectChannel) ChannelInterceptor(org.springframework.messaging.support.ChannelInterceptor) PartitionHandler(org.springframework.cloud.stream.binder.PartitionHandler) CustomPartitionSelectorClass(org.springframework.cloud.stream.partitioning.CustomPartitionSelectorClass) PropertySource(org.springframework.context.annotation.PropertySource) MessageSource(org.springframework.integration.core.MessageSource) Source(org.springframework.cloud.stream.messaging.Source) Field(java.lang.reflect.Field) PartitionKeyExtractorStrategy(org.springframework.cloud.stream.binder.PartitionKeyExtractorStrategy) CustomPartitionKeyExtractorClass(org.springframework.cloud.stream.partitioning.CustomPartitionKeyExtractorClass) ApplicationContext(org.springframework.context.ApplicationContext) Test(org.junit.Test)

Example 10 with ChannelInterceptor

use of org.springframework.messaging.support.ChannelInterceptor in project spring-cloud-stream by spring-cloud.

the class CustomPartitionedProducerTest method testCustomPartitionedProducerMultipleInstances.

public void testCustomPartitionedProducerMultipleInstances() {
    ApplicationContext context = SpringApplication.run(CustomPartitionedProducerTest.TestSourceMultipleStrategies.class, "--spring.jmx.enabled=false", "--spring.main.web-application-type=none", "--spring.cloud.stream.bindings.output.producer.partitionKeyExtractorName=customPartitionKeyExtractorOne", "--spring.cloud.stream.bindings.output.producer.partitionSelectorName=customPartitionSelectorTwo");
    Source testSource = context.getBean(Source.class);
    DirectChannel messageChannel = (DirectChannel) testSource.output();
    for (ChannelInterceptor channelInterceptor : messageChannel.getChannelInterceptors()) {
        if (channelInterceptor instanceof MessageConverterConfigurer.PartitioningInterceptor) {
            Field partitionHandlerField = ReflectionUtils.findField(MessageConverterConfigurer.PartitioningInterceptor.class, "partitionHandler");
            ReflectionUtils.makeAccessible(partitionHandlerField);
            PartitionHandler partitionHandler = (PartitionHandler) ReflectionUtils.getField(partitionHandlerField, channelInterceptor);
            Field partitonKeyExtractorField = ReflectionUtils.findField(PartitionHandler.class, "partitionKeyExtractorStrategy");
            ReflectionUtils.makeAccessible(partitonKeyExtractorField);
            Field partitonSelectorField = ReflectionUtils.findField(PartitionHandler.class, "partitionSelectorStrategy");
            ReflectionUtils.makeAccessible(partitonSelectorField);
            Assert.assertTrue(((PartitionKeyExtractorStrategy) ReflectionUtils.getField(partitonKeyExtractorField, partitionHandler)).getClass().equals(CustomPartitionKeyExtractorClass.class));
            Assert.assertTrue(((PartitionSelectorStrategy) ReflectionUtils.getField(partitonSelectorField, partitionHandler)).getClass().equals(CustomPartitionSelectorClass.class));
        }
    }
}
Also used : PartitionSelectorStrategy(org.springframework.cloud.stream.binder.PartitionSelectorStrategy) DirectChannel(org.springframework.integration.channel.DirectChannel) ChannelInterceptor(org.springframework.messaging.support.ChannelInterceptor) PartitionHandler(org.springframework.cloud.stream.binder.PartitionHandler) CustomPartitionSelectorClass(org.springframework.cloud.stream.partitioning.CustomPartitionSelectorClass) PropertySource(org.springframework.context.annotation.PropertySource) MessageSource(org.springframework.integration.core.MessageSource) Source(org.springframework.cloud.stream.messaging.Source) Field(java.lang.reflect.Field) PartitionKeyExtractorStrategy(org.springframework.cloud.stream.binder.PartitionKeyExtractorStrategy) CustomPartitionKeyExtractorClass(org.springframework.cloud.stream.partitioning.CustomPartitionKeyExtractorClass) ApplicationContext(org.springframework.context.ApplicationContext)

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