gnzlrm
gnzlrm

Reputation: 302

Mid-flow file sourcing with Spring Integration DSL

I'm in the need to create a flow that, whenever a file is received at a folder, it picks up some other pre-existing files from a different folder, transforms all of them (including the newly received) using the same transformer and then aggregates them to create a single output. I imagine I could use a splitter-aggregator approach to do the transformation and aggregation steps; but I don't know what SI component type (if any) I could use to merge the new file (the one that's received from the original MessageSource that kicks-off the flow execution) with the existing ones in a same flow.

The project I'm working on uses spring-integration-core-5.0.11.RELEASE and spring-integration-file-5.0.11.RELEASE and we're creating the flows using Spring Integration DSL.

Upvotes: 2

Views: 187

Answers (1)

Artem Bilan
Artem Bilan

Reputation: 121542

Please, consider to use a. .enrich() EIP-method:

/**
 * Populate a {@link org.springframework.integration.transformer.ContentEnricher}
 * to the current integration flow position
 * with provided options.
 * Typically used with a Java 8 Lambda expression:
 * <pre class="code">
 * {@code
 *  .enrich(e -> e.requestChannel("enrichChannel")
 *                  .requestPayload(Message::getPayload)
 *                  .shouldClonePayload(false)
 *                  .autoStartup(false)
 *                  .<Map<String, String>>headerFunction("foo", m -> m.getPayload().get("name")))
 * }
 * </pre>
 * @param enricherConfigurer the {@link Consumer} to provide
 * {@link org.springframework.integration.transformer.ContentEnricher} options.
 * @return the current {@link IntegrationFlowDefinition}.
 * @see EnricherSpec
 */
public B enrich(Consumer<EnricherSpec> enricherConfigurer) {

This way you can call whatever you need based on the request messages and store everything in the output payload.

Please, see more info about Enricher in the Reference Manual.

Also you can take a look into the sample. I know that one is on XML config, but the principle is the same.

UPDATE

For that trick you need to wrap your request File into Collection (List, of course) and all the other file you'd just add to this collection with a simple addAll() expression.

Another, much better solution would be with the .gateway(). So, you send your file over there and simply return a Collection together with others. This way your flow should be smooth enough to just deal with one file upstream and collection downstream.

Upvotes: 2

Related Questions