Posts

Parallel Flow - Spring Batch Part 10

There will be cases when you will need to have parallel flows. For instance, in our case, Once the order is placed, we want to start the packaging but also we want to update our Gift Repository. Lets see how it is done. With this understanding, lets create three new steps. checkGiftRepositoryStep  - To check if the repository is short of gifts  orderNewGiftsStep  - Order new gifts giftRepositoryUpdatedStep  - Update repository with new gifts 

Reusability - Spring Batch Part 9

Image
With SpringBatch, you are able to reuse existing steps, jobs. Lets look at how we can reuse existing steps. In our case, when a customer orders a chocolate box, we also want to package it and deliver it. These are the steps we used while the customer ordered a corporate gift as well.  full code available at: https://github.com/ricsr/spring-batch-demo/tree/exercise_09

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