Posts

Showing posts from July, 2020

StepExecutionListener - Adding hooks or listeners - Spring Batch Part 8

You can add interceptors to you steps by using the Listeners around your steps. We will check the StepExecutionListener - no points for guessing what it does - in this blogpost. We add add listeners both before and after Steps. Lets consider that along with Corporate Gifts, you also want to supply beautifully arrange chocolates. Lets create a job for it. full code available at:  https://github.com/ricsr/spring-batch-demo/tree/exercise_08 @Bean public Job orderChocolateBoxJob (){ return this . jobBuilderFactory .get( "orderChocolateBoxJob" ) .start(selectChocolatesStep()) .next(arrangeChocolatesStep()) .build() ; } @Bean public Step selectChocolatesStep (){ return this . stepBuilderFactory .get( "selectChocolatesStep" ).tasklet( new Tasklet() { @Override public RepeatStatus execute (StepContribution contribution , ChunkContext chunkContext) throws Exception { System. out .println( "Ordering Chocolate : "

Stop and Fail Transitions - Spring Batch Part 7

Image
Lets modify our throwException boolean to true and run application from our previous blog post // Toggle true/false to test rerunning failed jobs private boolean throwException = true; In case of an exception thrown by the packageStep, we go to manualPackageStep and the workflow just stops there. But the steps to deliver/pickup have not been executed. If you try rerunning by setting the throwException = false, it wont run.  Note: I have dropped the Spring Job Repository tables just to able to rerun the application just to make it easy to rerun the Job Also, if you check the Step status of packageStep in BATCH_STEP_EXECUTION table, it will show that the Step was ABANDONED and the status of the Job in BATCH_JOB_EXECUTION  table, it will be COMPLETED.  But this is not what we want. We want to manually fix the packaging and then send it out either for delivery or for pickup. The problem is that we are ending our condiotional block using end(), which Spring Batch considers a successful flow

Conditional Flow - Spring Batch Part 6

Jump to Conditional Flow using Custom Status There can be muliple scenario where you want your jobs to proceed depending on the outcome of your steps. For example, in case of our Corporate gifts shop, in case the automatic packaging of the gifts fails, we want some manual intervention to be done i.e. somebody should manually package the item.  IF-ELSE IF loop for packaging:     if packageStep fails                              then manualPackageStep                         else if packageStep executes successfully                              then sendOutForDelivery Entire code available at git repo:   https://github.com/ricsr/spring-batch-demo/tree/exercise_06 Using SpringBatch you can achieve it as follows: Create you manualPackageStep, which is very similar to your packageStep, except that it does the packaging manually. @Bean public Step manualPackageStep () { return this . stepBuilderFactory .get( "manualPackageStep" ).tasklet( new Tasklet() { @Override pu

Rerunning Failed Jobs - Spring Batch Part 5

Image
If the Spring Job fails, Spring Batch Job uses Job Repository to know where the Job Failed, and restarts from that step. Lets see an example on how it is done: entire code can be found at git repo:  https://github.com/ricsr/spring-batch-demo/tree/exercise_05 Note: I have dropped the Spring Job Repository tables just to able to rerun the application just to make it easy to rerun the Job DROP TABLE BATCH_STEP_EXECUTION_CONTEXT ; DROP TABLE BATCH_STEP_EXECUTION ; DROP TABLE BATCH_JOB_INSTANCE ; DROP TABLE BATCH_JOB_EXECUTION_PARAMS ; DROP TABLE BATCH_JOB_EXECUTION_CONTEXT ; DROP TABLE BATCH_JOB_EXECUTION ;

Job Parameters - Spring Batch Part 4

Image
If you try rerunning your Spring Batch application from  Part 3  blogpost, you will see that you are not able to do it. Why? Spring Batch utilizes the Job Repositories table and knows that the job has run already. You have a Corporate Gift Shop., you want to continuously run the batch job so it picks up the orders placed and packages them and ships them. How do you rerun job? The answer is simple, create different Jobs each time you want to run them by passing Job Parameters. You can find the entire code at:   https://github.com/ricsr/spring-batch-demo/tree/exercise_04 In intellij this is how I do, by editing run time parameters: Rerun, and you should be able to see the output. Check the db tables, and you should see new entries. Its very important that you are able to read the Job Parameters as they may have some important information you want to use while running your Job or Logging purpose. If you want to package your gifts in a specific gift wrapper, you could specify like this: @B

Job Repository - Spring Batch Part 3

Image
Spring Batch's Job Repository does the work of Scheduling and Interacting with Jobs. It also manages failures and reruns. As we saw in the previous blog, a Job can have multiple steps - Job Repository handles it all. It does all of the management using a few tables in the database. Till now, we have been using the h2 in-memory database - which actually creates a new instance of the db everytime we restart the application. Lets use standalone h2 db, so we can take a look at the tables involved and the data it holds. Git repo link:  https://github.com/ricsr/spring-batch-demo/tree/exercise_03 Download H2 DB and open the console.  Edit your application.properties with the following as reference: spring.datasource.url = jdbc:h2:tcp://localhost/~/test spring.datasource.driverClassName = org.h2.Driver spring.datasource.username = sa spring.datasource.password = test spring.jpa.database-platform = org.hibernate.dialect.H2Dialect Start your application. Once the application runs successfull

Creating multiple SpringBatch Job - Spring Batch Part2

Let us consider, you own a corporate gift shop and its Christmas time!! You are accepting bulk orders and offer delivery. Lets see how to solve it using Spring Batch. Entire sample code is available at:   https://github.com/ricsr/spring-batch-demo/tree/exercise_02/src/main/java/com/ricsr/springbootdemo There are three steps involved in this process: Read an order  Package an order Ship the order for delivery Let us create these three steps: @Bean public Step readOrderStep () { return this . stepBuilderFactory .get( "readOrderStep" ).tasklet( new Tasklet() { @Override public RepeatStatus execute (StepContribution contribution , ChunkContext chunkContext) throws Exception { System. out .println( "Order Received" ) ; return RepeatStatus. FINISHED ; } }).build() ; } @Bean public Step packageStep () { return this . stepBuilderFactory .get( "packageStep" ).tasklet( new Tasklet() { @Override public RepeatSt

Creating your SpringBatch Application - Part 1

Image
Lets look at creating a very simple Spring Batch Application using Spring Boot. You can download the entire example from:   https://github.com/ricsr/spring-batch-demo/tree/exercise_01 Step1: Lets create our project. Goto  https://start.spring.io/ , and select Spring Batch and H2 Database as dependencies. Click on generate. Note that I am using Gradle. You are free to use Maven for dependency management. Step2: Import the project into you IDE. I am using IntelliJ in my examples. Step3: Edit your main class. In my case its the  SpringbootdemoApplication.class package com.ricsr.springbootdemo ; import org.springframework.batch.core.Job ; import org.springframework.batch.core.Step ; import org.springframework.batch.core.StepContribution ; import org.springframework.batch.core.configuration.annotation. EnableBatchProcessing ; import org.springframework.batch.core.configuration.annotation.JobBuilderFactory ; import org.springframework.batch.core.configuration.annotation.StepBuilderFactory ;

Batch Processing with Spring Batch

Image
What is Batch Processing Runs as a process without human intervention Runs as a background process The input is fixed: Can be service calls, files from a location, db entries etc Scheduled at a particular time Examples of where Batch Processing can be useful: End of Day Balance Sheet Reporting Weekly reporting Monthly reporting Yearly reporting.. and so on Credit Card Statements Synchronizing between two systems (source and target) Jobs should be scheduled according to Business Requirements. Why use Spring Batch with Java? Spring Batch is the de facto standard for batch processing on the JVM. Its implementation of common batch patterns, such as chunk-based processing and partitioning, lets you create high-performing, scalable batch applications that are resilient enough for your most mission-critical processes. Spring Boot provides an additional level of production-grade features to let you speed up the development of your batch processes. Batch processing fits