Search in sources :

Example 11 with MessageSelector

use of org.springframework.integration.core.MessageSelector in project spring-integration by spring-projects.

the class RecipientListRouter method removeRecipient.

@Override
@ManagedOperation
public int removeRecipient(String channelName, String selectorExpression) {
    int counter = 0;
    MessageChannel targetChannel = getChannelResolver().resolveDestination(channelName);
    for (Iterator<Recipient> it = this.recipients.iterator(); it.hasNext(); ) {
        Recipient next = it.next();
        MessageSelector selector = next.getSelector();
        MessageChannel channel = next.getChannel();
        if (selector instanceof ExpressionEvaluatingSelector && channel == targetChannel && ((ExpressionEvaluatingSelector) selector).getExpressionString().equals(selectorExpression)) {
            it.remove();
            counter++;
        }
    }
    return counter;
}
Also used : ExpressionEvaluatingSelector(org.springframework.integration.filter.ExpressionEvaluatingSelector) MessageChannel(org.springframework.messaging.MessageChannel) MessageSelector(org.springframework.integration.core.MessageSelector) ManagedOperation(org.springframework.jmx.export.annotation.ManagedOperation)

Example 12 with MessageSelector

use of org.springframework.integration.core.MessageSelector in project spring-integration by spring-projects.

the class MessageSelectingInterceptorTests method testMultipleSelectorsAccept.

@Test
public void testMultipleSelectorsAccept() {
    final AtomicInteger counter = new AtomicInteger();
    MessageSelector selector1 = new TestMessageSelector(true, counter);
    MessageSelector selector2 = new TestMessageSelector(true, counter);
    MessageSelectingInterceptor interceptor = new MessageSelectingInterceptor(selector1, selector2);
    QueueChannel channel = new QueueChannel();
    channel.addInterceptor(interceptor);
    assertTrue(channel.send(new GenericMessage<String>("test1")));
    assertEquals(2, counter.get());
}
Also used : GenericMessage(org.springframework.messaging.support.GenericMessage) QueueChannel(org.springframework.integration.channel.QueueChannel) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MessageSelector(org.springframework.integration.core.MessageSelector) Test(org.junit.Test)

Example 13 with MessageSelector

use of org.springframework.integration.core.MessageSelector in project spring-integration by spring-projects.

the class MessageSelectingInterceptorTests method testMultipleSelectorsReject.

@Test
public void testMultipleSelectorsReject() {
    boolean exceptionThrown = false;
    final AtomicInteger counter = new AtomicInteger();
    MessageSelector selector1 = new TestMessageSelector(true, counter);
    MessageSelector selector2 = new TestMessageSelector(false, counter);
    MessageSelector selector3 = new TestMessageSelector(false, counter);
    MessageSelector selector4 = new TestMessageSelector(true, counter);
    MessageSelectingInterceptor interceptor = new MessageSelectingInterceptor(selector1, selector2, selector3, selector4);
    QueueChannel channel = new QueueChannel();
    channel.addInterceptor(interceptor);
    try {
        channel.send(new GenericMessage<String>("test1"));
    } catch (MessageDeliveryException e) {
        exceptionThrown = true;
    }
    assertTrue(exceptionThrown);
    assertEquals(2, counter.get());
}
Also used : QueueChannel(org.springframework.integration.channel.QueueChannel) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MessageSelector(org.springframework.integration.core.MessageSelector) MessageDeliveryException(org.springframework.messaging.MessageDeliveryException) Test(org.junit.Test)

Example 14 with MessageSelector

use of org.springframework.integration.core.MessageSelector 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 15 with MessageSelector

use of org.springframework.integration.core.MessageSelector 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)

Aggregations

MessageSelector (org.springframework.integration.core.MessageSelector)15 Test (org.junit.Test)9 GenericMessage (org.springframework.messaging.support.GenericMessage)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 QueueChannel (org.springframework.integration.channel.QueueChannel)4 MethodInvokingSelector (org.springframework.integration.filter.MethodInvokingSelector)4 MessageFilter (org.springframework.integration.filter.MessageFilter)3 LambdaMessageProcessor (org.springframework.integration.handler.LambdaMessageProcessor)3 MessageSelectorChain (org.springframework.integration.selector.MessageSelectorChain)2 List (java.util.List)1 DirectFieldAccessor (org.springframework.beans.DirectFieldAccessor)1 Bean (org.springframework.context.annotation.Bean)1 ExpressionEvaluatingSelector (org.springframework.integration.filter.ExpressionEvaluatingSelector)1 GroovyScriptExecutingMessageProcessor (org.springframework.integration.groovy.GroovyScriptExecutingMessageProcessor)1 MessageProcessor (org.springframework.integration.handler.MessageProcessor)1 ManagedOperation (org.springframework.jmx.export.annotation.ManagedOperation)1 MessageChannel (org.springframework.messaging.MessageChannel)1 MessageDeliveryException (org.springframework.messaging.MessageDeliveryException)1