@Bean public IntegrationFlow scriptFilter() { return f -> f.filter(Scripts.processor(this.filterScript), e -> e.discardChannel("discardChannel")) .channel(results()); }
/** * Set to 'true' if you wish the discard processing to occur within any * request handler advice applied to this filter. Also applies to * throwing an exception on rejection. Default: true. * @param discardWithinAdvice the discardWithinAdvice. * @return the endpoint spec. * @see MessageFilter#setDiscardWithinAdvice(boolean) */ public FilterEndpointSpec discardWithinAdvice(boolean discardWithinAdvice) { this.handler.setDiscardWithinAdvice(discardWithinAdvice); return _this(); }
@Bean public IntegrationFlow gatewayRequestFlow() { return IntegrationFlows.from("gatewayRequest") .filter("foo"::equals, f -> f.throwExceptionOnRejection(true)) .<String, String>transform(String::toUpperCase) .get(); }
/** * Configure a subflow to run for discarded messages instead of a * {@link #discardChannel(MessageChannel)}. * @param discardFlow the discard flow. * @return the endpoint spec. */ public FilterEndpointSpec discardFlow(IntegrationFlow discardFlow) { return discardChannel(obtainInputChannelFromFlow(discardFlow)); }
@Bean public IntegrationFlow flow2() { return IntegrationFlows.from(this.inputChannel) .filter(p -> p instanceof String, e -> e .id("filter") .discardFlow(df -> df .transform(String.class, "Discarded: "::concat) .channel(MessageChannels.queue("discardChannel")))) .channel("foo") .fixedSubscriberChannel() .<String, Integer>transform(Integer::parseInt) .transform(Foo::new) .transform(new PayloadSerializingTransformer(), c -> c.autoStartup(false).id("payloadSerializingTransformer")) .channel(MessageChannels.queue(new SimpleMessageStore(), "fooQueue")) .transform(Transformers.deserializer(Foo.class.getName())) .<Foo, Integer>transform(f -> f.value) .filter("true", e -> e.id("expressionFilter")) .channel(publishSubscribeChannel()) .transform((Integer p) -> p * 2, c -> c.advice(this.expressionAdvice())) .get(); }
/** * 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 expected payload type. It can also be * {@code Message.class} if you wish to access the entire message in the selector. * Conversion to this type will be attempted, if necessary. * @param genericSelector the {@link GenericSelector} to use. * @param endpointConfigurer the {@link Consumer} to provide integration endpoint options. * @param <P> the source payload type or {@code Message.class}. * @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, ClassUtils.SELECTOR_ACCEPT_METHOD)); return this.register(new FilterEndpointSpec(new MessageFilter(selector)), endpointConfigurer); }
@Override public void accept( GenericEndpointSpec<MessageTransformingHandler> e) { e.id(flowName + ".transformer"); } })
/** * Configure a subflow to run for discarded messages instead of a * {@link #discardChannel(MessageChannel)}. * @param discardFlow the discard flow. * @return the endpoint spec. */ public FilterEndpointSpec discardFlow(IntegrationFlow discardFlow) { return discardChannel(obtainInputChannelFromFlow(discardFlow)); }
@Bean public IntegrationFlow flow2() { return IntegrationFlows.from(this.inputChannel) .filter(p -> p instanceof String, e -> e .id("filter") .discardFlow(df -> df .transform(String.class, "Discarded: "::concat) .channel(c -> c.queue("discardChannel")))) .channel("foo") .fixedSubscriberChannel() .<String, Integer>transform(Integer::parseInt) .transform(new PayloadSerializingTransformer(), c -> c.autoStartup(false).id("payloadSerializingTransformer")) .channel(MessageChannels.queue(new SimpleMessageStore(), "fooQueue")) .transform(new PayloadDeserializingTransformer()) .filter("true", e -> e.id("expressionFilter")) .channel(publishSubscribeChannel()) .transform((Integer p) -> p * 2, c -> c.advice(this.expressionAdvice())) .get(); }
/** * 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 expected payload type. It can also be * {@code Message.class} if you wish to access the entire message in the selector. * Conversion to this type will be attempted, if necessary. * @param genericSelector the {@link GenericSelector} to use. * @param endpointConfigurer the {@link Consumer} to provide integration endpoint options. * @param <P> the source payload type or {@code Message.class}. * @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, ClassUtils.SELECTOR_ACCEPT_METHOD)); return this.register(new FilterEndpointSpec(new MessageFilter(selector)), endpointConfigurer); }
@Override public void accept( GenericEndpointSpec<MessageTransformingHandler> e) { e.id(flowName + ".transformer"); } })
@Bean public IntegrationFlow scriptFilter() { return f -> f .filter(Scripts.processor(scriptResource()) .lang("groovy"), e -> e.discardChannel("discardChannel")) .channel(c -> c.queue("results")); }
/** * Specify a channel where rejected Messages should be sent. If the discard * channel is null (the default), rejected Messages will be dropped. However, * the 'throwExceptionOnRejection' flag determines whether rejected Messages * trigger an exception. That value is evaluated regardless of the presence * of a discard channel. * @param discardChannel the discardChannel. * @return the endpoint spec. * @see MessageFilter#setDiscardChannel(MessageChannel) */ public FilterEndpointSpec discardChannel(MessageChannel discardChannel) { this.handler.setDiscardChannel(discardChannel); return _this(); }
@Bean public IntegrationFlow fileReadingFlow() { return IntegrationFlows .from(Files.inboundAdapter(tmpDir.getRoot()) .patternFilter("*.sitest") .useWatchService(true) .watchEvents(FileReadingMessageSource.WatchEventType.CREATE, FileReadingMessageSource.WatchEventType.MODIFY), e -> e.poller(Pollers.fixedDelay(100) .errorChannel("filePollingErrorChannel"))) .filter(File.class, p -> !p.getName().startsWith("a"), e -> e.throwExceptionOnRejection(true)) .transform(Files.toStringTransformer()) .aggregate(a -> a.correlationExpression("1") .releaseStrategy(g -> g.size() == 25)) .channel(MessageChannels.queue("fileReadingResultChannel")) .get(); }
/** * 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); }
/** * Configure a subflow to run for discarded messages instead of a * {@link #discardChannel(MessageChannel)}. * @param discardFlow the discard flow. * @return the endpoint spec. */ public FilterEndpointSpec discardFlow(IntegrationFlow discardFlow) { Assert.notNull(discardFlow, "'discardFlow' must not be null"); DirectChannel channel = new DirectChannel(); IntegrationFlowBuilder flowBuilder = IntegrationFlows.from(channel); discardFlow.configure(flowBuilder); this.componentToRegister.add(flowBuilder.get()); return discardChannel(channel); }
/** * The default value is <code>false</code> meaning that rejected * Messages will be quietly dropped or sent to the discard channel if * available. Typically this value would not be <code>true</code> when * a discard channel is provided, but if so, it will still apply * (in such a case, the Message will be sent to the discard channel, * and <em>then</em> the exception will be thrown). * @param throwExceptionOnRejection the throwExceptionOnRejection. * @return the endpoint spec. * @see MessageFilter#setThrowExceptionOnRejection(boolean) */ public FilterEndpointSpec throwExceptionOnRejection(boolean throwExceptionOnRejection) { this.handler.setThrowExceptionOnRejection(throwExceptionOnRejection); return _this(); }
@Bean public IntegrationFlow gatewayRequestFlow() { return IntegrationFlows.from("gatewayRequest") .filter("foo"::equals, f -> f.throwExceptionOnRejection(true)) .<String, String>transform(String::toUpperCase) .get(); }
/** * 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); }
/** * Configure a subflow to run for discarded messages instead of a * {@link #discardChannel(MessageChannel)}. * @param discardFlow the discard flow. * @return the endpoint spec. */ public FilterEndpointSpec discardFlow(IntegrationFlow discardFlow) { Assert.notNull(discardFlow, "'discardFlow' must not be null"); DirectChannel channel = new DirectChannel(); IntegrationFlowBuilder flowBuilder = IntegrationFlows.from(channel); discardFlow.configure(flowBuilder); this.componentToRegister.add(flowBuilder.get()); return discardChannel(channel); }