How I reduced my Java batch application’s code by 80% using Easy Batch!
Save up to a workweek a year by efficiently managing your coding bookmarks, aka #codingmarks, on www.codingmarks.org. Share your favorites with the community and they will be published weekly on Github. Help us build the programming-resources location - Star
In this post, I will show how can Easy Batch tremendously simplify your batch applications by reducing plumbing code considerably. This will make your applications code more readable, understandable and maintainable!
The use case is a typical production application that loads data from an input CSV flat file to a database.
Consider the following CSV input file containing products data:
Suppose we have a JPA EntityManager that will be used to persist Product objects to the database. We would like to map each record of this file to an instance of the following Product POJO :
Before persisting products to the database, we should validate data to ensure that:
- product id and name are specified
- product price is not negative
- product last update date is in the past
Finally, we should skip records starting with # from being processed, mainly the header record (there could be other records starting with # like a trailer record that marks the end of data in the file).
To keep the example simple, we will write products data to the standard output and not to a database.
So let’s get started!
The following listing is a possible solution that I have seen a handred times in production systems:
This solution actually works perfectly and implements the requirements above. But it is evident that it is a maintainance nightmare! It could be worse if the Product POJO contained dozen of fields, which is often the case in production.
In this 95 lines solution, there is only one line which represents the batch business logic. Did you guessed it?
It is line 78 := ” + product);// or in production, it would be persisting the object to the database
ALL the rest is plumbing : reading, filtering, parsing and validating data, mapping records to Product instances and reporting some statistics at the end of execution.
This is where Easy Batch comes to play to handle ALL of this plumbing for you! With Easy Batch, you concentrate only on your batch business logic. So let’s see how would be the solution with Easy Batch.
First, we will create a RecordProcessor that will implement our batch business logic :
Then, we will DECLARE (not implement like in the above solution!) data validation constraints on our Product POJO with the elegant Bean Validation API as follows:
Finally, we should just configure Easy Batch to:
- Read data from the flat file
- Filter records starting with #
- Map each CSV record to an instance of the Product POJO
- Validate products data
- and process each record using our ProductProcessor implementation
This can be done with the following snippet:
That’s all! Except implementing the core batch business logic, all we have done is providing configuration metadata that Easy Batch cannot guess! The framework will handle all the plumbing of reading, filtering, parsing, validation and mapping data to the domain object Product.
Now let’s do the count of lines of code. Both solutions use the Product POJO and their respective Main classes have 6 lines of imports, so we will not take into account these imports neither the number of line of code of the Product POJO.
- For the second solution we have:
- 5 lines for the ProductProcessor class (empty lines ignored)
- 4 lines for Bean Validation API annotations we added on the Product POJO
- and 9 lines for the Main class WithEasyBatchLauncher
In sum, 82LOC vs 18LOC, which is 78% less than the first solution!
I am pretty sure there are some readers who will say, but it is not 80% like in the title of the post 😉
Actually I had to put 90% or even 95% because this is not all what Easy Batch have done for us, in addition to all what we have seen above, Easy Batch
- allows to monitor the batch execution with JMX at runtime
So, at the end of this post, this is what Easy Batch is all about, making your life easier when you have to deal with batch applications in Java by letting you concentrate on your batch business logic and handling ALL the rest for you.
Even though the sample I used in this post is about processing a flat file, Easy Batch can also handle the plumbing of processing data from a database or an xml file, which can be more complex than the plumbing code of processing data from a flat file.
You can check out Easy Batch tutorials here!
Published at Codingpedia.org with permission of Mahmoud Ben Hassine – source “How I reduced my Java batch application’s code by 80% using Easy Batch!” from http://www.