Spring Integration In-Depth with
Examples
1. What is Spring Integration?
Spring Integration is a framework that extends the Spring programming model to support
the well-known Enterprise Integration Patterns (EIPs). It provides a lightweight messaging
system to integrate systems both within and outside the JVM.
2. Core Concepts of Spring Integration
- **Message**: Data wrapper with headers and payload.
- **Channel**: A pipe where messages flow.
- **Endpoint**: A component that produces, consumes, or processes messages.
- **Gateway**: Entry point for sending messages.
- **Transformer**: Transforms message content.
- **Filter**: Filters out unwanted messages.
- **Router**: Routes messages to different channels.
- **Service Activator**: Invokes a POJO method as message handler.
- **Adapters**: Integrate with external systems (JMS, FTP, REST, etc.).
3. Simple Spring Integration XML Configuration
<int:channel id="inputChannel"/>
<int:service-activator input-channel="inputChannel" ref="echoService" method="echo"/>
<bean id="echoService" class="com.example.EchoService"/>
4. Java DSL Example (Spring Boot)
@Bean
public IntegrationFlow simpleFlow() {
return IntegrationFlows.from(Http.inboundGateway("/echo"))
.transform(String.class, String::toUpperCase)
.handle(System.out::println)
.get();
}
5. Using Gateways
@MessagingGateway
public interface MyGateway {
@Gateway(requestChannel = "inputChannel")
String process(String input);
}
@Bean
public IntegrationFlow gatewayFlow() {
return IntegrationFlows.from("inputChannel")
.handle(msg -> "Hello " + msg)
.get();
}
6. Message Filtering
@Bean
public IntegrationFlow filteringFlow() {
return IntegrationFlows.from("filterChannel")
.filter((String msg) -> msg.startsWith("A"))
.handle(System.out::println)
.get();
}
7. Routers Example
@Bean
public IntegrationFlow routerFlow() {
return IntegrationFlows.from("routerInput")
.<String, Boolean>route(msg -> msg.contains("error"),
mapping -> mapping.channelMapping(true, "errorChannel")
.channelMapping(false, "successChannel"))
.get();
}
8. Transformers
@Bean
public IntegrationFlow transformerFlow() {
return IntegrationFlows.from("transformChannel")
.transform(String.class, String::toUpperCase)
.handle(System.out::println)
.get();
}
9. Connecting to External Systems (e.g., File or JMS)
@Bean
public IntegrationFlow fileInboundFlow() {
return IntegrationFlows.from(Files.inboundAdapter(new File("input"))
.autoCreateDirectory(true)
.patternFilter("*.txt"))
.transform(Files.toStringTransformer())
.handle(System.out::println)
.get();
}
10. Benefits of Using Spring Integration
- Decouples business logic and messaging
- Encourages clean, maintainable integration flows
- Supports synchronous and asynchronous messaging
- Easily integrates with Spring Boot
- Ready-to-use adapters for REST, JMS, AMQP, FTP, etc.