Tuesday, September 9, 2014

Dealing with empty inputs

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

1.Introduction


Errors in programming are the norm not the exception. Finding and fixing errors is not an easy task, specially when user reports an error that don't exist, both code and data are correct. A common situation in batch processing is empty file, no input. Clients see no output result, the conclusion is batch job didn't finished, report bug. To prevent this situation spring batch defines a listener after step is finished to check the number of reading. 

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



- A bean afterStepListener is defined and referenced in the orderEmptyFileJob job definition, afterStepListener will use code in a class com.web.listener.EmptyFileHandler, Listing 3.


- afterStep method is annotated with @afterStep, and the method will be executed after step, checking the readcount and if it is not greater than zero will log a message that file is empty and return job status failed.

- A bean afterJobListener is defined and referenced in the orderEmptyFileJob job definition, afterJobListener will use code in a class com.web.listener.JobListener, Listing 4.


5. CSV file

- The input empty file that will be used src/main/resources/cvs/emptyFile.csv
- For non empty file test use data in listing 5.1.



6. Running orderEmptyFileJob in eclipse

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


- The result of running is Listing 6.



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



- Click on executions then ID number of job execution, to see exit message EMPTY FILE, picture 4.



8. Eclipse Project


No comments:

Post a Comment