-1

For a spring batch job, there are steps. In those steps, you typically have a read, process (optional), and write.

What I need to accomplish is in step 1 is read from a database query and print one line from the result to a csv file. This database query could have many lines of data as a result. But I just want it to print one of those lines of data and then move on to the next step.

In the second step, I want to read from a separate database, but query this database based on the ID from the previously written data. That data has an ID. Then, from the result of this new query using that ID, I want to write every line that comes from that query to the same csv file until there is no more data to write from that query. This would all be below the previously written line from step 1.

Then, I want to repeat step 1 and print the next line from that query. Then go again to step two based on this new ID. Continue this loop until step 1 no longer has any lines of data left.

It is not clear how you can do a loop like this with Spring Batch. How can you do a loop similar to this? Especially given that I want to only print one line each time step 1 is ran.

Also, how can I share the ID from step 1 to step 2, so step 2 can use that in the query?

user7977797
  • 73
  • 1
  • 1
  • 10
  • *how can I share the ID from step 1 to step 2* - pass it as a parameter – Scary Wombat May 11 '22 at 00:26
  • Does this answer your question: https://stackoverflow.com/questions/40975705/how-to-run-a-step-in-a-loop-in-spring-batch-updated? – Mahmoud Ben Hassine May 11 '22 at 13:39
  • Unfortunately that post does not really answer the question and I have seen that post before asking this question. The last link in that post seems the most promising, but even that doesn't really do the loop I am describing. Or if it does, it is not clear how that answer aligns with my question. – user7977797 May 11 '22 at 21:23

1 Answers1

0

Does this answer your question: How to run a step in a loop in Spring Batch : Updated

Unfortunately that post does not really answer the question and I have seen that post before asking this question. The last link in that post seems the most promising, but even that doesn't really do the loop I am describing. Or if it does, it is not clear how that answer aligns with my question.

In that case, you can put a decider after the step and make it run the same step until your stop condition is met. The step can put the stop condition (errors, no more data, etc) in the execution context and the decider can use that information from the execution context to make the decision. Please check Programmatic Flow Decisions for more details about how to achieve that with code examples.

Mahmoud Ben Hassine
  • 28,519
  • 3
  • 32
  • 50