0
votes

I am quite new to Spring Batch and am stuck with a problem for which I could not find a solution.

I have create a job which has a step and two flows:

Step 1:

Retrieves a list of contract numbers(for simplification, a unique number which will be used to search further records). Using ItemReader single chunk, it will pass a single contract number to next step.

Flow 1:

This flow has a Step(Reader,Processor,Writer) whose Reader will pick this contract number and retrieve a list of member ids. These Ids will be passed in chunks(of 10) to the processor.

The processor will further perform several Query calls to finally create a Participant details list to the writer. The writer will write this data in chunks to the Workbook object.

Flow 2: Once all the data is written in workbook, the object is sent as a file to a remote location. This process is done using a tasklet which has all the necessary details to send file to the proposed destination.

Now, once this Entire process is completed (Step 1-> Flow 1-> Flow 2) it checks whether any more contract details are to be written into the remote location.

If yes, another Contract number is retrieved from the list which is then passed to the flows(flow1 and flow2) Once all the contract numbers are processed then the code completes with RepeatStatus.FINISHED

Adding a diagram for better understanding:

Diagrammatic representation of the above explanation

It looks something like this:

Job
-> Step 1 (retrieve Id number list but send a single contract number)
   -> Flow 1
      -> Reader
      -> Processor
      -> Writer
   -> Flow 2 
      -> Tasklet (Send file to remote location)
   (If all contract-numbers are not processed go to Step 1 and iterate to the next contract-number else finish the job)

My problems start here:

  1. How do I jump back from flow 2 back to Step 1 based on a condition? I do find several suggestions where people add a decider loop but you can go back to the previous step (in this case of the condition is not satisfied in flow 2, flow 2 will be re-triggered). So how do I jump back from flow2 to Step 1?
  2. How do I pass data between all the steps and flows throughout job? (without using execution context)

If you think there is a better way to do this please do suggest.

1
Is your initial input data set fixed or can change while the job is running? - Mahmoud Ben Hassine
Once the job starts running and I get my input, it will remain the same. However, it will be a list of inputs which needs to be iterated over one by one within the job. - Julia Byers

1 Answers

0
votes

How do I jump back from flow 2 back to Step 1 based on a condition?

Use a JobExecutionDecider for that. You need to make sure that step 1 is allowed to restart even if complete (parameter allowStartIfComplete=true)

How do I pass data between all the steps and flows throughout job? (without using execution context)

If you don't want to share data through the execution context, you can use a shared object between steps.