Back in late 2009 I had written a blog post, where I presented some results from my Apache Camel learning experience. Now in 2011 it is time to do the same for Spring Integration. I am in the process of creating a few examples for Spring Integration that I create while learning it. So my hope is to create a little series of blog posts covering Spring Integration and Enterprise Integration Patterns (EIP) in general.
Nevertheless, don't forget to check out the wonderful examples that are provided via the Spring Integration Git repo at: http://git.springsource.org/spring-integration/samples/. It contains a sophisticated list of examples and is the best place to get started.
Well, let's get started - The source code for my example is available via GitHub, and will be successively expanded with additional examples in the coming weeks:
- A user drops a file in a local directory (/camellos-si/data/inbox/), that is continuously polled by Spring Integration.
- The file is picked up by Spring Integration and converted to a byte array with the original file being deleted.
- The resulting byte array is sent to an in-memory channel.
- A custom transformer will pick up the data array and compress the data using a ZipOutputStream
- The resulting byte array is yet again being sent to another channel.
- Finally an Outbound Channel Adapter will grab the data from the channel and send it via Ftp to an embedded Ftp server (stored under: /camellos-si/data/ftp/).
java -jar camellos-si.jar
That's it. Spring Integration should boot up and you can drop files you want to zip up into '/camellos-si/data/inbox/'.
* * *
Some interesting additional features covered by my example:
Maven Shade Plugin (http://maven.apache.org/plugins/maven-shade-plugin/)
The Maven Shade Plugin can be a nice simplifier in stand-alone deployment scenarios. Let's say you have a project with multiple jar dependencies, then you can use this plugin to merge all jar dependencies into one single jar. You can also configure the Maven Shade Plugin so that it generates the Main-Class property in META-INF/MANIFEST.MF and consequently, you can deliver a single jar (No class-path hell) and execute the application like my simple example:
java -jar camellos-si.jar
Maven Assembly Plugin (http://maven.apache.org/plugins/maven-assembly-plugin/)
The Maven Assembly plugin helps with creating distributable packages. For instance for my example I need not only the compiled jar file but I also need a few directories in the right place, e.g. Apache FtpServer requires the users.properties file to be an actual file (having it as a class path resource embedded in your jar won't work).
Basically, you can use the Maven Assembly Plugin to move things around and to mold your resulting distributable package (a Zip file in my case).
Embedded Ftp Server using Apache FtpServer (http://mina.apache.org/ftpserver/)
Something I always find intriguing with Spring is the simple embedability of additional services such as running ActiveMQ embedded in your Spring context by just adding a single line of Xml, or in this case embedding Apache FtpServer. I know it is a blurry line where you start building your own application server but mentally I still find it easier to do it in Spring than using an app server, particularly when you only want to do it for testing, showcasing etc. and move on to use external ActiveMQ, Ftp Server instances for production etc.
I hope this example has been enlightening for your own Spring Integration discoveries - stay tuned for more.