Example 3:Make a file movies.txt that looks like this (movie name:actors separated with comma):
Now we'll try to read the file and put it in simple POJO.
For route to read a file, there is file component. So let's make a new route in configure() method in our route builder class.
In this route we're using file component to read a file movie.txt from some folder and with property noop=true the file will not be removed but just read. The body that component file produces will be of type java.io.File.
When invoking our bean ExampleBean there is a new method giveMeMovies that accepts body from route. In our bean we must therefore create this method.
In this method implementation I use @Body annotation. It is not necessary but I use it for easier code reading.
The method just reads file line by line and creates list of Movie objects. This list is then returned to route and becomes new body with different type. In the route there is processor to show how to get body and data from it.
Example 4:Method giveMeMovies in ExampleGean returns a list of objects. That's why this method can be used with splitter. Splitter splits a message into a number of pieces and process them independently. This time we will not change ExampleBean since we have everything we need to show how splitter works. We just need to make a new route in configure() method in our route builder class.
This route has exactly same functionality as one in Example 3 but is few lines shorter. Be careful to close splitter with .end() or you could have problems when route does something after splitter.
My posts on Apache Camel:
- Part I - Property Placeholder
- Part II - Using Beans
- Part III - More on using Beans
- Part IV - Solr
- Part V - Marshalling and Unmarshalling