Wednesday, September 10, 2014

Validate Input data



Time needed to complete  ~60 minutes

1.Introduction

What money is for bankers, data is for IT developers . Money can be dirty, data can be dirty too, for this reason developers need to check data using validation  before sending for processing. This article will show how validation can be used in spring batch jobs.

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  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-orderValidate.xml , Listing 2.



- 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.


5. CSV files

- The input file with invalid data that will be used src/main/resources/cvs/invalidOrderData.csv line 7 mobilex and 12-08-2015.
- 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/OrderValidationApp.java Listing 10.



- The result of running is in Listing 11.



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 orderValidationJob, Change dateTime parameter, click launch button.  job will completed with FAILED status.

- Click on executions -> ID number of job execution -> FAILED under column Status to see validation messages picture 3.


8. Eclipse Project

3 comments:

  1. Can you show us how to do the same with org.springframework.batch.item.validator.ValidatingItemProcessor ??

    ReplyDelete