liveBook · Manning

Integration flow

As explained in Spring in Action, Sixth Edition

An integration flow is an ordered sequence of messaging components that receives, transforms, routes, or sends data between an application and external resources.

An integration flow is a pipeline of messaging components that moves data between an application and an external resource. Messages carry the data through steps that can transform, route, or process it before it reaches its destination. section 10.0 section 10.4

Channels connect the steps. A gateway lets application code submit data through an interface, while a channel adapter connects a channel to an external system. Depending on the job, a flow can also use filters, transformers, routers, splitters, aggregators, and service activators. section 10.2

Write text to a file

A file-writing flow starts when application code calls a gateway method with a filename and text. The gateway turns the call into a message on textInChannel. A transformer converts the payload to uppercase and sends it to fileWriterChannel. A file-writing handler then writes the payload using the filename in the message header; if the file exists, it appends the text with a newline. section 10.1 section 10.1.1

Listing 10.1 Message gateway interface to transform method invocations into messages
package sia6;
 
import org.springframework.integration.annotation.MessagingGateway;
import org.springframework.integration.file.FileHeaders;
import org.springframework.messaging.handler.annotation.Header;
 
@MessagingGateway(defaultRequestChannel="textInChannel")   #1
public interface FileWriterGateway {
 
  void writeToFile(
      @Header(FileHeaders.FILENAME) String filename,       #2
      String data);
 
}

The gateway interface shows how a method call enters the flow and how the filename becomes a message header. section 10.1

In Spring Boot, add the Spring Integration starter and the file endpoint module for this example. The flow can be configured in XML or Java; Java configuration fits naturally with Spring Boot autoconfiguration. In the Java version, @Transformer connects the uppercase transformation to its input and output channels, and @ServiceActivator connects the output channel to a FileWritingMessageHandler. Set setExpectReply(false) on the handler to avoid errors about a missing reply channel. section 10.1 section 10.1.2

Turn emails into orders

An email flow can poll an inbox through an inbound channel adapter, transform each email into an order object, and pass the order to a handler that submits it to a REST API. Configuration properties supply the server, mailbox, credentials, and polling rate; the default rate is 30 seconds. section 10.3

Listing 10.5 Defining an integration flow to accept emails and submit them as orders
package tacos.email;
 
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.integration.dsl.IntegrationFlow;
import org.springframework.integration.dsl.IntegrationFlows;
import org.springframework.integration.dsl.Pollers;
import org.springframework.integration.mail.dsl.Mail;
 
@Configuration
public class TacoOrderEmailIntegrationConfig {
  
  @Bean
  public IntegrationFlow tacoOrderEmailFlow(
      EmailProperties emailProps,
      EmailToOrderTransformer emailToOrderTransformer,
      OrderSubmitMessageHandler orderSubmitHandler) {
    
    return IntegrationFlows
        .from(Mail.imapInboundAdapter(emailProps.getImapUrl()),
            e -> e.poller(
                Pollers.fixedDelay(emailProps.getPollRate())))
        .transform(emailToOrderTransformer)
        .handle(orderSubmitHandler)
        .get();
  }
  
}

The Java DSL configuration brings the email adapter, poller, transformer, and handler together in one flow. section 10.3

How Spring in Action, Sixth Edition covers it

How deep it goes
Explains component roles and walks through file and email flows, while surveying rather than exhaustively covering Spring Integration.
Examples
Uses an uppercase file writer and an email-to-order flow that submits orders to a REST API.
What you can do after
Apply integration flows via Spring Integration to write files and process incoming email orders.
How it connects
Connects message channels, gateways, channel adapters, transformers, routers, and service activators.

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.