liveBook · Manning

Java DSL

As explained in Spring in Action, Sixth Edition

Java DSL is a Spring Integration domain-specific language expressed through Java APIs for declaratively defining integration flows.

Spring Integration’s Java DSL defines a message-processing pipeline with a fluent Java API. Instead of declaring a bean for every component, you can return the whole flow from one @Bean method. It is an alternative to XML configuration and annotation-based Java configuration. section 10.1.2 section 10.1.3

A file-writing flow follows four steps:

  • Start at textInChannel with IntegrationFlows.from(...). If no channel bean with that name exists, Spring Integration creates it. section 10.1.3
  • Call transform(...) to uppercase each text payload. section 10.1.3
  • Call handle(...) with a Files outbound adapter to write the result, then get() to build the IntegrationFlow. section 10.1.3
  • Add channel(...) when the connection between components needs an explicit name or configuration; otherwise, the flow need not name it. 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();
}

The complete file-writing flow shows where an explicit channel fits between the transformer and outbound adapter. section 10.1.3

The same API handles branching and service calls. route(...) selects subflows, split(...) turns one message into several, and handle(...) invokes a handler. A terminal GenericHandler should return null when there is no output channel; returning a payload instead can produce a no-output-channel error. Keep longer fluent flows readable with indentation, extracted methods, or subflows. section 10.2.4 section 10.2.5 section 10.2.6 section 10.1.3

Email orders

A separate application can poll an IMAP mailbox, transform each email into an order, and pass that order to a handler that POSTs it to a REST API. The flow combines Mail.imapInboundAdapter(...), a configured fixed-delay poller, transform(...), and handle(...); the mail adapter requires the Spring Integration mail dependency. section 10.3

How Spring in Action, Sixth Edition covers it

How deep it goes
Works through fluent flow construction, optional channels, branching, splitting, and handlers; advises extracting complex flows for readability.
Examples
Uses an uppercase-to-file flow, even-or-odd routing, purchase-order splitting, and an email-to-REST order flow.
What it assumes
Builds on Java bean configuration and introduces the fluent DSL by replacing separate component beans with one flow bean.
What you can do after
Build it from scratch as an IntegrationFlow bean that transforms messages and connects file or email adapters to handlers.
How it connects
Connects message channels, transformers, channel adapters, routers, splitters, and service activators in integration flows.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Spring in Action, Sixth Edition. The section links point to the parts of the book it draws on.