Basic Spring batch tutorial with Maven and Eclipse.

Batch jobs with Spring batch framework is very common and popular. This tutorial assumes that

1) You have set up you Java, Maven, and Eclipse

Once you have followed the following steps, you should have a project structure as shown below.

spring-batch-tut-1

Step 1: Create a new Maven project with the command shown below.

Step 2: Import it into eclipse IDE via File –> import –> Existing Maven Projects.

Step 3: Update the pom.xml file so that it looks like as shown below.

Step 4: Define the reader and writer files as shown below.

spring-batch-tut-2

Step 5: Define the batch job and wire the reader and writer via the Spring context file as shown below.

src/main/resources/applicationBatchContext.xml

Step 6: Finally, the test class to bootstrap “applicationBatchContext.xml”.

Run the SimpleBatchTest java file right clicking on it and the Run As –> Java Application.

Output

Running more than once can throw JobIsAlreadyRunningException. So, you need to supply different JobParameters as shown below.

The above was run with 3 threads and commit-interval of 1. Experiment by changing the “commit-interval” (aka chunk size) within the step and the “concurrencyLimit” within the SimpleAsyncTaskExecutor in the spring config file.


Java Interview FAQs

800+ Java Interview Q&As

Top