Search in sources :

Example 1 with ChannelInterceptor

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

the class PollingConsumer method applyBeforeHandle.

private Message<?> applyBeforeHandle(Message<?> message, Deque<ExecutorChannelInterceptor> interceptorStack) {
    Message<?> theMessage = message;
    for (ChannelInterceptor interceptor : this.channelInterceptors) {
        if (interceptor instanceof ExecutorChannelInterceptor) {
            ExecutorChannelInterceptor executorInterceptor = (ExecutorChannelInterceptor) interceptor;
            theMessage = executorInterceptor.beforeHandle(theMessage, this.inputChannel, this.handler);
            if (message == null) {
                if (logger.isDebugEnabled()) {
                    logger.debug(executorInterceptor.getClass().getSimpleName() + " returned null from beforeHandle, i.e. precluding the send.");
                }
                triggerAfterMessageHandled(null, null, interceptorStack);
                return null;
            }
            interceptorStack.add(executorInterceptor);
        }
    }
    return theMessage;
}
Also used : ExecutorChannelInterceptor(org.springframework.messaging.support.ExecutorChannelInterceptor) ExecutorChannelInterceptor(org.springframework.messaging.support.ExecutorChannelInterceptor) ChannelInterceptor(org.springframework.messaging.support.ChannelInterceptor)

Example 2 with ChannelInterceptor

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

the class EnableIntegrationTests method testParentChildAnnotationConfigurationFromAnotherPackage.

@Test
public void testParentChildAnnotationConfigurationFromAnotherPackage() {
    AnnotationConfigApplicationContext child = new AnnotationConfigApplicationContext();
    child.register(org.springframework.integration.configuration2.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 3 with ChannelInterceptor

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

the class AbstractMessageChannel method send.

/**
 * Send a message on this channel. If the channel is at capacity, this
 * method will block until either the timeout occurs or the sending thread
 * is interrupted. If the specified timeout is 0, the method will return
 * immediately. If less than zero, it will block indefinitely (see
 * {@link #send(Message)}).
 * @param message the Message to send
 * @param timeout the timeout in milliseconds
 * @return <code>true</code> if the message is sent successfully,
 * <code>false</code> if the message cannot be sent within the allotted
 * time or the sending thread is interrupted.
 */
@Override
public boolean send(Message<?> message, long timeout) {
    Assert.notNull(message, "message must not be null");
    Assert.notNull(message.getPayload(), "message payload must not be null");
    if (this.shouldTrack) {
        message = MessageHistory.write(message, this, this.getMessageBuilderFactory());
    }
    Deque<ChannelInterceptor> interceptorStack = null;
    boolean sent = false;
    boolean metricsProcessed = false;
    MetricsContext metrics = null;
    boolean countsEnabled = this.countsEnabled;
    ChannelInterceptorList interceptors = this.interceptors;
    AbstractMessageChannelMetrics channelMetrics = this.channelMetrics;
    SampleFacade sample = null;
    try {
        if (this.datatypes.length > 0) {
            message = this.convertPayloadIfNecessary(message);
        }
        boolean debugEnabled = this.loggingEnabled && logger.isDebugEnabled();
        if (debugEnabled) {
            logger.debug("preSend on channel '" + this + "', message: " + message);
        }
        if (interceptors.getSize() > 0) {
            interceptorStack = new ArrayDeque<>();
            message = interceptors.preSend(message, this, interceptorStack);
            if (message == null) {
                return false;
            }
        }
        if (countsEnabled) {
            metrics = channelMetrics.beforeSend();
            if (this.metricsCaptor != null) {
                sample = this.metricsCaptor.start();
            }
            sent = doSend(message, timeout);
            if (sample != null) {
                sample.stop(sendTimer(sent));
            }
            channelMetrics.afterSend(metrics, sent);
            metricsProcessed = true;
        } else {
            sent = doSend(message, timeout);
        }
        if (debugEnabled) {
            logger.debug("postSend (sent=" + sent + ") on channel '" + this + "', message: " + message);
        }
        if (interceptorStack != null) {
            interceptors.postSend(message, this, sent);
            interceptors.afterSendCompletion(message, this, sent, null, interceptorStack);
        }
        return sent;
    } catch (Exception e) {
        if (countsEnabled && !metricsProcessed) {
            if (sample != null) {
                sample.stop(buildSendTimer(false, e.getClass().getSimpleName()));
            }
            channelMetrics.afterSend(metrics, false);
        }
        if (interceptorStack != null) {
            interceptors.afterSendCompletion(message, this, sent, e, interceptorStack);
        }
        throw IntegrationUtils.wrapInDeliveryExceptionIfNecessary(message, () -> "failed to send Message to channel '" + this.getComponentName() + "'", e);
    }
}
Also used : SampleFacade(org.springframework.integration.support.management.metrics.SampleFacade) ChannelInterceptor(org.springframework.messaging.support.ChannelInterceptor) MetricsContext(org.springframework.integration.support.management.MetricsContext) AbstractMessageChannelMetrics(org.springframework.integration.support.management.AbstractMessageChannelMetrics) MessageDeliveryException(org.springframework.messaging.MessageDeliveryException)

Example 4 with ChannelInterceptor

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

the class GlobalChannelInterceptorTests method validateGlobalInterceptor.

@Test
public void validateGlobalInterceptor() throws Exception {
    Map<String, ChannelInterceptorAware> channels = applicationContext.getBeansOfType(ChannelInterceptorAware.class);
    for (String channelName : channels.keySet()) {
        ChannelInterceptorAware channel = channels.get(channelName);
        if (channelName.equals("nullChannel")) {
            continue;
        }
        ChannelInterceptor[] interceptors = channel.getChannelInterceptors().toArray(new ChannelInterceptor[channel.getChannelInterceptors().size()]);
        if (channelName.equals("inputA")) {
            // 328741
            assertTrue(interceptors.length == 10);
            assertEquals("interceptor-three", interceptors[0].toString());
            assertEquals("interceptor-two", interceptors[1].toString());
            assertEquals("interceptor-eight", interceptors[2].toString());
            assertEquals("interceptor-seven", interceptors[3].toString());
            assertEquals("interceptor-five", interceptors[4].toString());
            assertEquals("interceptor-six", interceptors[5].toString());
            assertEquals("interceptor-ten", interceptors[6].toString());
            assertEquals("interceptor-eleven", interceptors[7].toString());
            assertEquals("interceptor-four", interceptors[8].toString());
            assertEquals("interceptor-one", interceptors[9].toString());
        } else if (channelName.equals("inputB")) {
            assertTrue(interceptors.length == 6);
            assertEquals("interceptor-three", interceptors[0].toString());
            assertEquals("interceptor-two", interceptors[1].toString());
            assertEquals("interceptor-ten", interceptors[2].toString());
            assertEquals("interceptor-eleven", interceptors[3].toString());
            assertEquals("interceptor-four", interceptors[4].toString());
            assertEquals("interceptor-one", interceptors[5].toString());
        } else if (channelName.equals("foo")) {
            assertTrue(interceptors.length == 6);
            assertEquals("interceptor-two", interceptors[0].toString());
            assertEquals("interceptor-five", interceptors[1].toString());
            assertEquals("interceptor-ten", interceptors[2].toString());
            assertEquals("interceptor-eleven", interceptors[3].toString());
            assertEquals("interceptor-four", interceptors[4].toString());
            assertEquals("interceptor-one", interceptors[5].toString());
        } else if (channelName.equals("bar")) {
            assertTrue(interceptors.length == 4);
            assertEquals("interceptor-eight", interceptors[0].toString());
            assertEquals("interceptor-seven", interceptors[1].toString());
            assertEquals("interceptor-ten", interceptors[2].toString());
            assertEquals("interceptor-eleven", interceptors[3].toString());
        } else if (channelName.equals("baz")) {
            assertTrue(interceptors.length == 2);
            assertEquals("interceptor-ten", interceptors[0].toString());
            assertEquals("interceptor-eleven", interceptors[1].toString());
        } else if (channelName.equals("inputWithProxy")) {
            assertTrue(interceptors.length == 6);
        } else if (channelName.equals("test")) {
            assertNotNull(interceptors);
            assertTrue(interceptors.length == 2);
            List<String> interceptorNames = new ArrayList<String>();
            for (ChannelInterceptor interceptor : interceptors) {
                interceptorNames.add(interceptor.toString());
            }
            assertTrue(interceptorNames.contains("interceptor-ten"));
            assertTrue(interceptorNames.contains("interceptor-eleven"));
        }
    }
}
Also used : ChannelInterceptor(org.springframework.messaging.support.ChannelInterceptor) ArrayList(java.util.ArrayList) ChannelInterceptorAware(org.springframework.integration.channel.ChannelInterceptorAware) Test(org.junit.Test)

Example 5 with ChannelInterceptor

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

the class ChannelInterceptorTests method testInterceptorBeanWithPNamespace.

@Test
public void testInterceptorBeanWithPNamespace() {
    ConfigurableApplicationContext ac = new ClassPathXmlApplicationContext("ChannelInterceptorTests-context.xml", ChannelInterceptorTests.class);
    ChannelInterceptorAware channel = ac.getBean("input", AbstractMessageChannel.class);
    List<ChannelInterceptor> interceptors = channel.getChannelInterceptors();
    ChannelInterceptor channelInterceptor = interceptors.get(0);
    assertThat(channelInterceptor, Matchers.instanceOf(PreSendReturnsMessageInterceptor.class));
    String foo = ((PreSendReturnsMessageInterceptor) channelInterceptor).getFoo();
    assertTrue(StringUtils.hasText(foo));
    assertEquals("foo", foo);
    ac.close();
}
Also used : ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) ChannelInterceptor(org.springframework.messaging.support.ChannelInterceptor) ExecutorChannelInterceptor(org.springframework.messaging.support.ExecutorChannelInterceptor) ChannelInterceptorAware(org.springframework.integration.channel.ChannelInterceptorAware) 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