Search in sources :

Example 21 with ChannelInterceptor

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

the class EnableIntegrationTests method testParentChildAnnotationConfiguration.

@Test
public void testParentChildAnnotationConfiguration() {
    AnnotationConfigApplicationContext child = new AnnotationConfigApplicationContext();
    child.register(ChildConfiguration.class);
    child.setParent(this.context);
    child.refresh();
    AbstractMessageChannel foo = child.getBean("foo", AbstractMessageChannel.class);
    ChannelInterceptor baz = child.getBean("baz", ChannelInterceptor.class);
    assertTrue(foo.getChannelInterceptors().contains(baz));
    assertFalse(this.output.getChannelInterceptors().contains(baz));
    child.close();
}
Also used : AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) AbstractMessageChannel(org.springframework.integration.channel.AbstractMessageChannel) GlobalChannelInterceptor(org.springframework.integration.config.GlobalChannelInterceptor) ChannelInterceptor(org.springframework.messaging.support.ChannelInterceptor) Test(org.junit.Test)

Example 22 with ChannelInterceptor

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

the class GlobalChannelInterceptorProcessor method addMatchingInterceptors.

/**
 * Add any interceptor whose pattern matches against the channel's name.
 * @param channel the message channel to add interceptors.
 * @param beanName the message channel bean name to match the pattern.
 */
public void addMatchingInterceptors(ChannelInterceptorAware channel, String beanName) {
    if (logger.isDebugEnabled()) {
        logger.debug("Applying global interceptors on channel '" + beanName + "'");
    }
    List<GlobalChannelInterceptorWrapper> tempInterceptors = new ArrayList<>();
    for (GlobalChannelInterceptorWrapper globalChannelInterceptorWrapper : this.positiveOrderInterceptors) {
        String[] patterns = globalChannelInterceptorWrapper.getPatterns();
        patterns = StringUtils.trimArrayElements(patterns);
        if (beanName != null && Boolean.TRUE.equals(PatternMatchUtils.smartMatch(beanName, patterns))) {
            tempInterceptors.add(globalChannelInterceptorWrapper);
        }
    }
    Collections.sort(tempInterceptors, this.comparator);
    for (GlobalChannelInterceptorWrapper next : tempInterceptors) {
        ChannelInterceptor channelInterceptor = next.getChannelInterceptor();
        if (!(channelInterceptor instanceof VetoCapableInterceptor) || ((VetoCapableInterceptor) channelInterceptor).shouldIntercept(beanName, channel)) {
            channel.addInterceptor(channelInterceptor);
        }
    }
    tempInterceptors.clear();
    for (GlobalChannelInterceptorWrapper globalChannelInterceptorWrapper : this.negativeOrderInterceptors) {
        String[] patterns = globalChannelInterceptorWrapper.getPatterns();
        patterns = StringUtils.trimArrayElements(patterns);
        if (beanName != null && Boolean.TRUE.equals(PatternMatchUtils.smartMatch(beanName, patterns))) {
            tempInterceptors.add(globalChannelInterceptorWrapper);
        }
    }
    Collections.sort(tempInterceptors, this.comparator);
    if (!tempInterceptors.isEmpty()) {
        for (int i = tempInterceptors.size() - 1; i >= 0; i--) {
            ChannelInterceptor channelInterceptor = tempInterceptors.get(i).getChannelInterceptor();
            if (!(channelInterceptor instanceof VetoCapableInterceptor) || ((VetoCapableInterceptor) channelInterceptor).shouldIntercept(beanName, channel)) {
                channel.addInterceptor(0, channelInterceptor);
            }
        }
    }
}
Also used : VetoCapableInterceptor(org.springframework.integration.channel.interceptor.VetoCapableInterceptor) ChannelInterceptor(org.springframework.messaging.support.ChannelInterceptor) ArrayList(java.util.ArrayList) GlobalChannelInterceptorWrapper(org.springframework.integration.channel.interceptor.GlobalChannelInterceptorWrapper)

Example 23 with ChannelInterceptor

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

the class MessageChannelsMonitorIntegrationTests method doTest.

private void doTest(String config, String channelName) throws Exception {
    ClassPathXmlApplicationContext context = createContext(config, channelName);
    try {
        int before = service.getCounter();
        CountDownLatch latch = new CountDownLatch(1);
        service.setLatch(latch);
        channel.send(new GenericMessage<String>("bar"));
        assertTrue(latch.await(10, TimeUnit.SECONDS));
        assertEquals(before + 1, service.getCounter());
        // The handler monitor is registered under the endpoint id (since it is explicit)
        int sends = messageChannelsMonitor.getChannelSendRate("" + channel).getCount();
        assertEquals("No statistics for input channel", 1, sends, 0.01);
        assertThat(channel, Matchers.instanceOf(ChannelInterceptorAware.class));
        List<ChannelInterceptor> channelInterceptors = ((ChannelInterceptorAware) channel).getChannelInterceptors();
        assertEquals(1, channelInterceptors.size());
        assertThat(channelInterceptors.get(0), Matchers.instanceOf(WireTap.class));
    } finally {
        context.close();
    }
}
Also used : ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) ChannelInterceptor(org.springframework.messaging.support.ChannelInterceptor) CountDownLatch(java.util.concurrent.CountDownLatch) WireTap(org.springframework.integration.channel.interceptor.WireTap) ChannelInterceptorAware(org.springframework.integration.channel.ChannelInterceptorAware)

Example 24 with ChannelInterceptor

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

the class GlobalChannelInterceptorTests method testJmsChannel.

@Test
public void testJmsChannel() {
    ActiveMqTestUtils.prepare();
    ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("GlobalChannelInterceptorTests-context.xml", GlobalChannelInterceptorTests.class);
    ChannelInterceptorAware jmsChannel = context.getBean("jmsChannel", AbstractMessageChannel.class);
    List<ChannelInterceptor> interceptors = jmsChannel.getChannelInterceptors();
    assertNotNull(interceptors);
    assertEquals(1, interceptors.size());
    assertTrue(interceptors.get(0) instanceof SampleInterceptor);
    context.close();
}
Also used : ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) ChannelInterceptor(org.springframework.messaging.support.ChannelInterceptor) ChannelInterceptorAware(org.springframework.integration.channel.ChannelInterceptorAware) Test(org.junit.Test)

Example 25 with ChannelInterceptor

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

the class JmsChannelParserTests method queueChannelWithInterceptors.

@Test
@SuppressWarnings("unchecked")
public void queueChannelWithInterceptors() {
    assertEquals(SubscribableJmsChannel.class, queueChannelWithInterceptors.getClass());
    SubscribableJmsChannel channel = (SubscribableJmsChannel) queueChannelWithInterceptors;
    DirectFieldAccessor accessor = new DirectFieldAccessor(channel);
    List<ChannelInterceptor> interceptors = (List<ChannelInterceptor>) new DirectFieldAccessor(accessor.getPropertyValue("interceptors")).getPropertyValue("interceptors");
    assertEquals(1, interceptors.size());
    assertEquals(TestInterceptor.class, interceptors.get(0).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)

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