Basic Apache Camel and Spring tutorial

The Apache camel tutorial below creates a route to poll the c:/temp/simple folder every minute (i.e. 60000 milliseconds) for files with patterns like test1.csv, test2.csv, etc. These files are then zipped and copied to the archive folder under c:/temp/simple.

camel-with-spring

Step 1: Define the Apache camel and spring libraries required.

Step 2: Define the Spring configuration file applicationContext.xml

Step 3: Define the Apache configuration file with the relevant routes. The rouet.xml defines the CamelContext and simple_file_route.xml defines the route context. More files can be added and included in the rout.xml for additional routes in a modular fashion.

${file:name.noext} is a file expression language, and refer to the Camel manual for further details.

Step 4: Finally, the main class that runs that runs Apache camel in a stand-alone mode continuously polling for files.


Step 5:Run the above class as a Java application, and create a new test1.csv file under c:\temp\simple and you will see that this files gets picked up, zipped, and copied to the archive folder.

Let’s extend the above to show how we can read a file say test1.csv in the format shown below

and then convert it to a format shown below where fields firstname and surname are swapped.

Step 1: Add camel-csv dependency to the pom.xml file that enables reading csv files

Step 2: The next step is to define the routes in the simple_file_route.xml file.

Step 3: The above route uses the “PersonMapper” bean class to transform the fields. This is basically translating the file by rearranging “surname” and “firstname” columns.

That’s all to it. This is the power of the integration framework Apache Camel. It supports 20+ protocols like ftp, smtp, etc.


Java Interview FAQs

800+ Java Interview Q&As

Top