Spring Integration Java DSL - Baeldung
Spring Integration Java DSL - Baeldung
(/)
Spring (https://www.baeldung.com/category/spring/) +
Spring Integration (https://www.baeldung.com/tag/spring-integration/)
1. Introduction
In this tutorial, we'll learn about the Spring Integration Java DSL for creating
application integrations.
We'll take the le-moving integration we built in Introduction to Spring
Integration (/spring-integration) and use the DSL instead.
2. Dependencies
https://www.baeldung.com/spring-integration-java-dsl 1/11
28/10/2020 Spring Integration Java DSL | Baeldung
https://www.baeldung.com/spring-integration-java-dsl 2/11
28/10/2020 Spring Integration Java DSL | Baeldung
1 @Bean
2 public IntegrationFlow upcaseFlow() {
3 return IntegrationFlows.from("input")
4 .transform(String::toUpperCase)
5 .get();
6 }
from can take several types, but in this tutorial, we will look at just three:
MessageSources
MessageChannels, and
Strings
We'll talk about all three shortly.
After we have called from, some customization methods are now available to
us:
1 IntegrationFlow flow = IntegrationFlows.from(sourceDirectory())
2 .filter(onlyJpgs())
3 .handle(targetDirectory())
4 // add more components
5 .get();
https://www.baeldung.com/spring-integration-java-dsl 3/11
28/10/2020 Spring Integration Java DSL | Baeldung
Simply put, a MessageSource is a place from which messages can come that
are external to the application
(http://joshlong.com/jl/blogPost/spring_integration_adapters_gateways_and
_channels.html).
More speci cally, we need something that can adapt that external source into
the Spring messaging representation. And since this adaptation is focused
on input, these are often called Input Channel Adapters.
The spring-integration- le dependency gives us an input channel adapter
that's great for our use case: FileReadingMessageSource:
1 @Bean
2 public MessageSource<File> sourceDirectory() {
FileReadingMessageSource messageSource = new
3
FileReadingMessageSource();
4 messageSource.setDirectory(new File(INPUT_DIR));
5 return messageSource;
6 }
https://www.baeldung.com/spring-integration-java-dsl 4/11
28/10/2020 Spring Integration Java DSL | Baeldung
In this case, we can make our input source more resilient by telling Spring
Integration to poll that source–our lesystem in this case–every 10 seconds.
And, of course, this doesn't apply to just our le input source, we could add
this poller to any MessageSource.
Or, because this lter is so simple, we could have instead de ned it using a
lambda:
1 IntegrationFlows.from(sourceDirectory())
2 .filter(source -> ((File) source).getName().endsWith(".jpg"));
Now that we have a ltered list of les, we need to write them to a new
location.
Service Activators are what we turn to when we're thinking about outputs in
Spring Integration.
Let's use the FileWritingMessageHandler service activator from spring-
integration- le:
1 @Bean
2 public MessageHandler targetDirectory() {
FileWritingMessageHandler handler = new
3
FileWritingMessageHandler(new File(OUTPUT_DIR));
4 handler.setFileExistsMode(FileExistsMode.REPLACE);
5 handler.setExpectReply(false);
6 return handler;
7 }
https://www.baeldung.com/spring-integration-java-dsl 6/11
28/10/2020 Spring Integration Java DSL | Baeldung
5. Additional Components
In our DSL-based le-moving application, we created an Inbound Channel
Adapter, a Message Filter, and a Service Activator.
Let's look at a few other common Spring Integration components and see how
we might use them.
https://www.baeldung.com/spring-integration-java-dsl 7/11
28/10/2020 Spring Integration Java DSL | Baeldung
There are dozens of channels to pick from, some of the more handy ones
being for concurrency, auditing, or intermediate persistence (think Kafka or
JMS bu ers).
Also, channels can be powerful when combined with Bridges.
5.2. Bridge
When we want to combine two channels, we use a Bridge.
Let's imagine that instead of writing directly to an output directory, we instead
had our le-moving app write to another channel:
1 @Bean
2 public IntegrationFlow fileReader() {
3 return IntegrationFlows.from(sourceDirectory())
4 .filter(onlyJpgs())
5 .channel("holdingTank")
6 .get();
7 }
Now, because we've simply written it to a channel, we can bridge from there
to other ows.
Let's create a bridge that polls our holding tank for messages and writes them
to a destination:
https://www.baeldung.com/spring-integration-java-dsl 8/11
28/10/2020 Spring Integration Java DSL | Baeldung
1 @Bean
2 public IntegrationFlow fileWriter() {
3 return IntegrationFlows.from("holdingTank")
.bridge(e -> e.poller(Pollers.fixedRate(1, TimeUnit.SECONDS,
4
20)))
5 .handle(targetDirectory())
6 .get();
7 }
As we can see, individual bridges can control the polling con guration for
di erent handlers.
As soon as our application context is loaded, we now have a more complex
app in action that will start moving les from the source directory to two target
directories.
6. Conclusion
In this article, we saw various ways to use the Spring Integration Java DSL to
build di erent integration pipelines.
Essentially, we were able to recreate the le-moving application from a
previous tutorial, this time using pure java.
Also, we took a look at a few other components like channels and bridges.
The complete source code used in this tutorial is available over on Github
(https://github.com/eugenp/tutorials/tree/master/spring-integration).
https://www.baeldung.com/spring-integration-java-dsl 9/11
28/10/2020 Spring Integration Java DSL | Baeldung
CATEGORIES
SPRING (HTTPS://WWW.BAELDUNG.COM/CATEGORY/SPRING/)
REST (HTTPS://WWW.BAELDUNG.COM/CATEGORY/REST/)
JAVA (HTTPS://WWW.BAELDUNG.COM/CATEGORY/JAVA/)
SECURITY (HTTPS://WWW.BAELDUNG.COM/CATEGORY/SECURITY-2/)
PERSISTENCE (HTTPS://WWW.BAELDUNG.COM/CATEGORY/PERSISTENCE/)
JACKSON (HTTPS://WWW.BAELDUNG.COM/CATEGORY/JSON/JACKSON/)
HTTP CLIENT-SIDE (HTTPS://WWW.BAELDUNG.COM/CATEGORY/HTTP/)
KOTLIN (HTTPS://WWW.BAELDUNG.COM/CATEGORY/KOTLIN/)
SERIES
JAVA “BACK TO BASICS” TUTORIAL (/JAVA-TUTORIAL)
JACKSON JSON TUTORIAL (/JACKSON)
HTTPCLIENT 4 TUTORIAL (/HTTPCLIENT-GUIDE)
REST WITH SPRING TUTORIAL (/REST-WITH-SPRING-SERIES)
SPRING PERSISTENCE TUTORIAL (/PERSISTENCE-WITH-SPRING-SERIES)
SECURITY WITH SPRING (/SECURITY-SPRING)
ABOUT
ABOUT BAELDUNG (/ABOUT)
https://www.baeldung.com/spring-integration-java-dsl 10/11
28/10/2020 Spring Integration Java DSL | Baeldung
https://www.baeldung.com/spring-integration-java-dsl 11/11