Spring Integration’s Java DSL is a fluent Java API for defining an integration flow: a pipeline that moves messages through processing components. Instead of declaring a separate bean for each component, you can define the entire flow in one IntegrationFlow bean. section 10.1.3 section 10.0
Write messages to a file
A file-writing flow starts at textInChannel, transforms each string payload to uppercase, and passes it to a file outbound adapter. IntegrationFlows starts the builder, and get() builds the IntegrationFlow. The adapter can append to an existing file and add a newline. section 10.1.3
@Bean
public IntegrationFlow fileWriterFlow() {
return IntegrationFlows
.from(MessageChannels.direct("textInChannel"))
.<String, String>transform(t -> t.toUpperCase())
.channel(MessageChannels.direct("FileWriterChannel"))
.handle(Files
.outboundAdapter(new File("/tmp/sia6/files"))
.fileExistsMode(FileExistsMode.APPEND)
.appendNewLine(true))
.get();
}
Follow the flow from the input channel through the transformer to the file adapter. The channel() call explicitly configures the channel between the transformer and adapter. section 10.1.3
You do not always need to declare channel beans yourself. A referenced input channel can be created automatically when no bean with that name exists, and the channel between flow components need not be named. Use channel() when you need explicit control over a connection. section 10.1.3
A flow can also branch. Call route() with a function that selects a mapping, then define a subflow for each result. For example, an even-or-odd router can send even numbers to a subflow that multiplies them by ten and odd numbers to one that converts them to Roman numerals. section 10.2.4
@Bean
public IntegrationFlow numberRoutingFlow(AtomicInteger source) {
return IntegrationFlows
...
.<Integer, String>route(n -> n%2==0 ? "EVEN":"ODD", mapping -> mapping
.subFlowMapping("EVEN", sf -> sf
.<Integer, Integer>transform(n -> n * 10)
.handle((i,h) -> { ... })
)
.subFlowMapping("ODD", sf -> sf
.transform(RomanNumbers::toRoman)
.handle((i,h) -> { ... })
)
)
.get();
}
See how route() connects the lambda’s EVEN and ODD results to separate subflows. section 10.2.4
Other steps include split() for breaking a message into parts and handle() for invoking a service. A MessageHandler receives the message; a GenericHandler receives its payload and headers. At the end of a flow, a GenericHandler should return null to avoid an error about a missing output channel. section 10.2.5 section 10.2.6
Turn email into orders
An email integration flow uses an IMAP inbound adapter to poll for messages, a transformer to turn each email into an order, and a handler to submit the order to a REST API. The mail adapter requires the Spring Integration mail dependency. section 10.3
Indent related calls to keep fluent flows readable. For longer flows, extract portions into methods or subflows; method references can also make nested handling easier to follow. section 10.1.3 section 10.2.5
