Search in sources :

Example 6 with MessageFilter

use of org.springframework.integration.filter.MessageFilter in project spring-integration by spring-projects.

the class IntegrationFlowDefinition method filter.

/**
 * Populate a {@link MessageFilter} with {@link MethodInvokingSelector}
 * for the provided {@link GenericSelector}.
 * In addition accept options for the integration endpoint using {@link FilterEndpointSpec}.
 * Typically used with a Java 8 Lambda expression:
 * <pre class="code">
 * {@code
 *  .filter(Date.class, p -> p.after(new Date()), e -> e.autoStartup(false))
 * }
 * </pre>
 * @param payloadType the {@link Class} for desired {@code payload} type.
 * @param genericSelector the {@link GenericSelector} to use.
 * @param endpointConfigurer the {@link Consumer} to provide integration endpoint options.
 * @param <P> the source payload type.
 * @return the current {@link IntegrationFlowDefinition}.
 * @see LambdaMessageProcessor
 * @see FilterEndpointSpec
 */
public <P> B filter(Class<P> payloadType, GenericSelector<P> genericSelector, Consumer<FilterEndpointSpec> endpointConfigurer) {
    Assert.notNull(genericSelector, "'genericSelector' must not be null");
    MessageSelector selector = genericSelector instanceof MessageSelector ? (MessageSelector) genericSelector : (isLambda(genericSelector) ? new MethodInvokingSelector(new LambdaMessageProcessor(genericSelector, payloadType)) : new MethodInvokingSelector(genericSelector));
    return this.register(new FilterEndpointSpec(new MessageFilter(selector)), endpointConfigurer);
}
Also used : LambdaMessageProcessor(org.springframework.integration.handler.LambdaMessageProcessor) MessageSelector(org.springframework.integration.core.MessageSelector) MessageFilter(org.springframework.integration.filter.MessageFilter) MethodInvokingSelector(org.springframework.integration.filter.MethodInvokingSelector)

Example 7 with MessageFilter

use of org.springframework.integration.filter.MessageFilter in project spring-integration by spring-projects.

the class AdvisedMessageHandlerTests method filterDiscardOutsideAdvice.

@Test
public void filterDiscardOutsideAdvice() {
    MessageFilter filter = new MessageFilter(message -> false);
    final QueueChannel discardChannel = new QueueChannel();
    filter.setDiscardChannel(discardChannel);
    List<Advice> adviceChain = new ArrayList<Advice>();
    final AtomicReference<Message<?>> discardedWithinAdvice = new AtomicReference<Message<?>>();
    final AtomicBoolean adviceCalled = new AtomicBoolean();
    adviceChain.add(new AbstractRequestHandlerAdvice() {

        @Override
        protected Object doInvoke(ExecutionCallback callback, Object target, Message<?> message) throws Exception {
            Object result = callback.execute();
            discardedWithinAdvice.set(discardChannel.receive(0));
            adviceCalled.set(true);
            return result;
        }
    });
    filter.setAdviceChain(adviceChain);
    filter.setDiscardWithinAdvice(false);
    filter.setBeanFactory(mock(BeanFactory.class));
    filter.afterPropertiesSet();
    filter.handleMessage(new GenericMessage<String>("foo"));
    assertTrue(adviceCalled.get());
    assertNull(discardedWithinAdvice.get());
    assertNotNull(discardChannel.receive(0));
}
Also used : QueueChannel(org.springframework.integration.channel.QueueChannel) ErrorMessage(org.springframework.messaging.support.ErrorMessage) AdviceMessage(org.springframework.integration.message.AdviceMessage) Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) ArrayList(java.util.ArrayList) AtomicReference(java.util.concurrent.atomic.AtomicReference) Matchers.containsString(org.hamcrest.Matchers.containsString) MessageHandlingException(org.springframework.messaging.MessageHandlingException) MessagingException(org.springframework.messaging.MessagingException) MessageHandlingExpressionEvaluatingAdviceException(org.springframework.integration.handler.advice.ExpressionEvaluatingRequestHandlerAdvice.MessageHandlingExpressionEvaluatingAdviceException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) BeanFactory(org.springframework.beans.factory.BeanFactory) Advice(org.aopalliance.aop.Advice) MessageFilter(org.springframework.integration.filter.MessageFilter) Test(org.junit.Test)

Aggregations

MessageFilter (org.springframework.integration.filter.MessageFilter)7 Matchers.containsString (org.hamcrest.Matchers.containsString)3 Test (org.junit.Test)3 QueueChannel (org.springframework.integration.channel.QueueChannel)3 MessageSelector (org.springframework.integration.core.MessageSelector)3 ArrayList (java.util.ArrayList)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 Advice (org.aopalliance.aop.Advice)2 BeanFactory (org.springframework.beans.factory.BeanFactory)2 MethodInvokingSelector (org.springframework.integration.filter.MethodInvokingSelector)2 MessageHandlingExpressionEvaluatingAdviceException (org.springframework.integration.handler.advice.ExpressionEvaluatingRequestHandlerAdvice.MessageHandlingExpressionEvaluatingAdviceException)2 AdviceMessage (org.springframework.integration.message.AdviceMessage)2 Message (org.springframework.messaging.Message)2 MessageHandlingException (org.springframework.messaging.MessageHandlingException)2 MessagingException (org.springframework.messaging.MessagingException)2 ErrorMessage (org.springframework.messaging.support.ErrorMessage)2 GenericMessage (org.springframework.messaging.support.GenericMessage)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 Bean (org.springframework.context.annotation.Bean)1 GroovyScriptExecutingMessageProcessor (org.springframework.integration.groovy.GroovyScriptExecutingMessageProcessor)1