Thursday, September 11, 2014

Logging invalid records



Time needed to complete  ~60 minutes
Prerequisite for this tutorial is Configure Spring Batch Admin to use MySQL

1.Introduction

In post Validate input data the batch job stop immediately after finding invalid order record, in real life  scenario if 15 out of 1 million record that needs to be processed are invalid and there is an invalid record in the first 100 records, the batch job will failed, no work is done, the question is can the batch job isolate these records and continues processing and later developers will deal with the invalid records. Spring batch has mechanism  to do that, and in this post will be explained, invalid data will be logged  to the file and for developers there will be hint in what stage(read ,process ,write) the error popped up to know where to look.

2.What is used in this tutorial:


1- Maven 3
2- Jdk 1.7
3- Tomcat 7.0.55
4- Eclipse Luna 4.4
5- Spring Core 3.2.9
6- Spring Batch 2.2.7
7- Spring Batch Admin Manager 1.3.0
8- MySQL 5 Database


3.Project Structure



4. Configuration and Other files

- The database configuration where the data will be written is in file database.xml path src/main/resources/META-INF/spring/batch/dbConfig/database.xml, this is not the same as the database used by spring batch admin to store job repository data. First create the database named company_db and in the database.xml change db_username and db_password to suite company_db username and password.




- The job definition that will read,process and write is in src/main/resources/META-INF/spring/batch/jobs/job-orderInvalidLogging.xml , Listing 2. All exceptions will be skipped and data that caused the exception will be logged. It is interesting to see how changing skip-limit value will change the behavior of the job execution. For example changing skip-limit =2 will cause job to throw:
 org.springframework.batch.core.step.skip.SkipLimitExceededException: Skip limit of '2' exceeded



- The Order class using hibernate validator annotations, listing 3, and there is a custom @AccptedValues validator listing 4 and Listing 5.







- Because of the orderDate property in Order class, mapper is required for the reader, if not implemented
reader will not know the date format.

- The validation process takes place in OrderProcessor class, listing 7.


- Until now all the logging in the previous posts was console output, logging to file will be added to log the invalid records will in jobApplication.log file, Listing 8, logging property file


5. CSV files

- The input file with invalid data that will be used src/main/resources/cvs/skipInvalidOrderData.csv invalid date at:
 line 3 value fixe and future order date.
 line 5 order date format
 line 7 value prepai

- File with valid data can be found in src/main/resources/cvs/orders_12082014.csv





6. Running orderValidationJob in eclipse


- To run the application use code in class src/main/java/com/web/app/LoggingInvalidOrderApp.java Listing 11.



- The result of running is in Listing 12.



7. Running orderJob From Spring Batch Admin

- In pom.xml change username_tc and password_tc to suite your local tomcat. 
- Start tomcat.
- Deploy application from run configuration in goal type clean install tomcat7:redeploy , click apply and 
Run buttons.

Note: If these steps are unfamiliar read steps 4 and 5 deploying spring batch admin Link How To

- open address localhost:8080 /springBatchAdminMysql/jobs  in web browser , picture 2





- Click orderLoggingJob, Change dateTime parameter, click launch button.  job will completed with COMPLETED status.

- Click on executions -> ID number of job execution -> COMPLETED under column Status to the 2 skips by processor and 1 skip by reader picture 3.


- the jobApplication.log file, listing 13, 2 record are skipped in processing phase, while one record was skipped because of parsing error of date in reading phase of the job.


8. Eclipse Project

2 comments: