Spring Integration is a Spring framework project for connecting applications and application components through real-time message-based integration flows. Messages move through pipelines of components that can transform, split, aggregate, route, or process data. section 1.4.5 section 10.4
A flow connects an application to resources such as files, email, external APIs, or databases. Message gateways and channel adapters provide entry and exit points, while message channels act as the pipes between processing steps. section 10.0 section 10.4
Common building blocks include:
- Message gateways turn method calls into messages; @MessagingGateway can cause Spring Integration to generate the implementation at runtime. section 10.1
- Message channels connect components and determine how messages are delivered, queued, prioritized, or dispatched across threads. section 10.2.1
- Endpoint modules connect flows to external systems; the file endpoint can read files into a flow or write flow data to the filesystem. section 10.1
Writing data to a file
Add the Spring Integration starter and file endpoint dependency, define a FileWriterGateway with defaultRequestChannel="textInChannel", and expose writeToFile(filename, data). Calls to that method become messages and enter the flow, where a file endpoint writes the data to the filesystem. section 10.1
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-integration</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-file</artifactId>
</dependency>
Use the dependency listing to add the Spring Integration starter and filesystem endpoint module to a Maven project. section 10.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);
}
Use the gateway listing as a concrete pattern for converting a Java method invocation into a message with a filename header and text payload. section 10.1
