Search in sources :

Example 1 with MessageFilter

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

the class AdvisedMessageHandlerTests method filterDiscardNoAdvice.

public void filterDiscardNoAdvice() {
    MessageFilter filter = new MessageFilter(message -> false);
    QueueChannel discardChannel = new QueueChannel();
    filter.setDiscardChannel(discardChannel);
    filter.handleMessage(new GenericMessage<String>("foo"));
    assertNotNull(discardChannel.receive(0));
}
Also used : QueueChannel(org.springframework.integration.channel.QueueChannel) Matchers.containsString(org.hamcrest.Matchers.containsString) MessageFilter(org.springframework.integration.filter.MessageFilter)

Example 2 with MessageFilter

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

the class AdvisedMessageHandlerTests method filterDiscardWithinAdvice.

@Test
public void filterDiscardWithinAdvice() {
    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<?>>();
    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));
            return result;
        }
    });
    filter.setAdviceChain(adviceChain);
    filter.setBeanFactory(mock(BeanFactory.class));
    filter.afterPropertiesSet();
    filter.handleMessage(new GenericMessage<String>("foo"));
    assertNotNull(discardedWithinAdvice.get());
    assertNull(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) BeanFactory(org.springframework.beans.factory.BeanFactory) Advice(org.aopalliance.aop.Advice) MessageFilter(org.springframework.integration.filter.MessageFilter) Test(org.junit.Test)

Example 3 with MessageFilter

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

the class GroovyFilterTests method testInt2433VerifyRiddingOfMessageProcessorsWrapping.

@Test
public void testInt2433VerifyRiddingOfMessageProcessorsWrapping() {
    assertTrue(this.groovyFilterMessageHandler instanceof MessageFilter);
    MessageSelector selector = TestUtils.getPropertyValue(this.groovyFilterMessageHandler, "selector", MethodInvokingSelector.class);
    @SuppressWarnings("rawtypes") MessageProcessor messageProcessor = TestUtils.getPropertyValue(selector, "messageProcessor", MessageProcessor.class);
    // before it was MethodInvokingMessageProcessor
    assertTrue(messageProcessor instanceof GroovyScriptExecutingMessageProcessor);
}
Also used : MessageProcessor(org.springframework.integration.handler.MessageProcessor) GroovyScriptExecutingMessageProcessor(org.springframework.integration.groovy.GroovyScriptExecutingMessageProcessor) GroovyScriptExecutingMessageProcessor(org.springframework.integration.groovy.GroovyScriptExecutingMessageProcessor) MessageSelector(org.springframework.integration.core.MessageSelector) MessageFilter(org.springframework.integration.filter.MessageFilter) Test(org.junit.Test)

Example 4 with MessageFilter

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

the class FilterAnnotationPostProcessor method createHandler.

@Override
protected MessageHandler createHandler(Object bean, Method method, List<Annotation> annotations) {
    MessageSelector selector;
    if (AnnotatedElementUtils.isAnnotated(method, Bean.class.getName())) {
        Object target = this.resolveTargetBeanFromMethodWithBeanAnnotation(method);
        if (target instanceof MessageSelector) {
            selector = (MessageSelector) target;
        } else if (this.extractTypeIfPossible(target, MessageFilter.class) != null) {
            checkMessageHandlerAttributes(resolveTargetBeanName(method), annotations);
            return (MessageHandler) target;
        } else {
            selector = new MethodInvokingSelector(target);
        }
    } else {
        Assert.isTrue(boolean.class.equals(method.getReturnType()) || Boolean.class.equals(method.getReturnType()), "The Filter annotation may only be applied to methods with a boolean return type.");
        selector = new MethodInvokingSelector(bean, method);
    }
    MessageFilter filter = new MessageFilter(selector);
    String discardWithinAdvice = MessagingAnnotationUtils.resolveAttribute(annotations, "discardWithinAdvice", String.class);
    if (StringUtils.hasText(discardWithinAdvice)) {
        discardWithinAdvice = this.beanFactory.resolveEmbeddedValue(discardWithinAdvice);
        if (StringUtils.hasText(discardWithinAdvice)) {
            filter.setDiscardWithinAdvice(Boolean.parseBoolean(discardWithinAdvice));
        }
    }
    String throwExceptionOnRejection = MessagingAnnotationUtils.resolveAttribute(annotations, "throwExceptionOnRejection", String.class);
    if (StringUtils.hasText(throwExceptionOnRejection)) {
        String throwExceptionOnRejectionValue = this.beanFactory.resolveEmbeddedValue(throwExceptionOnRejection);
        if (StringUtils.hasText(throwExceptionOnRejectionValue)) {
            filter.setThrowExceptionOnRejection(Boolean.parseBoolean(throwExceptionOnRejectionValue));
        }
    }
    String discardChannelName = MessagingAnnotationUtils.resolveAttribute(annotations, "discardChannel", String.class);
    if (StringUtils.hasText(discardChannelName)) {
        filter.setDiscardChannelName(discardChannelName);
    }
    this.setOutputChannelIfPresent(annotations, filter);
    return filter;
}
Also used : MessageSelector(org.springframework.integration.core.MessageSelector) MessageFilter(org.springframework.integration.filter.MessageFilter) Bean(org.springframework.context.annotation.Bean) MethodInvokingSelector(org.springframework.integration.filter.MethodInvokingSelector)

Example 5 with MessageFilter

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

the class FilterFactoryBean method createFilter.

protected MessageFilter createFilter(MessageSelector selector) {
    MessageFilter filter = new MessageFilter(selector);
    postProcessReplyProducer(filter);
    return filter;
}
Also used : MessageFilter(org.springframework.integration.filter.MessageFilter)

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