Search in sources :

Example 1 with MethodInvokingSelector

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

the class RecipientListRouterSpec method recipient.

/**
 * Adds a recipient channel that will be selected if the the selector's accept method returns 'true'.
 * @param channel the recipient channel.
 * @param selector the selector.
 * @param <P> the selector source type.
 * @return the router spec.
 */
public <P> RecipientListRouterSpec recipient(MessageChannel channel, GenericSelector<P> selector) {
    MessageSelector messageSelector;
    if (selector instanceof MessageSelector) {
        messageSelector = (MessageSelector) selector;
    } else {
        messageSelector = isLambda(selector) ? new MethodInvokingSelector(new LambdaMessageProcessor(selector, null)) : new MethodInvokingSelector(selector);
    }
    this.handler.addRecipient(channel, messageSelector);
    return _this();
}
Also used : LambdaMessageProcessor(org.springframework.integration.handler.LambdaMessageProcessor) MessageSelector(org.springframework.integration.core.MessageSelector) MethodInvokingSelector(org.springframework.integration.filter.MethodInvokingSelector)

Example 2 with MethodInvokingSelector

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

the class RecipientListRouterSpec method recipient.

/**
 * Adds a recipient channel that will be selected if the the selector's accept method returns 'true'.
 * @param channelName the channel name.
 * @param selector the selector.
 * @param <P> the selector source type.
 * @return the router spec.
 */
public <P> RecipientListRouterSpec recipient(String channelName, GenericSelector<P> selector) {
    MessageSelector messageSelector;
    if (selector instanceof MessageSelector) {
        messageSelector = (MessageSelector) selector;
    } else {
        messageSelector = isLambda(selector) ? new MethodInvokingSelector(new LambdaMessageProcessor(selector, null)) : new MethodInvokingSelector(selector);
    }
    this.handler.addRecipient(channelName, messageSelector);
    return _this();
}
Also used : LambdaMessageProcessor(org.springframework.integration.handler.LambdaMessageProcessor) MessageSelector(org.springframework.integration.core.MessageSelector) MethodInvokingSelector(org.springframework.integration.filter.MethodInvokingSelector)

Example 3 with MethodInvokingSelector

use of org.springframework.integration.filter.MethodInvokingSelector 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 4 with MethodInvokingSelector

use of org.springframework.integration.filter.MethodInvokingSelector 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 5 with MethodInvokingSelector

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

the class IntegrationFlowDefinition method filter.

/**
 * Populate a {@link MessageFilter} with {@link MethodInvokingSelector}
 * for the {@link org.springframework.integration.handler.MessageProcessor} from
 * the provided {@link MessageProcessorSpec}.
 * In addition accept options for the integration endpoint using {@link FilterEndpointSpec}.
 * <pre class="code">
 * {@code
 *  .filter(Scripts.script(scriptResource).lang("ruby"),
 *        e -> e.autoStartup(false))
 * }
 * </pre>
 * @param messageProcessorSpec the {@link MessageProcessorSpec} to use.
 * @param endpointConfigurer the {@link Consumer} to provide integration endpoint options.
 * @return the current {@link IntegrationFlowDefinition}.
 */
public B filter(MessageProcessorSpec<?> messageProcessorSpec, Consumer<FilterEndpointSpec> endpointConfigurer) {
    Assert.notNull(messageProcessorSpec, "'messageProcessorSpec' must not be null");
    MessageProcessor<?> processor = messageProcessorSpec.get();
    return addComponent(processor).filter(new MethodInvokingSelector(processor), endpointConfigurer);
}
Also used : MethodInvokingSelector(org.springframework.integration.filter.MethodInvokingSelector)

Aggregations

MethodInvokingSelector (org.springframework.integration.filter.MethodInvokingSelector)5 MessageSelector (org.springframework.integration.core.MessageSelector)4 LambdaMessageProcessor (org.springframework.integration.handler.LambdaMessageProcessor)3 MessageFilter (org.springframework.integration.filter.MessageFilter)2 Bean (org.springframework.context.annotation.Bean)1